#include "cmwfactorymotorconfig.h"
#include "ui_cmwfactorymotorconfig.h"
#include "logic.h"
#include "cctrlcheckboxid.h"
CMWFactoryMotorConfig::CMWFactoryMotorConfig(QWidget *parent) :
    CMWidget(parent),
    ui(new Ui::CMWFactoryMotorConfig)
{
    ui->setupUi(this);
    m_iTimerInterval=100;
    Icon_OFF.load(CMakePath::make_bin_path("../theme/led_off.png"));
    Icon_ON.load(CMakePath::make_bin_path("../theme/led_blue.png"));

    Icon_OFF=Icon_OFF.scaledToWidth(24);
    Icon_OFF=Icon_OFF.scaledToHeight(24);
    Icon_ON=Icon_ON.scaledToWidth(24);
    Icon_ON=Icon_ON.scaledToHeight(24);
}

CMWFactoryMotorConfig::~CMWFactoryMotorConfig()
{
    delete ui;
}
#define iParamCountConfig 5
#define iParamCountHome 7
void CMWFactoryMotorConfig::MInit()
{
    CMotorDriver* pMotorDriver=GET_CARD(MOTOR_DRIVER,CMotorDriver);
    CHANNELMAP::iterator ii=pMotorDriver->mchannels.begin();
    Variant* pParam_Config[iParamCountConfig*MAX_AXIS]={0};
    Variant* pParam_Home[iParamCountHome*MAX_AXIS]={0};
    int32_t irowY=0;
    QStringList nameList;

    QList<int> IDList;

    for(;ii!=pMotorDriver->mchannels.end();ii++)
    {
        if((*ii)->m_itype!=CMotorDriver::MODULE_MOTOR){continue;}
        pParam_Config[0+irowY*iParamCountConfig]=((*ii)->m_Variants.
                                                  get_Variant(Var_Motor_Direction_Reverse_ID));

        pParam_Config[1+irowY*iParamCountConfig]=((*ii)->m_Variants.
                                                  get_Variant(Var_Motor_Nag_softlimit_ID));
        pParam_Config[2+irowY*iParamCountConfig]=((*ii)->m_Variants.
                                                  get_Variant(Var_Motor_Pas_softlimit_ID));

        pParam_Config[3+irowY*iParamCountConfig]=((*ii)->m_Variants.
                                                  get_Variant(Var_Motor_Nag_Pos_Thres));

        pParam_Config[4+irowY*iParamCountConfig]=((*ii)->m_Variants.
                                                  get_Variant(Var_Motor_Pas_Pos_Thres));

//        pParam_Config[5+irowY*iParamCountConfig]=((*ii)->m_Variants.
//                                                  get_Variant(Var_Motor_Default_kdec_ID));
//        pParam_Config[6+irowY*iParamCountConfig]=((*ii)->m_Variants.
//                                                  get_Variant(Var_Motor_Default_jerk_ID));



        nameList<<(*ii)->m_sName;

        pParam_Home[0+irowY*iParamCountHome]=((*ii)->m_Variants.
                                                  get_Variant(Var_Base_SerialID_ID));
        pParam_Home[1+irowY*iParamCountHome]=((*ii)->m_Variants.
                                              get_Variant(Var_Motor_Home_mode_ID));
        pParam_Home[2+irowY*iParamCountHome]=((*ii)->m_Variants.
                                              get_Variant(Var_Motor_Home_speed_ID));
        pParam_Home[3+irowY*iParamCountHome]=((*ii)->m_Variants.
                                              get_Variant(Var_Motor_Home_creep_ID));
        pParam_Home[4+irowY*iParamCountHome]=((*ii)->m_Variants.
                                              get_Variant(Var_Motor_Home_accspeed_ID));
        pParam_Home[5+irowY*iParamCountHome]=((*ii)->m_Variants.
                                              get_Variant(Var_Motor_Home_decspeed_ID));
        pParam_Home[6+irowY*iParamCountHome]=((*ii)->m_Variants.
                                              get_Variant(Var_Motor_Home_orginaloffset_ID));


        IDList<<(*ii)->m_iID;
        irowY++;

    }

    ui->tableWidget_Sensor->setColumnCount(4);
    ui->tableWidget_Sensor->setRowCount(irowY);
    ui->tableWidget_Sensor->setEditTriggers(QAbstractItemView::NoEditTriggers);
    QStringList header;
    header<<QObject::trUtf8("Motor")<<QObject::trUtf8("Nag Limit")
         <<QObject::trUtf8("Pas Limit")<<QObject::trUtf8("Axis Enable");

    ui->tableWidget_Sensor->setHorizontalHeaderLabels(header);

    for(int i=0;i<irowY;i++)
    {
        QTableWidgetItem* pitem=new QTableWidgetItem(nameList[i]);
        ui->tableWidget_Sensor->setItem(i,0,pitem);
        pitem->setData(Qt::UserRole,IDList[i]);

        pitem=new QTableWidgetItem(QIcon(Icon_ON),QString());
        ui->tableWidget_Sensor->setItem(i,1,pitem);

        pitem=new QTableWidgetItem(QIcon(Icon_ON),QString());
        ui->tableWidget_Sensor->setItem(i,2,pitem);

        pitem=new QTableWidgetItem();

        ui->tableWidget_Sensor->setItem(i,3,pitem);
        CCtrlCheckBoxID*  pcheckbox=new CCtrlCheckBoxID(ui->tableWidget_Sensor,IDList[i]);
        ui->tableWidget_Sensor->setCellWidget(i,3,pcheckbox);

        connect(pcheckbox,SIGNAL(MClick_Signal(int,bool)),
                this,SLOT(on_AxisEnableClick(int,bool)));

        V_MsgOutput Output;
        Output.vList.push_back(false);
        m_NagLimit.push_back(Output);
        m_PasLimit.push_back(Output);
    }


    ui->widget_config->
            MParamInit_table(pParam_Config,iParamCountConfig,irowY,nameList);
    mchildwidgets[(uintptr_t)ui->widget_config]=ui->widget_config;

    ui->widget_home->
            MParamInit_table(pParam_Home,iParamCountHome,irowY,nameList);
    mchildwidgets[(uintptr_t)ui->widget_home]=ui->widget_home;

}

void CMWFactoryMotorConfig::MTimeFresh()
{
    int iSize= ui->tableWidget_Sensor->rowCount();
    for(int i=0;i<iSize;i++)
    {
        int iMotorID=ui->tableWidget_Sensor->item(i,0)->data(Qt::UserRole).toInt();
        V_MsgQueue msgqueue;
        msgqueue.pushback_Msg(VarMsg_Driver_ID, QObject::tr("Fresh Input IO Sig"),
                              QVariantList() << MOTOR_DRIVER << CMotorDriver::IS_MOTOR_ONFRONTLIMIT
                              << iMotorID, &(m_PasLimit[i]));


        msgqueue.pushback_Msg(VarMsg_Driver_ID, QObject::tr("Fresh Input IO Sig"),
                              QVariantList() << MOTOR_DRIVER << CMotorDriver::IS_MOTOR_ONREARLIMIT
                              << iMotorID, &(m_NagLimit[i]));

        m_gSystem.FindSM(SM_IO)->appandMsgqueue_toIdle(msgqueue);
        ui->tableWidget_Sensor->item(i,1)
                ->setIcon(m_NagLimit[i].vList[0].toBool()?Icon_OFF:Icon_ON);

        ui->tableWidget_Sensor->item(i,2)
                ->setIcon(m_PasLimit[i].vList[0].toBool()?Icon_OFF:Icon_ON);
    }
}

void CMWFactoryMotorConfig::MFresh()
{
    int iSize= ui->tableWidget_Sensor->rowCount();
    CMotorDriver* pMotorDriver=GET_CARD(MOTOR_DRIVER,CMotorDriver);
    try
    {
        for(int i=0;i<iSize;i++)
        {
            int iMotorID=ui->tableWidget_Sensor->item(i,0)->data(Qt::UserRole).toInt();
            QCheckBox*  pcheckbox=(QCheckBox*)ui->tableWidget_Sensor->cellWidget(i,3);
            pcheckbox->setChecked(pMotorDriver->is_motor_enabled(iMotorID));
        }

    }
    catch(QString& s)
    {
        MqDebug()<<s;
    }
}

void CMWFactoryMotorConfig::on_AxisEnableClick(int iMotoriD, bool bchecked)
{
    MqDebug()<<"on_AxisEnableClick  MOTORID:"<<iMotoriD;
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Set Multiplier Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::ENABLE_AXIS
                          <<iMotoriD<<bchecked,0);

    m_gSystem.start_test(SM_MAIN, msgqueue,false);
}
