#include "cmwcompmotor.h"
#include "ui_cmwcompmotor.h"
#include "logic.h"
#include "cctrlparamtable.h"
#include "base.h"
#include <QHeaderView>
CMWCompMotor::CMWCompMotor(QWidget *parent) :
    CMWidget(parent),
    ui(new Ui::CMWCompMotor)
{
    ui->setupUi(this);
    mchildwidgets[uintptr_t(ui->widget_spd)]=ui->widget_spd;
}

CMWCompMotor::~CMWCompMotor()
{
    delete ui;
}

void CMWCompMotor::MFresh()
{
	CMWidget::MFresh();

	ui->groupBox_3->setVisible(false);
}

void CMWCompMotor::MInit()
{
	CMotorDriver* pMotorDriver=GET_CARD(MOTOR_DRIVER,CMotorDriver);
    CHANNELMAP::iterator ii=pMotorDriver->mchannels.begin();
    Variant* pParam[5*MAX_AXIS]={0};
    int32_t irowY=0;
    QStringList nameList;

    for(;ii!=pMotorDriver->mchannels.end();ii++)
    {
        if((*ii)->m_itype!=CMotorDriver::MODULE_MOTOR){continue;}
        int id = (*ii)->m_iID;
        if (/*id == MOTORG2_POWER ||*/ id == MOTORG3_WIDEINDEX || id == MOTORG4_WIDE5050
            || id == MOTORG7_MASK)
        {
            continue;
        }

#ifndef __USE_G5_G6__
        if (id == MOTORG5_NARROWINDEX || id == MOTORG6_NARROW5050)
        {
            continue;
        }
#endif
        pParam[0+irowY*5]=((*ii)->m_Variants.get_Variant(Var_Motor_Default_speed_ID));
        pParam[1+irowY*5]=((*ii)->m_Variants.get_Variant(Var_Motor_Default_accspd_ID));
        pParam[2+irowY*5]=((*ii)->m_Variants.get_Variant(Var_Motor_Default_decspd_ID));
        pParam[3+irowY*5]=((*ii)->m_Variants.get_Variant(Var_Motor_Default_jerk_ID));
        pParam[4+irowY*5]=((*ii)->m_Variants.get_Variant(Var_Motor_Default_kdec_ID));
        nameList<<(*ii)->m_sName;
        ui->comboBox_Axis->addItem((*ii)->m_sName,(*ii)->m_iID);
        irowY++;
    }
    ui->widget_spd->MParamInit_table(pParam,5,irowY,nameList);

    ui->widget_spd->m_pTable->horizontalHeader()->
            setSectionResizeMode(QHeaderView::Stretch);

    ui->groupBox_3->setVisible(false);
}

void CMWCompMotor::MApply()
{
    CMWidget::MApply();

}

void CMWCompMotor::comp_relmove_msg(int iDirect)
{
    int32_t  motorid=ui->comboBox_Axis->currentData().toInt();
    double dDis=ui->lineEdit_Dis->text().toDouble();
    dDis=fabs(dDis)*(iDirect>0?1:-1);
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("%1 Rel Move").arg(ui->comboBox_Axis->currentText()),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::RELMOVE
                          <<motorid<<dDis,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);
}

void CMWCompMotor::comp_absmove_msg()
{
    int32_t  motorid=ui->comboBox_Axis->currentData().toInt();
    double dPos=ui->lineEdit_Pos->text().toDouble();
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("%1 Abs Move").arg(ui->comboBox_Axis->currentText()),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::ABSMOVE
                          <<motorid<<dPos,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);
}

void CMWCompMotor::comp_jogmove_msg(int iDirect)
{

//    CMotorDriver* pMotorDriver=GET_CARD(MOTORDRIVER,CMotorDriver);
    double dRate=ui->pushButton_HighSpd->isChecked()?0.1:0.01;
    int32_t  motorid=ui->comboBox_Axis->currentData().toInt();
    CMotor* pMotor=GET_CHDIRECT(motorid,CMotor);
    double dSpd=pMotor->m_Variants
            .get_VData(Var_Motor_Default_speed_ID)->toDouble()*dRate;
    dSpd*=iDirect;
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("%1 Jog Move").arg(ui->comboBox_Axis->currentText()),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::STARTMOVE
                          <<motorid<<dSpd,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);

}

void CMWCompMotor::comp_stopmove_msg()
{
    int32_t  motorid=ui->comboBox_Axis->currentData().toInt();
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("%1 Stop Move").arg(ui->comboBox_Axis->currentText()),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::STOPMOVE
                          <<motorid,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);
}


void CMWCompMotor::on_comboBox_Axis_currentIndexChanged(int index)
{
    int32_t  motorid=ui->comboBox_Axis->itemData(index).toInt();
    CMotor* pMotor=GET_CHDIRECT(motorid,CMotor);
    ui->label_unit_Dis->setText(pMotor->m_Variants.get_VData(Var_Motor_Unit_ID)->toString() );
    ui->label_unit_Pos->setText(pMotor->m_Variants.get_VData(Var_Motor_Unit_ID)->toString() );

}


void CMWCompMotor::on_pushButton_defaultSpd_clicked()
{
    CMotorDriver* pMotorDriver=GET_CARD(MOTOR_DRIVER,CMotorDriver);
    CHANNELMAP::iterator ii=pMotorDriver->mchannels.begin();

    V_MsgQueue msgqueue;
    for(;ii!=pMotorDriver->mchannels.end();ii++)
    {
        if((*ii)->m_itype!=CMotorDriver::MODULE_MOTOR){continue;}
        msgqueue.pushback_Msg(VarMsg_Driver_ID,
                              QObject::trUtf8("Set Motor %1 Default Spd Sig").arg((*ii)->m_sName),
                              QVariantList()
                              <<MOTOR_DRIVER<<CMotorDriver::SETDEFAULTSPEED
                              <<(*ii)->m_iID,0);

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

void CMWCompMotor::on_pushButton_JogPas_pressed()
{
    comp_jogmove_msg(1);
}

void CMWCompMotor::on_pushButton_JogPas_released()
{
    comp_stopmove_msg();
}

void CMWCompMotor::on_pushButton_JogNag_pressed()
{
    comp_jogmove_msg(-1);
}

void CMWCompMotor::on_pushButton_JogNag_released()
{
    comp_stopmove_msg();
}

void CMWCompMotor::on_pushButton_HighSpd_clicked(bool checked)
{
    ui->pushButton_HighSpd->setText(checked?tr("HighSpeed"):tr("LowSpeed"));
}

void CMWCompMotor::on_pushButton_MoveRelNag_clicked()
{
    comp_relmove_msg(-1);
}

void CMWCompMotor::on_pushButton_MoveRelPas_clicked()
{
    comp_relmove_msg(1);
}

void CMWCompMotor::on_pushButton_stop_clicked()
{
    comp_stopmove_msg();
}

void CMWCompMotor::on_pushButton_MoveAbs_clicked()
{
    comp_absmove_msg();
}
