#include "leadscheme.h"
#include <QDebug>
#include <QJsonObject>
#include <QJsonValue>
#include <QButtonGroup>
#include "operatorjson.h"
#include <QApplication>
#include <QScrollArea>
LeadScheme::LeadScheme(QWidget * parent ):QWidget (parent)
{
    init();
    initconnect();
    initLay();

}
LeadScheme::~LeadScheme()
{


}

void LeadScheme::init()
{

      m_btnNew.setText("新建");
      m_btndelete.setText("删除");
      m_btnCancel.setText("取消");
      m_btnOK.setText("确认");
      m_btnNew.setStyleSheet("QPushButton{\
                                 background: white;\
                                 border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:hover{\
                                   background: #0d9ddb;\
                 border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:pressed{\
                                   background: #0d9ddb;\
                border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:checked{\
                                 background: #0d9ddb;\
                  border-radius:10px;padding:7px 10px;\
                         }");
      m_btndelete.setStyleSheet("QPushButton{\
                                 background: red;\
                                color:white;\
                                 border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:hover{\
                                   background: #0d9ddb;\
                 border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:pressed{\
                                   background: #0d9ddb;\
                border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:checked{\
                                 background: #0d9ddb;\
                  border-radius:10px;padding:7px 10px;\
                         }");
      m_btnCancel.setStyleSheet("QPushButton{\
                                 background: white;\
                                    color:blue;\
                                 border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:hover{\
                                   background: #0d9ddb;\
                 border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:pressed{\
                                   background: #0d9ddb;\
                border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:checked{\
                                 background: #0d9ddb;\
                  border-radius:10px;padding:7px 10px;\
                         }");
      m_btnOK.setStyleSheet("QPushButton{\
                                 background: green;\
                                 color:white;\
                                 border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:hover{\
                                   background: #0d9ddb;\
                 border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:pressed{\
                                   background: #0d9ddb;\
                border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:checked{\
                                 background: #0d9ddb;\
                  border-radius:10px;padding:7px 10px;\
                         }");

}
bool LeadScheme::initconnect()
{
    bool bCon = true;
    bCon = connect(&m_btnOK,SIGNAL(clicked(bool)),this,SLOT(slotSave()));
    if(!bCon)
     {
                qDebug()<<"connect failed"<<endl;
                return bCon;

    }
    bCon = connect(&m_btnNew,SIGNAL(clicked(bool)),this,SLOT(slotNew()));
    if(!bCon)
     {
                qDebug()<<"connect failed"<<endl;
                return bCon;

    }
    bCon = connect(&m_btndelete,SIGNAL(clicked(bool)),this,SLOT(slotDelete()));
    if(!bCon)
     {
                qDebug()<<"connect failed"<<endl;
                return bCon;

    }
     return bCon;
}
void LeadScheme::initLay()
{
#if 0
    QLabel * labScheme = new QLabel;
    labScheme->setText("导联方案");
    QVBoxLayout * vlayScheme = new QVBoxLayout;
    vlayScheme->addWidget(labScheme);
    vlayScheme->addWidget(&m_LeadSchemes);

    QLabel * labNum = new QLabel;
    labNum->setText("导联");
    QVBoxLayout * vlayNum = new QVBoxLayout;
    vlayNum->addWidget(labNum );
    vlayNum->addWidget(&m_listNum);


    QLabel * labPoints = new QLabel;
    labPoints->setText("测点");
    QVBoxLayout * vlayPoints = new QVBoxLayout;
    vlayPoints->addWidget(labPoints);
    vlayPoints->addWidget(&m_listPoints);


    QLabel * labExPoints = new QLabel;
    labExPoints->setText("参考点");
    QVBoxLayout * vlayExPoints = new QVBoxLayout;
    vlayExPoints->addWidget(labExPoints );
    vlayExPoints->addWidget(&m_listExPoints );


    QHBoxLayout * hlayWPoints =  new QHBoxLayout;
    QWidget     * wPoints = new QWidget;
    wPoints->setLayout(hlayWPoints);
    hlayWPoints->addLayout(vlayNum );
    hlayWPoints->addLayout(vlayPoints);
    hlayWPoints->addLayout(vlayExPoints);

    QLabel * labScheme = new QLabel;
    labScheme->setText("导联方案");
    QVBoxLayout * vlayScheme = new QVBoxLayout;
    vlayScheme->addWidget(labScheme,1,Qt::AlignHCenter);
    vlayScheme->addWidget(&m_LeadSchemes,1);
    vlayScheme->addStretch();


#else

    QLabel * labScheme = new QLabel;
    labScheme->setText("导联方案");

    QLabel * labNum = new QLabel;
    labNum->setText("导联");

     QLabel * labPoints = new QLabel;
    labPoints->setText("测点");

    QLabel * labExPoints = new QLabel;
    labExPoints->setText("参考点");
    //start
    QWidget     * wPoints = new QWidget;
    wPoints->setStyleSheet("QPushButton{\
                          background: rgba(255, 255, 255);\
                                                  color: black;\
                                                  border-radius: 8px;\
                                                  border:0px ; \
                                          }\
                                          QPushButton:hover{\
                                                  color: black;\
                                                  background: rgb(255, 255, 255);\
                                                  border-radius: 8px;\
                                          }\
                                          QPushButton:pressed{\
                                                  color: black;\
                                                  background: rgb(255, 255, 255);\
                                                  border-radius: 8px;\
                                          }\
                                          QPushButton:checked{\
                                                  color: white;\
                                                  background: rgb(51, 144, 76);\
                                                  border-radius: 8px;\
                                          }"\
                                         "QWidget#NavList1{background-color:rgb(255, 255, 255);}"\
                                    );
    QGridLayout * gridlayOut =  new QGridLayout;
    QVBoxLayout  * vlayPoints = new QVBoxLayout;
    vlayPoints->addLayout(gridlayOut);
   // vlayPoints->addStretch();
    wPoints->setLayout(vlayPoints);
    QStringList strList1,strList2,strList3;
    strList1<<"0"<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9"<<"10"<<"11"<<
                            "12"<<"13"<<"14"<<"15";
   strList2<<"F1"<<"F2"<<"F3"<<"F4"<<"C3"<<"C4"<<"P3"<<"P4"<<"O1"<<"O2"<<"F7"<<"F8"<<
                                "T3"<<"T4"<<"T5"<<"T6";
   strList3<<"A1"<<"A2"<<"A1"<<"A1"<<"A2"<<"A1"<<"A2"<<"A1"<<"A2"<<"A1"<<"A2"<<"A1"<<
                                "A1"<<"A2"<<"A1"<<"A2";
    QButtonGroup *pButtonGroup0 = new QButtonGroup(this);

    QButtonGroup *pButtonGroup1 = new QButtonGroup(this);
    // 设置互斥
    pButtonGroup1->setExclusive(true);
    QButtonGroup *pButtonGroup2= new QButtonGroup(this);
    // 设置互斥
    pButtonGroup2->setExclusive(true);
    QButtonGroup *pButtonGroup3 = new QButtonGroup(this);
    // 设置互斥
    pButtonGroup3->setExclusive(true);
    int colmn = 0;

#if 0
    QStringList  Sches;
    Sches<<"8通道"<<"16通道"<<"64通道";
    gridlayOut->addWidget(labScheme,0,colmn++,Qt::AlignHCenter);
    for (int i= 0 ; i<Sches.size();i++)
    {
        QPushButton * btn  =  new QPushButton(Sches.at(i));
        //btn->setMinimumHeight(minHeight);
        btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        pButtonGroup0->addButton(btn);
        gridlayOut->addWidget( btn,i+1,0);
        btn->setCheckable(true);
    }
#else
    QStringList  Sches;
    Sches<<"8通道"<<"16通道"<<"64通道";
    gridlayOut->addWidget(labScheme,0,colmn++,Qt::AlignHCenter);
    QScrollArea * scroll = new QScrollArea;
    scroll->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
    scroll->setWidgetResizable(true);
    scroll->setAlignment(Qt::AlignRight);
    QWidget * wShech = new QWidget;

    gridlayOut->addWidget(scroll,1,colmn-1,12,1);
   // QGridLayout * gridShech  =  new QGridLayout;
    QVBoxLayout * vlayHH = new QVBoxLayout;
    wShech->setLayout(vlayHH);
    for (int i= 0 ; i<Sches.size();i++)
    {
        QPushButton * btn  =  new QPushButton(Sches.at(i));
        btn->setMaximumHeight(40);
        btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        pButtonGroup0->addButton(btn);
        //gridShech->addWidget( btn,i+1,0);
        vlayHH->addWidget(btn);
        btn->setCheckable(true);
    }
    vlayHH->addStretch();

    scroll->setWidget(wShech);

    QHBoxLayout * hbtn  = new QHBoxLayout;
    hbtn->addStretch();
    hbtn->addWidget(&m_btnNew);
    hbtn->addWidget(&m_btndelete);
    //hbtn->addWidget(&m_btnCancel);
    hbtn->addWidget(&m_btnOK);
    hbtn->addStretch();
    gridlayOut->addLayout(hbtn,14,colmn-1,1,1);
    QLabel * desLab = new QLabel;
    desLab->setText("1 选择重构方式0-9\n(注意：0方式不能重构\n2 左键点击重构选择测参点\n3左键点击部位要选择的点)");
     gridlayOut->addWidget(desLab,15,colmn-1,3,1);

#endif

    gridlayOut->addWidget(labNum,0,colmn++,Qt::AlignHCenter);
    int minHeight = 45;
    for (int i = 0 ;i<strList1.size();i++)
    {
          QPushButton * btn  =  new QPushButton(strList1.at(i));
          //btn->setMinimumHeight(minHeight);
          btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
          pButtonGroup1->addButton(btn);
          gridlayOut->addWidget( btn,i+1,1);
           btn->setCheckable(true);

    }
    gridlayOut->addWidget( labPoints,0,colmn++,Qt::AlignHCenter);
    for (int i = 0 ;i<strList2.size();i++)
    {
          QPushButton * btn  =  new QPushButton(strList2.at(i));
           //btn->setMinimumHeight(minHeight);
           btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
          pButtonGroup2->addButton(btn);
          gridlayOut->addWidget( btn,i+1,2);
           btn->setCheckable(true);

    }
    gridlayOut->addWidget( labExPoints,0,colmn++,Qt::AlignHCenter);
    for (int i = 0 ;i<strList3.size();i++)
    {
          QPushButton * btn  =  new QPushButton(strList3.at(i));
          //btn->setMinimumHeight(minHeight);
          btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
          pButtonGroup3->addButton(btn);
          gridlayOut->addWidget( btn,i+1,3);
           btn->setCheckable(true);

    }
//end

#endif
    QWidget * wClickedPints = new QWidget;
    wClickedPints->setStyleSheet("border-image:url(:/image/img_lead.png);}");

    QVBoxLayout * vlaypointsTemp = new QVBoxLayout;
    m_editShecName.setFixedWidth(180);
    m_editShecName.setPlaceholderText("输入方案名称");
    vlaypointsTemp->addWidget(&m_editShecName);
    vlaypointsTemp->addWidget(wClickedPints);
    QWidget * ww  =new QWidget;
    ww->setLayout(vlaypointsTemp);
   // wClickedPints->setFixedWidth(500);
    //wClickedPints->show();
    QHBoxLayout  * hlayAll = new QHBoxLayout;
   // hlayAll->addLayout(vlayScheme,1);
    hlayAll->addWidget(wPoints,3);
    hlayAll->addWidget(ww,3);


    QList<QColor> colors;
    colors.append(QColor(100, 184, 255));
    colors.append(QColor(255, 107, 107));
    colors.append(QColor(24, 189, 155));
    colors.append(QColor(1, 174, 103));
    colors.append(QColor(52, 73, 94));

    int x = 5;
    int y = 5;
    int radius = 50;

    QStringList  listNames;
    listNames<<"Fp1"<<"Fp2"
            <<"F7"<<"F3"<<"Fz"<<"F4"<<"F8"
             <<"A1"<<"T3"<<"C3"<<"Cz"<<"C4"<<"T4"<<"A2"
            <<"T5"<<"P3" <<"Pz"<<"P4" <<"T6"
            <<"O1"<<"O2";

    QJsonObject json;
    OperatorJson Ojson;
    Ojson.readJson(QApplication::applicationDirPath()+"/0.json",json);


    qDebug()<<"hahaha"<<json<<endl;
    for(int i  = 0 ; i< listNames.size();i++)
    {
        MoveButton * btn  =  new MoveButton(wClickedPints);
        btn->setParent(wClickedPints);
        int x =json[listNames.at(i)].toObject()["x"].toInt();
        int  y = json[listNames.at(i)].toObject()["y"].toInt();

        btn->setGeometry(x,y, radius, radius);
        //x = x + radius;
        btn->setText(listNames.at(i));
        btn->setCanMove(true);
        btn->setShowOverlay(true);
        btn->setBgColor(QColor(1, 174, 103));
        m_listMoveBtns.append(btn);
    }


    QWidget  * Wall =  new QWidget;
    Wall->setLayout(hlayAll);
    Wall->setStyleSheet("QWidget \
      {\
          background-color:white;\
           border-radius:10px;\
       }");





    QVBoxLayout * vlayAll = new QVBoxLayout;
    vlayAll->addWidget(Wall);
    //vlayAll->addLayout(hbtn);
    setLayout(vlayAll);
}

  void  LeadScheme::slotSave()
  {
      QJsonObject jsonSave;

      for(int i  = 0;i< m_listMoveBtns.size();i++ )
      {
              auto btn =  m_listMoveBtns.at(i);
              QString text = btn->getText();
              int x = btn->x();
              int y = btn->y();
              QJsonObject perJson;
              perJson.insert("x",x);
              perJson.insert("y",y);
              jsonSave.insert(text,perJson);
              qDebug()<<QString("名称:%1\tx:%2\ty:%3").arg(text).arg(x).arg(y)<<endl;
        }
        OperatorJson json;
        json.savaJson(jsonSave,QApplication::applicationDirPath()+"/0.json");

  }
  void  LeadScheme::slotNew()
  {

  }
  void  LeadScheme::slotDelete()
  {

  }
