#include "mainwidget.h"
#include "plotwidget.h"
#include "ctlwidget.h"
#include "anawidget.h"
#include <QVBoxLayout>
#include <QComboBox>
#include <QtWidgets/QWidget>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QStackedLayout>
#include <QPushButton>
#include <QLabel>
#include <QTextEdit>
#include <QLineEdit>
#include <QGroupBox>


MainWidget::MainWidget(QWidget *parent) : QWidget(parent)
{
    createUI();
}

MainWidget::~MainWidget()
{

}

void MainWidget::createUI()
{
    m_customPlotWidget = new PlotWidget(this);
    m_customRealWidget = new RealWidget(this);
    m_stackLayout = new QStackedLayout();
    m_stackLayout->addWidget(m_customPlotWidget);
    m_stackLayout->addWidget(m_customRealWidget);


    CtlWidget *ctlWidget = new CtlWidget(this);
    ///ctlWidget->setStyleSheet("background-color:rgba(50, 50, 50, 50)");
    ///anaWidget->setGeometry(200,200,800,800);
    ctlWidget->resize(400, 400);

    /*QTreeView *m_treeView = new QTreeView(this);
    m_treeView->resize(400, 400);
    QFileSystemModel *m_model=new QFileSystemModel(this);

    // 设置根目录
    m_model->setRootPath(QDir::currentPath());

    // 设置过滤器,只过滤出.ana
    QStringList filter;
    filter << ".ana";

    // 使用过滤器
    m_model->setNameFilters(filter);
    m_model->setNameFilterDisables(false);

    // 设置数据模型
    m_treeView->setModel(m_model);*/

    AnaWidget *anaWidget = new AnaWidget(this);
    ///anaWidget->setStyleSheet("background-color:rgba(0, 0, 100, 100)");
    ///anaWidget->setGeometry(200,200,800,800);
    anaWidget->resize(400, 400);
    ///anaWidget->hide();

    QGridLayout *baseLayout = new QGridLayout();
    baseLayout->addWidget(ctlWidget, 0, 0, 1, 5);
    baseLayout->addWidget(anaWidget, 1, 0, 10, 1);
    baseLayout->addLayout(m_stackLayout, 1, 1, 10, 1);
    ///baseLayout->addWidget(m_customPlotWidget, 1, 1, 10, 1 );
    //列比 第0列与第1列1:7
    baseLayout -> setColumnStretch(0, 1);
    baseLayout -> setColumnStretch(1, 6);;
    baseLayout -> setRowStretch(0, 1);
    baseLayout -> setRowStretch(1, 8);
    setLayout(baseLayout);


    connect(ctlWidget->m_pushbutton, SIGNAL(clicked()), m_customPlotWidget, SLOT(replot_xlsx_data()));
    connect(ctlWidget->m_pushbutton, SIGNAL(clicked()), ctlWidget, SLOT(disable_load_button()));
    connect(m_customPlotWidget, SIGNAL(calcu_button_enable()), anaWidget, SLOT(enable_calcu_button()));

    connect(anaWidget->m_realbutton, SIGNAL(clicked()), this, SLOT(showStackCPRealWidget()));
    connect(anaWidget->m_anabutton, SIGNAL(clicked()), this, SLOT(showStackCPAnaWidget()));
    connect(anaWidget->m_startbutton, SIGNAL(clicked()), m_customRealWidget, SLOT(start_plot_real_line()));
    connect(anaWidget->m_stopbutton, SIGNAL(clicked()), m_customRealWidget, SLOT(stop_plot_real_line()));

    ///connect(ctlWidget->m_openbutton, SIGNAL(clicked()), SLOT(refresh_));

    connect(anaWidget->m_calcubutton, SIGNAL(clicked()), m_customPlotWidget, SLOT(get_coordinate_calcu()));
    connect(anaWidget->m_choosebutton, SIGNAL(clicked()), m_customPlotWidget, SLOT(show_line_cursor()));
    connect(anaWidget->m_cancelbutton, SIGNAL(clicked()), m_customPlotWidget, SLOT(hide_line_cursor()));
    connect(m_customPlotWidget, SIGNAL(show_area()), anaWidget, SLOT(show_area_edit()));
    connect(m_customPlotWidget, SIGNAL(show_tang()), anaWidget, SLOT(show_tang_edit()));
}

void MainWidget::showStackCPRealWidget()
{
    m_stackLayout->setCurrentIndex(1);
    // uint16_t crcres = 0x1606;
    // ///m_stackLayout->setCurrentIndex(0);
    // QByteArray sendArray;
    // sendArray.resize(9);
    // sendArray[0] = 0x5A;    //帧头
    // sendArray[1] = 0xA5;
    // sendArray[2] = 0x06;    //数据总长度
    // sendArray[3] = 0x83;
    // sendArray[4] = 0x00;
    // sendArray[5] = 0x28;
    // sendArray[6] = 0x01;
    // sendArray[7] = crcres>>8;
    // sendArray[8] = crcres;

    // unsigned char *objbuf = NULL;
    // int lenbuf = sendArray.size();
    // objbuf = (unsigned char*)std::malloc(lenbuf);
    // std::memcpy(objbuf, (unsigned char*)sendArray.data(), lenbuf);

    // for(int i=0; i<lenbuf; i++)
    // {
    //     // if((objbuf[0]==0x5A)&&(objbuf[1]==0xA5))
    //     //     qDebug()<<"16进制"<<QString("%1").arg(objbuf[i],0,16,QLatin1Char('0'));
    // }

    // free(objbuf);
    // objbuf = NULL;

    // unsigned char *crcdataBuf;
    // crcdataBuf = (unsigned char*)std::malloc(7);          //CRC校验
    // ///std::memcpy(crcdataBuf, validBuf+3, (m_data_len+1));
    // crcdataBuf[0] = 0x5A;
    // crcdataBuf[1] = 0xA5;
    // crcdataBuf[2] = 0x06;
    // crcdataBuf[3] = 0x83;
    // crcdataBuf[4] = 0x00;
    // crcdataBuf[5] = 0x28;
    // crcdataBuf[6] = 0x01;

    // uint16_t res = CRC16_MODBUS(crcdataBuf, 7);
    // /*// 转化
    // QByteArray array;
    // array.resize(sizeof(crcdataBuf));//重置数据大小
    // int len = sizeof(crcdataBuf);
    // array = QByteArray::fromRawData(reinterpret_cast<const char*>(crcdataBuf), len);
    // ///std::memcpy((unsigned char*)array.data(), crcdataBuf, len);//复制数据
    // uint16_t crcrec = ModbusCRC16(array);          */  //转化数据错误导致计算错误

    // QByteArray crcArray;
    // crcArray.resize(7);
    // crcArray[0] = 0x5A;    //帧头
    // crcArray[1] = 0xA5;
    // crcArray[2] = 0x06;    //数据总长度
    // crcArray[3] = 0x83;
    // crcArray[4] = 0x00;
    // crcArray[5] = 0x28;
    // crcArray[6] = 0x01;
    // uint16_t crcret = ModbusCRC16(crcArray);

    // for(int i=0; i<crcArray.size(); i++)
    // {
    //     ///qDebug()<<"16进制"<<crcArray.at(i)<<Qt::endl;
    //     qDebug() << QString("%1").arg(crcArray.at(i), 0, 16, QLatin1Char('0'))<<Qt::endl;
    //     ///qDebug("%02x", crcArray.at(i));
    // }
    // qDebug()<<"ModbusCRC16 "<<crcret<<Qt::endl;
    // qDebug()<<"ModbusCRCres "<<res<<Qt::endl;
    // ///qDebug()<<"ModbusCRCrec "<<crcrec<<Qt::endl;

}

void MainWidget::showStackCPAnaWidget()
{
    m_stackLayout->setCurrentIndex(0);
}

uint16_t MainWidget::ModbusCRC16(QByteArray senddata)
{
    int len=senddata.size();
    uint16_t wcrc=0XFFFF;//预置16位crc寄存器，初值全部为1
    uint8_t temp;//定义中间变量
    int i=0,j=0;//定义计数
    for(i=0;i<len;i++)//循环计算每个数据
    {
        temp=senddata.at(i);
        wcrc^=temp;
        for(j=0;j<8;j++){
            //判断右移出的是不是1，如果是1则与多项式进行异或。
            if(wcrc&0X0001){
                wcrc>>=1;//先将数据右移一位
                wcrc^=0XA001;//与上面的多项式进行异或
            }
            else//如果不是1，则直接移出
                wcrc>>=1;//直接移出
        }
    }
    temp=wcrc;//crc的值
    return wcrc;
}


uint16_t MainWidget::CRC16_MODBUS(unsigned char *data, unsigned int datalen)
{
    unsigned short wCRCin = 0xFFFF;
    unsigned short wCPoly = 0x8005;

    InvertUint16(&wCPoly,&wCPoly);
    while (datalen--)
    {
        wCRCin ^= *(data++);
        for(int i = 0;i < 8;i++)
        {
            if(wCRCin & 0x01)
                wCRCin = (wCRCin >> 1) ^ wCPoly;
            else
                wCRCin = wCRCin >> 1;
        }
    }
    return (wCRCin);
}


void MainWidget::InvertUint16(unsigned short *dBuf,unsigned short *srcBuf)
{
    int i;
    unsigned short tmp[4]={0};

    for(i=0;i< 16;i++)
    {
        if(srcBuf[0]& (1 << i))
            tmp[0]|=1<<(15 - i);
    }
    dBuf[0] = tmp[0];
}






