#include "sendprogramdialog.h"
#include "ui_sendprogramdialog.h"
#include <QAction>
#include <communication/network.h>

SendProgramDialog::SendProgramDialog(QWidget *parent,QString strProgramName,QString strProgramPath) :
    LoQDialog(parent),
    ui(new Ui::SendProgramDialog)
{
    ui->setupUi(this);
    m_strProgramName=strProgramName;
    m_strProgramPath=strProgramPath;
    ui->label_2->setText(tr("Program Name")+":"+strProgramName);
    QAction *search = new QAction(ui->txtSearch);
    search->setIcon(QIcon(":/res/ProgramManager/bnSearch.png"));
    ui->txtSearch->addAction(search, QLineEdit::LeadingPosition);
    ui->txtSearch->setClearButtonEnabled(true);
    ui->txtSearch->setProperty("ssType", "progManageTool");
    ui->txtSearch->setProperty("ssName", "searchProg");
    ui->wDevicePublishList->setProperty("ssType", "topList");
    ui->wDevicePublishList->setProperty("ssName", "publishTreeWidget1");
  //  ui->wDevicePublishList->setStyleSheet("background-color: #f0f0f0;");
    ui->pushButtonClose->setProperty("ssType", "progManageTool");
    ui->pushButtonClose->setStyleSheet("QPushButton{background:rgba(28,154,210,255);}");
    ui->pushButtonRefresh->setProperty("ssType", "progManageTool");
    ui->pushButtonRefresh->setStyleSheet("QPushButton{background:rgba(28,154,210,255);}");
    ui->pushButtonPublish->setProperty("ssType", "progManageTool");
    ui->pushButtonPublish->setStyleSheet("QPushButton{background:rgba(28,154,210,255);}");
    ui->pushButtonCancel->setProperty("ssType", "progManageTool");
    ui->pushButtonCancel->setStyleSheet("QPushButton{background:rgba(28,154,210,255);}");
    ui->pushButtonMax->setProperty("ssType", "progManageTool");
    ui->pushButtonMax->setStyleSheet("QPushButton{background:rgba(28,154,210,255);}");

    m_headerItem = new QTreeWidgetItem();
    m_headerItem->setTextAlignment(1, Qt::AlignHCenter | Qt::AlignVCenter);

    for(int i=1; i<ENUM_DEVICE_PUBLISH_HEADE_END; i++) {
        m_headerItem->setTextAlignment(i, Qt::AlignCenter);
    }
    m_headerItem->setData(ENUM_DEVICE_PUBLISH_HEADE_CHECK, 0, "");
    m_headerItem->setData(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_ID, 0, tr("Screen ID"));
    m_headerItem->setData(ENUM_DEVICE_PUBLISH_HEADE_REMARK_NAME, 0, tr("Remark Name"));
    m_headerItem->setData(ENUM_DEVICE_PUBLISH_HEADE_ONLINE, 0, tr("Online"));

    m_headerItem->setData(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_IP, 0, tr("Screen IP"));
    m_headerItem->setData(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_SIZE, 0, tr("Screen Size"));
    m_headerItem->setData(ENUM_DEVICE_PUBLISH_HEADE_ENCRYPT,0,tr("Security"));
    m_headerItem->setData(ENUM_DEVICE_PUBLISH_HEADE_PROGRESS, 0, tr("Progress"));
    m_headerItem->setData(ENUM_DEVICE_PUBLISH_HEADE_REMARKS, 0, tr("Remarks"));

    ui->wDevicePublishList->setHeaderItem(m_headerItem);
    ui->wDevicePublishList->header()->setSectionResizeMode(ENUM_DEVICE_PUBLISH_HEADE_CHECK, QHeaderView::Fixed);
    ui->wDevicePublishList->header()->setSectionResizeMode(ENUM_DEVICE_PUBLISH_HEADE_ONLINE, QHeaderView::Fixed);
    ui->wDevicePublishList->header()->setSectionResizeMode(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_ID, QHeaderView::Fixed);
    ui->wDevicePublishList->header()->setSectionResizeMode(ENUM_DEVICE_PUBLISH_HEADE_REMARK_NAME, QHeaderView::Stretch);
    ui->wDevicePublishList->header()->setSectionResizeMode(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_IP, QHeaderView::Fixed);
    ui->wDevicePublishList->header()->setSectionResizeMode(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_SIZE, QHeaderView::Fixed);
    ui->wDevicePublishList->header()->setSectionResizeMode(ENUM_DEVICE_PUBLISH_HEADE_ENCRYPT, QHeaderView::Fixed);
    ui->wDevicePublishList->header()->setSectionResizeMode(ENUM_DEVICE_PUBLISH_HEADE_PROGRESS, QHeaderView::Fixed);
    ui->wDevicePublishList->header()->setSectionResizeMode(ENUM_DEVICE_PUBLISH_HEADE_REMARKS, QHeaderView::Stretch);


     ui->wDevicePublishList->header()->setStretchLastSection(false);
     ui->wDevicePublishList->setColumnWidth(ENUM_DEVICE_PUBLISH_HEADE_CHECK, 52);
     ui->wDevicePublishList->setColumnWidth(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_ID, 130);
     ui->wDevicePublishList->setColumnWidth(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_IP, 130);
     ui->wDevicePublishList->setColumnWidth(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_SIZE, 88);
    ui->wDevicePublishList->setColumnWidth(ENUM_DEVICE_PUBLISH_HEADE_ONLINE, 50);
    ui->wDevicePublishList->setColumnWidth(ENUM_DEVICE_PUBLISH_HEADE_ENCRYPT, 52);
    ui->wDevicePublishList->setColumnWidth(ENUM_DEVICE_PUBLISH_HEADE_PROGRESS, 120);
    ui->wDevicePublishList->setColumnWidth(ENUM_DEVICE_PUBLISH_HEADE_REMARKS, 172);
    connect(ui->pushButtonRefresh,SIGNAL(clicked()),this,SLOT(onRefresh()));
    connect(ui->pushButtonPublish,SIGNAL(clicked()),this,SLOT(onPublish()));
    connect(ui->txtSearch,SIGNAL(textChanged(const QString &)),this,SLOT(FilterProgram(const QString &)));
    connect(ui->pushButtonMax,SIGNAL(clicked()),this,SLOT(onMaxWnd()));
    onRefresh();
    m_pTimer=new QTimer(this);
    connect(m_pTimer,SIGNAL(timeout()),this,SLOT(onRefresh()));
    m_pTimer->start(5000);

}
void SendProgramDialog::onRefresh()
{

    if(p_globaNetComm!=nullptr)
    {
        int iCount=p_globaNetComm->LedCardList.count();
        for(int i=0;i<iCount;i++)
        {
            onAddLedCard(p_globaNetComm->LedCardList.at(i));
        }
        ui->label->setText(tr("All")+":"+QString::number(iCount));

    }

}
void SendProgramDialog::onPublish()
{
    int cnt = ui->wDevicePublishList->topLevelItemCount();
    QStringList selectProgramlist;
    //int ii=0;
    for(int i=0; i<cnt; i++) {
        if(ui->wDevicePublishList->topLevelItem(i)->checkState(0) == Qt::Checked) {
            static_cast<wProgramPublishItem *>(ui->wDevicePublishList->topLevelItem(i))->onPublish();
           // ii++;
        }
        else {
           // ii++;
        }
    }

}
SendProgramDialog::~SendProgramDialog()
{
    if(m_pTimer!=nullptr)
    {
        if(m_pTimer->isActive())
            m_pTimer->stop();
        delete m_pTimer;
    }
    delete ui;
}
void SendProgramDialog::onAddLedCard(LedCard *p)
{
    // qDebug() << " SLOT onAddLedCard FUNC:" << p->m_strCardId;
     int iExistFlg=0;
     int cnt = ui->wDevicePublishList->topLevelItemCount();
     for(int i=0; i<cnt; i++) {
         QString strTempCardId=static_cast<wProgramPublishItem*>(ui->wDevicePublishList->topLevelItem(i))->GetLedCardId();

         if(strTempCardId == p->m_strCardId) {
             iExistFlg=1;
             static_cast<wProgramPublishItem*>(ui->wDevicePublishList->topLevelItem(i))->SetItemParam(p);
             break;
         }
     }
     if(iExistFlg==0)
     {
         m_pwDeviceItemList.append(new wProgramPublishItem( p, ui->wDevicePublishList,this,m_strProgramName,m_strProgramPath));
     }
}
void SendProgramDialog::FilterProgram(const QString &strtemp)
{
    if (strtemp.isEmpty())	//显示全部
    {
        for (int i = 0; i< ui->wDevicePublishList->topLevelItemCount(); ++i)
        {
            QTreeWidgetItem* topItem = ui->wDevicePublishList->topLevelItem(i);
            ui->wDevicePublishList->setRowHidden(i,ui->wDevicePublishList->indexFromItem(topItem->parent()),false);
        }
    }
    else
    {
        QList<QTreeWidgetItem*> resultList = ui->wDevicePublishList->findItems(strtemp, Qt::MatchContains,ENUM_DEVICE_PUBLISH_HEADE_SCREEN_ID);	//搜索结果
        if (resultList.size() > 0)
        {
            //QMessageBox::warning(this, "Export", QString(resultList.size()));
            for (int i = 0; i< ui->wDevicePublishList->topLevelItemCount(); ++i)
            {
                QTreeWidgetItem* topItem = ui->wDevicePublishList->topLevelItem(i);
                if (resultList.contains(topItem))
                    ui->wDevicePublishList->setRowHidden(i,ui->wDevicePublishList->indexFromItem(topItem->parent()),false);	//显示匹配的结果
                else
                    ui->wDevicePublishList->setRowHidden(i,ui->wDevicePublishList->indexFromItem(topItem->parent()),true);	//隐藏不匹配的结果
            }
        }
        else {

            QList<QTreeWidgetItem*> resultList6 = ui->wDevicePublishList->findItems(strtemp, Qt::MatchContains,ENUM_DEVICE_PUBLISH_HEADE_REMARK_NAME);	//搜索结果
            if (resultList6.size() > 0)
            {
                //QMessageBox::warning(this, "Export", QString(resultList.size()));
                for (int i = 0; i< ui->wDevicePublishList->topLevelItemCount(); ++i)
                {
                    QTreeWidgetItem* topItem = ui->wDevicePublishList->topLevelItem(i);
                    if (resultList6.contains(topItem))
                        ui->wDevicePublishList->setRowHidden(i,ui->wDevicePublishList->indexFromItem(topItem->parent()),false);	//显示匹配的结果
                    else
                        ui->wDevicePublishList->setRowHidden(i,ui->wDevicePublishList->indexFromItem(topItem->parent()),true);	//隐藏不匹配的结果
                }
            }
            else {


                //QMessageBox::warning(this, "Export", "FilterProgram");
                QList<QTreeWidgetItem*> resultList1 = ui->wDevicePublishList->findItems(strtemp, Qt::MatchContains,ENUM_DEVICE_PUBLISH_HEADE_SCREEN_SIZE);	//搜索结果
                if (resultList1.size() > 0)
                {
                    //QMessageBox::warning(this, "Export", QString(resultList.size()));
                    for (int i = 0; i< ui->wDevicePublishList->topLevelItemCount(); ++i)
                    {
                        QTreeWidgetItem* topItem = ui->wDevicePublishList->topLevelItem(i);
                        if (resultList1.contains(topItem))
                            ui->wDevicePublishList->setRowHidden(i,ui->wDevicePublishList->indexFromItem(topItem->parent()),false);	//显示匹配的结果
                        else
                            ui->wDevicePublishList->setRowHidden(i,ui->wDevicePublishList->indexFromItem(topItem->parent()),true);	//隐藏不匹配的结果
                    }
                }
                else {
                    QList<QTreeWidgetItem*> resultList2 = ui->wDevicePublishList->findItems(strtemp, Qt::MatchContains,ENUM_DEVICE_PUBLISH_HEADE_SCREEN_IP);	//搜索结果
                    if (resultList2.size() > 0)
                    {
                        //QMessageBox::warning(this, "Export", QString(resultList.size()));
                        for (int i = 0; i< ui->wDevicePublishList->topLevelItemCount(); ++i)
                        {
                            QTreeWidgetItem* topItem = ui->wDevicePublishList->topLevelItem(i);
                            if (resultList2.contains(topItem))
                                ui->wDevicePublishList->setRowHidden(i,ui->wDevicePublishList->indexFromItem(topItem->parent()),false);	//显示匹配的结果
                            else
                                ui->wDevicePublishList->setRowHidden(i,ui->wDevicePublishList->indexFromItem(topItem->parent()),true);	//隐藏不匹配的结果
                        }
                    }
                    else {
                        for (int i = 0; i< ui->wDevicePublishList->topLevelItemCount(); ++i)
                        {
                            QTreeWidgetItem* topItem = ui->wDevicePublishList->topLevelItem(i);
                                ui->wDevicePublishList->setRowHidden(i,ui->wDevicePublishList->indexFromItem(topItem->parent()),true);	//隐藏不匹配的结果
                        }
                    }
                }
            }




        }
    }

}
void SendProgramDialog::onMaxWnd()
{
    QWidget *pWindow = this;//window();
    if(pWindow->isMaximized()) {
        pWindow->showNormal();
        qDebug()<<"showNormal";
    } else {
        pWindow->showMaximized();
        qDebug()<<"showMaximized";
    }
}
