#include <Nice/Property/BridgeMemFn.hh>
#include <Nice/Qt/WidgetFactory.hh>
#include <Nice/Qt/QtWidgetAttributeName.h>
#include <Ltcs/Qt/Iif/ServiceWidget.h>
#include <Ltcs/Qt/Iif/ServiceWidgetUI.h>
#include <Ltcs/Iif/PropertyName.h>
#include <Qt/qglobal.h>
#include <QTextEdit>
#include <boost/assign/list_of.hpp> // for 'map_list_of()'

//extern "C" void instantType()
//{
//    D_LOG("Load Basda::MoccaServiceWidgetClassic");
//}

LtcsIifServiceWidget::LtcsIifServiceWidget(QWidget * parent,
        bool _connect) :
    QWidget(parent), Nice::BridgeItem()
{
    Nice::BridgeItem::m_autoSynch = true;

    m_iifView = new Ui::LtcsIifServiceWidgetUI;
    m_iifView->setupUi(this);

    init(this);

}

void LtcsIifServiceWidget::initSlots()
{
    m_iifView->m_basdaServiceState->Nice::BridgeItem::connect(
        m_root->node("STATE"));
    
    NICE_QT_CONNECT ( m_root->leaf ( "STATE" ), this, LtcsIifServiceWidget, setState, Basda::ServiceStateMachine );
   
    m_iifView->m_reachableLed->Nice::BridgeItem::connect(
        m_root->leaf("CMD")->attribute(
            Ltcs::Iif::Svc::AttributeName::isReachable()));
    m_iifView->m_nameItemLabel->Nice::BridgeItem::connect(
        m_root->node("NAME"));

    m_iifView->m_altItemLabel->Nice::BridgeItem::connect(
        m_root->node(Ltcs::Iif::Svc::PropertyName::varALTString()));
    m_iifView->m_azItemLabel->Nice::BridgeItem::connect(
        m_root->node(Ltcs::Iif::Svc::PropertyName::varAZString()));
    m_iifView->m_decItemLabel->Nice::BridgeItem::connect(
        m_root->node(Ltcs::Iif::Svc::PropertyName::varDEC()));
    m_iifView->m_raItemLabel->Nice::BridgeItem::connect(
        m_root->node(Ltcs::Iif::Svc::PropertyName::varRA()));
    m_iifView->m_paItemLabel->Nice::BridgeItem::connect(
        m_root->node(Ltcs::Iif::Svc::PropertyName::varPA()));
    m_iifView->m_lstItemLabel->Nice::BridgeItem::connect(
        m_root->node(Ltcs::Iif::Svc::PropertyName::varLST()));
    m_iifView->m_utcItemLabel->Nice::BridgeItem::connect(
        m_root->node(Ltcs::Iif::Svc::PropertyName::varUTC()));

//    m_iifView->m_raTimeEdit->Nice::BridgeItem::connect(
//            m_root->node("CMD.SETPRESETTELESCOPE.RA"));
//    m_iifView->m_decTimeEdit->Nice::BridgeItem::connect(
//            m_root->node("CMD.SETPRESETTELESCOPE.DEC"));
}

void LtcsIifServiceWidget::setState (Basda::ServiceStateMachine _state)
{
  
  if(m_state != _state.now())
  {
      m_state = _state.now();
      if(m_state == Basda::ONLINE)
      {
         m_iifView->checkBox->setChecked(true);
         m_iifView->m_presetTelescopePushButton->setEnabled(true);
      }
      else
      {
         m_iifView->checkBox->setChecked(false);
         m_iifView->m_presetTelescopePushButton->setEnabled(false);
      }
  }  
}


void LtcsIifServiceWidget::initAttributes()
{
    m_cmd = Ltcs::IifServiceWorkerInterface::create(getNode());
}

void LtcsIifServiceWidget::slotQt_presetTelescope()
{
    try
    {
//		Nice::Time ra = m_root->getAtNode<Nice::Time>("CMD.SETPRESETTELESCOPE.RA");
//		Nice::Time dec = m_root->getAtNode<Nice::Time>("CMD.SETPRESETTELESCOPE.DEC");
        int raH = m_iifView->m_raHSpinBox->value();
        int raMin = m_iifView->m_raMinSpinBox->value();
        double raSec = m_iifView->m_raSecDoubleSpinBox->value();
        int decDeg = m_iifView->m_decDegSpinBox->value();
        int decMin = m_iifView->m_decMinSpinBox->value();
        double decSec = m_iifView->m_decSecDoubleSpinBox->value();

        m_cmd->setPresetTelescopeStart(raH, raMin, raSec, decDeg, decMin, decSec);
    }

    catch (Basda::ServiceWorkerInterfaceIsBusyException &e)
    {
        E_LOG(e)
    }

    catch (Basda::ServiceWorkerInterfaceCommandFailedException &e)
    {
        E_LOG(e)
    }

    catch (const std::exception& ex)
    {
        E_LOG(ex.what());
    }
}

void LtcsIifServiceWidget::slotQt_authorize(bool _b)
{
  Basda::ServiceStateMachine c = m_root->getAtNode<Basda::ServiceStateMachine>("STATE");
  c.setNext(_b ? Basda::ONLINE : Basda::STANDBY);
  m_root->setAtNode<Basda::ServiceStateMachine>("STATE", c);
  
}

struct WidgetFactoryTypeMapInitializerLtcsIifServiceWidgetClassic
{
    WidgetFactoryTypeMapInitializerLtcsIifServiceWidgetClassic()
    {
        NicePlugins::addPlugin(
            new QTPluginImpl<LtcsIifServiceWidget>(
                "LtcsIifServiceWidget.h",
                "LtcsIifServiceWidget"));
        Nice::WidgetFactory::addWidgetFactory<
        LtcsIifServiceWidget>(
            "LtcsIifServiceWidget",
            "LtcsIifServiceWidget");
    }
    ;
} s_WidgetFactoryTypeMapInitializerLtcsIifServiceWidget;
