#include "debuginfodialog.h"
#include "ui_debuginfodialog.h"

DebugInfoDialog::DebugInfoDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DebugInfoDialog),
    m_itemPerPage(25)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground, true);//设置窗体背景透明
    setWindowModality(Qt::ApplicationModal);
    connect(g_pMachine, SIGNAL(siDebugInfo()), this, SLOT(slotDebugInfo()));

    initDialog();
    initControl();
}

DebugInfoDialog::~DebugInfoDialog()
{
    delete ui;
}
void DebugInfoDialog::initDialog()
{
    m_curPages = 1;//当前页数
    m_pageNums = 1;//总页数
    m_infoList.clear();
}
void DebugInfoDialog::initControl()
{
    m_DebugInfoBtnList.append(ui->button1);
    m_DebugInfoBtnList.append(ui->button2);
    m_DebugInfoBtnList.append(ui->button3);
    m_DebugInfoBtnList.append(ui->button4);
    m_DebugInfoBtnList.append(ui->button5);
    m_DebugInfoBtnList.append(ui->button6);
    m_DebugInfoBtnList.append(ui->button7);
    m_DebugInfoBtnList.append(ui->button8);
    m_DebugInfoBtnList.append(ui->button9);
    m_DebugInfoBtnList.append(ui->button10);
    m_DebugInfoBtnList.append(ui->button11);
    m_DebugInfoBtnList.append(ui->button12);
    m_DebugInfoBtnList.append(ui->button13);
    m_DebugInfoBtnList.append(ui->button14);
    m_DebugInfoBtnList.append(ui->button15);
    m_DebugInfoBtnList.append(ui->button16);
    m_DebugInfoBtnList.append(ui->button17);
    m_DebugInfoBtnList.append(ui->button18);
    m_DebugInfoBtnList.append(ui->button19);
    m_DebugInfoBtnList.append(ui->button20);
    m_DebugInfoBtnList.append(ui->button21);
    m_DebugInfoBtnList.append(ui->button22);
    m_DebugInfoBtnList.append(ui->button23);
    m_DebugInfoBtnList.append(ui->button24);
    m_DebugInfoBtnList.append(ui->button25);
    //根据不同分辨率设置控件的位置和尺寸
    switch (g_emResolut)
    {
    case resolution1910:
        initResolution1910();
        break;
    case resolution1006:
        initResolution1006();
        break;
    default:
        this->resize(1920,1080);
        break;
    }
    initControlStyle();//初始化窗体控件样式
    //refreshUi();
}
void DebugInfoDialog::initResolution1910()
{
    //上状态栏
    this->resize(1322,935);
    this->move(192+g_mainWidgetPos.x(),65+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0,0,1322,935);
    ui->labelMainTitle->setGeometry(12,8,300,40);
    for(int i = 0; i < m_itemPerPage; i++)
    {
        m_DebugInfoBtnList[i]->setGeometry(30,52 + i * 34,800,30);
    }
    ui->labelPage->setGeometry(12,890,400,40);
    ui->buttonPgUp->setGeometry(1040,40,118,40);
    ui->buttonPgDn->setGeometry(1178,40,118,40);
    ui->buttonCancel->setGeometry(1040,100,118,40);
}
void DebugInfoDialog::initResolution1006()
{
     m_itemPerPage = m_itemPerPage -8;
    this->resize(707,513);
    this->move(101+g_mainWidgetPos.x(),36+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0,0,710,513);
    ui->labelMainTitle->setGeometry(10,6,300,28);
    for(int i = 0; i < m_itemPerPage; i++)
    {
        m_DebugInfoBtnList[i]->setGeometry(10,30 + i * 27,500,24);
    }
    ui->labelPage->setGeometry(10,485,150,30);
    ui->buttonPgUp->setGeometry(520,40,82,30);
    ui->buttonPgDn->setGeometry(610,40,82,30);
    ui->buttonCancel->setGeometry(520,80,82,30);
}
//初始化窗体控件样式
void DebugInfoDialog::initControlStyle()
{
    SetControlStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());
    //背景图
    //QString style1 = "background-color: rgb(62, 62, 62);outline:none;border:0px;border-radius:10px;";
    QString style1 = "background-color: rgb(246, 242, 239);outline:none;border:0px;border-radius:10px;";
    ui->frameBack->setStyleSheet(style1);
    ui->labelMainTitle->setFont(fontNormal_3);
    ui->labelMainTitle->setStyleSheet(LABELGREYSTYLE);

    QString style2 = "border:0px;text-align:left;background-color:rgba(255, 255, 255, 0);color: rgb(78, 78, 78);";//0, 183, 13
    for(int i = 0; i < m_DebugInfoBtnList.size(); i++)
    {
        m_DebugInfoBtnList[i]->setStyleSheet(style2);//文本左对齐
        m_DebugInfoBtnList[i]->setFont(fontNormal_4);
    }
    ui->labelPage->setFont(fontNormal_4);
    ui->labelPage->setStyleSheet(LABELGREYSTYLE);
    ui->buttonPgUp->setOrangeGradientBottomStyle(BORDER_RADIUS2);
    ui->buttonPgUp->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgUp->objectName()),12);
    ui->buttonPgDn->setOrangeGradientBottomStyle(BORDER_RADIUS2);
    ui->buttonPgDn->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgDn->objectName()),12);
    ui->buttonCancel->setOrangeGradientBottomStyle(BORDER_RADIUS2);
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonCancel->objectName()),12);
}
void DebugInfoDialog::addListInfo()
{
    m_infoList = g_pSettings->readToCsv(TYPE_DEBUGINFO);//读取   QStringList
    refreshUi();
}
void DebugInfoDialog::refreshUi()
{
    int itemNum;
    if (m_infoList.size() <= 0)
    {
        itemNum = 0;

        for (int i = 0; i < m_itemPerPage; i++)
        {
            m_DebugInfoBtnList.at(i)->hide();
        }
        ui->labelPage->setText(tr("pageNum: 0/0"));//页数: 0/0
        ui->buttonPgUp->setEnabled(false);
        ui->buttonPgDn->setEnabled(false);
        //ui->buttonOk->setEnabled(false);

        return;
    }
    else
    {
        itemNum = m_infoList.size();
    }

    m_pageNums = (itemNum + m_itemPerPage - 1)/m_itemPerPage;// 计算页数
    if (m_curPages > m_pageNums)// 当前页
    {
        m_curPages = m_pageNums;
    }
    if (m_curPages <= 1 && itemNum != 0)
    {
        m_curPages = 1;
    }

    int itemidx = (m_curPages - 1) * m_itemPerPage;

    for (int i = 0; i < m_itemPerPage; i++)
    {
        if (itemidx < itemNum)
        {
            //设置图标和文件夹名称
            m_DebugInfoBtnList[i]->setText(m_infoList.at(itemidx));
            m_DebugInfoBtnList.at(i)->show();

            itemidx++;
        }
        else
        {
            m_DebugInfoBtnList.at(i)->hide();
        }
    }

    // 翻页按钮
    if (m_curPages <= 1)
    {
        ui->buttonPgUp->setEnabled(false);
    }
    else
    {
        ui->buttonPgUp->setEnabled(true);
    }
    if (m_curPages >= m_pageNums)
    {
        ui->buttonPgDn->setEnabled(false);
    }
    else
    {
        ui->buttonPgDn->setEnabled(true);
    }

    // 页信息
    if(m_pageNums == 0)
    {
        m_curPages = 0;
    }

    QString str = tr("pageNum: ") + QString("%1/%2").arg(m_curPages).arg(m_pageNums);//页数:
    ui->labelPage->setText(str);
}
void DebugInfoDialog::slotDebugInfo()
{
    DebugInfo info;//调试信息
    char mDebugInfoStr[1024+1];

    info = g_pMachine->getDebugInfo();
    memcpy(mDebugInfoStr, info.debugInfoStr, 1024);
    mDebugInfoStr[1024] = 0;

    QString strDebugInfoStr;
    strDebugInfoStr.sprintf("%s", mDebugInfoStr);
    QString strCurTime = QDateTime::currentDateTime().toString("yyyy/MM/dd/hh:mm:ss");//时间
    QString strMessage;
    strMessage = QString("%1//%2").arg(strCurTime).arg(strDebugInfoStr);//时间，内容
    m_infoList.insert(0,strMessage);

    //m_infoList = g_pSettings->readToCsv(TYPE_DEBUGINFO);//读取   QStringList
    refreshUi();
}
void DebugInfoDialog::on_buttonCancel_clicked()
{
     done(0);
}
void DebugInfoDialog::on_buttonPgUp_clicked()
{
    m_curPages--;
    refreshUi();
}
void DebugInfoDialog::on_buttonPgDn_clicked()
{
    m_curPages++;
    refreshUi();
}
