#include "wordsinputdialog.h"
#include "ui_wordsinputdialog.h"

WordsInputDialog::WordsInputDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WordsInputDialog)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_TranslucentBackground, true);//设置窗体背景透明

    m_buttonCharacterList.clear();
    m_buttonCharacterList.append(ui->buttonChara1);
    m_buttonCharacterList.append(ui->buttonChara2);
    m_buttonCharacterList.append(ui->buttonChara3);
    m_buttonCharacterList.append(ui->buttonChara4);
    m_buttonCharacterList.append(ui->buttonChara5);
    m_buttonCharacterList.append(ui->buttonChara6);
    m_buttonCharacterList.append(ui->buttonChara7);
    m_buttonCharacterList.append(ui->buttonChara8);
    m_buttonCharacterList.append(ui->buttonChara9);
    m_buttonCharacterList.append(ui->buttonChara10);

    for(int var = 0; var < m_buttonCharacterList.size(); var++)
    {
        connect(m_buttonCharacterList.at(var),
                SIGNAL(clicked()),
                this,
                SLOT(inputBtnClick())
                );
    }


    m_buttonWordList.clear();
    m_buttonWordList.append(ui->buttonLatterQ);
    m_buttonWordList.append(ui->buttonLatterW);
    m_buttonWordList.append(ui->buttonLatterE);
    m_buttonWordList.append(ui->buttonLatterR);
    m_buttonWordList.append(ui->buttonLatterT);
    m_buttonWordList.append(ui->buttonLatterY);
    m_buttonWordList.append(ui->buttonLatterU);
    m_buttonWordList.append(ui->buttonLatterI);
    m_buttonWordList.append(ui->buttonLatterO);
    m_buttonWordList.append(ui->buttonLatterP);
    m_buttonWordList.append(ui->buttonLatterA);
    m_buttonWordList.append(ui->buttonLatterS);
    m_buttonWordList.append(ui->buttonLatterD);
    m_buttonWordList.append(ui->buttonLatterF);
    m_buttonWordList.append(ui->buttonLatterG);
    m_buttonWordList.append(ui->buttonLatterH);
    m_buttonWordList.append(ui->buttonLatterJ);
    m_buttonWordList.append(ui->buttonLatterK);
    m_buttonWordList.append(ui->buttonLatterL);
    m_buttonWordList.append(ui->buttonLatterZ);
    m_buttonWordList.append(ui->buttonLatterX);
    m_buttonWordList.append(ui->buttonLatterC);
    m_buttonWordList.append(ui->buttonLatterV);
    m_buttonWordList.append(ui->buttonLatterB);
    m_buttonWordList.append(ui->buttonLatterN);
    m_buttonWordList.append(ui->buttonLatterM);

    for(int var = 0; var < m_buttonWordList.size(); var++)
    {
        connect(m_buttonWordList.at(var),
                SIGNAL(clicked()),
                this,
                SLOT(inputBtnClick())
                );
    }

    m_buttonNumberList.clear();
    m_buttonNumberList.append(ui->buttonNum1);
    m_buttonNumberList.append(ui->buttonNum2);
    m_buttonNumberList.append(ui->buttonNum3);
    m_buttonNumberList.append(ui->buttonNum4);
    m_buttonNumberList.append(ui->buttonNum5);
    m_buttonNumberList.append(ui->buttonNum6);
    m_buttonNumberList.append(ui->buttonNum7);
    m_buttonNumberList.append(ui->buttonNum8);
    m_buttonNumberList.append(ui->buttonNum9);
    m_buttonNumberList.append(ui->buttonNum0);
    for(int var = 0; var < m_buttonNumberList.size(); var++)
    {
        connect(m_buttonNumberList.at(var),
                SIGNAL(clicked()),
                this,
                SLOT(inputBtnClick())
                );
    }

    m_shift = 1;
    m_inputStr.clear();
    ui->labelNameNewWords->clear();
    ui->labelNameOldWords->clear();
    ui->buttonOk->setEnabled(false);

    initControl();
    initArabControlPos();
}

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

void WordsInputDialog::setInputStr(QString str)
{
    m_inputStr = m_inputStr + str;
    ui->labelNameNewWords->setText(m_inputStr);
    if(m_inputStr.length() > 0)
    {
        ui->buttonOk->setEnabled(true);
    }
}

//初始化窗体控件，包括位置、尺寸、样式
void WordsInputDialog::initControl()
{
    //根据不同分辨率设置控件的位置和尺寸
    switch (g_emResolut)
    {
    case resolution1910:
        initResolution1910();
        break;
    case resolution1006:
        initResolution1006();
    default:
        this->resize(1920,1080);
        break;
    }

    initControlStyle();//初始化窗体控件样式
}

//初始化窗体控件为1920x1080分辨率
void WordsInputDialog::initResolution1910()
{
    this->resize(1454,840);
    this->move((1920-this->width())/2+g_mainWidgetPos.x(),(1080-this->height())/2+g_mainWidgetPos.y());

    ui->frameBack->setGeometry(0,0,1454,840);
    ui->labelTitle->setGeometry(68,46,1000,56);

    ui->labelName->setGeometry(82,145,340,56);
    ui->labelNameNewWords->setGeometry(452,145,554,56);
    ui->labelNameOldWords->setGeometry(1040,151,340,56);


    for(int i = 0; i < m_buttonCharacterList.size(); i++)
    {
        m_buttonCharacterList[i]->setGeometry(78+i*130,254,122,70);
    }

    for(int i = 0; i < m_buttonNumberList.size(); i++)
    {
        m_buttonNumberList[i]->setGeometry(78+i*130,334,122,70);
    }

    int x = 0;
    int y = 0;
    int idx = 0;
    for(int i = 0; i < m_buttonWordList.size(); i++)
    {
        if(m_buttonWordList[i]->text().toUpper() == "Q")
        {
           x = 78;
           y = 414;
           idx = i;
        }
        if(m_buttonWordList[i]->text().toUpper() == "A")
        {
           x = 140;
           y = 494;
           idx = 0;
        }
        if(m_buttonWordList[i]->text().toUpper() == "Z")
        {
           x = 270;
           y = 574;
           idx = 0;
        }
        m_buttonWordList[i]->setGeometry(x+idx*130,y,122,70);
        idx++;
    }

    ui->buttonShift->setGeometry(80,574,182,70);
    ui->buttonBack->setGeometry(1182,574,192,70);
    ui->frameTwo->setGeometry(0,676,1462,894);
    ui->buttonOk->setGeometry(1014,0,174,70);
    ui->buttonCancel->setGeometry(1200,0,174,70);
}
//初始化窗体控件为1024x600分辨率
void WordsInputDialog::initResolution1006()
{
    this->resize(820,501);
    this->move((1024-this->width())/2+g_mainWidgetPos.x(),(600-this->height())/2+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0,0,820,501);
    ui->labelTitle->setGeometry(40,21,1024,48);

    ui->labelName->setGeometry(50,82,181,44);
    ui->labelNameNewWords->setGeometry(258,82,554,44);
    ui->labelNameOldWords->setGeometry(560,82,200,44);


    for(int i = 0; i < m_buttonCharacterList.size(); i++)
    {
        m_buttonCharacterList[i]->setGeometry(50+i*73,140,62,40);
    }

    for(int i = 0; i < m_buttonNumberList.size(); i++)
    {
        m_buttonNumberList[i]->setGeometry(50+i*73,190,62,40);
    }

    int x = 0;
    int y = 0;
    int idx = 0;
    for(int i = 0; i < m_buttonWordList.size(); i++)
    {
        if(m_buttonWordList[i]->text().toUpper() == "Q")
        {
           x = 50;
           y = 240;
           idx = i;
        }
        if(m_buttonWordList[i]->text().toUpper() == "A")
        {
           x =86;
           y = 290;
           idx = 0;
        }
        if(m_buttonWordList[i]->text().toUpper() == "Z")
        {
           x = 159;
           y = 340;
           idx = 0;
        }
        m_buttonWordList[i]->setGeometry(x+idx*73,y,62,40);
        idx++;
    }
    ui->buttonShift->setGeometry(50,340,98,40);
    ui->buttonBack->setGeometry(671,340,98,40);
    ui->frameTwo->setGeometry(564,400,321,51);
    ui->buttonOk->setGeometry(0,0,96,40);
    ui->buttonCancel->setGeometry(108,0,96,40);
}

//初始化窗体控件样式
void WordsInputDialog::initControlStyle()
{
    SetControlStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    //白蓝色渐变、灰蓝色渐变
    QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(255, 255, 255), stop:1 rgb(215, 243, 254));border-radius:5px;}";
    style += "QPushButton:pressed{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(217, 222, 224), stop:1 rgb(191,213,223));}";
    style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(230, 230, 230), stop:1 rgb(182,182,182));}";

    for(int i = 0; i < m_buttonWordList.size(); i++)
    {
        m_buttonWordList[i]->setStyleSheet(style);
        m_buttonWordList[i]->setFont(fontNormal_1);
    }

    for(int i = 0; i < m_buttonNumberList.size(); i++)
    {
        m_buttonNumberList[i]->setStyleSheet(style);
        m_buttonNumberList[i]->setFont(fontNormal_1);
    }

    for(int i = 0; i < m_buttonCharacterList.size(); i++)
    {
        m_buttonCharacterList[i]->setStyleSheet(style);
        m_buttonCharacterList[i]->setFont(fontNormal_1);
    }

    ui->frameBack->setStyleSheet(TRANSPARENTSTYLE);
    ui->frameBack->setStyleSheet(setControlStyle.getStyleSheet(this->objectName()));

    ui->labelName->setStyleSheet(LABELNAVYBLUESTYLE);
    ui->labelName->setFont(fontNormal_1);
//    ui->labelName->setAlignment(Qt::AlignRight);
//    ui->labelName->setAlignment(Qt::AlignVCenter);

    ui->labelTitle->setStyleSheet(LABELWHITESTYLE);
    ui->labelTitle->setFont(fontNormal_1);
    ui->labelTitle->setAlignment(Qt::AlignLeft);
    ui->labelTitle->setAlignment(Qt::AlignVCenter);

    ui->labelNameNewWords->setStyleSheet(LABELNAVYBLUESTYLE);
    ui->labelNameNewWords->setFont(fontNormal_1);
    ui->labelNameNewWords->setAlignment(Qt::AlignLeft);
    ui->labelNameNewWords->setAlignment(Qt::AlignVCenter);

    ui->labelNameOldWords->setStyleSheet(LABELNAVYBLUESTYLE);
    ui->labelNameOldWords->setFont(fontNormal_1);
    ui->labelNameOldWords->setAlignment(Qt::AlignLeft);
    ui->labelNameOldWords->setAlignment(Qt::AlignVCenter);

    ui->buttonShift->setStyleSheet(style);
    ui->buttonShift->setFont(fontNormal_1);

    ui->buttonBack->setStyleSheet(style);
    ui->buttonBack->setFont(fontNormal_1);

    ui->frameTwo->setStyleSheet(TRANSPARENTSTYLE);
    ui->buttonOk->setWhiteBlueBottomStyle();
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonOk->objectName()+"_1"),12);
    ui->buttonCancel->setWhiteBlueBottomStyle();
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonCancel->objectName()+"_1"),12);
}

void WordsInputDialog::initArabControlPos()
{
    int language = g_pSettings->readFromInHMIiFile("HMI/language").toInt();
    if(language != arabic)
    {
        return;
    }

    ui->labelTitle->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    ui->labelName->setAlignment(Qt::AlignLeft| Qt::AlignVCenter);
    ui->labelNameOldWords->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
}

void WordsInputDialog::setTitleStr(QString str)
{
    ui->labelTitle->setText(str);
}

void WordsInputDialog::setOldWordsStr(QString str)
{
    ui->labelNameOldWords->setText(str);
}

void WordsInputDialog::clean()
{
    m_inputStr.clear();
    ui->labelNameNewWords->clear();
    ui->buttonOk->setEnabled(false);
}

void WordsInputDialog::wifiClean()
{
    m_inputStr.clear();
    ui->labelNameNewWords->clear();
    ui->buttonOk->setEnabled(true);
}

QString WordsInputDialog::getInputStr()
{
    return m_inputStr;
}

void WordsInputDialog::inputBtnClick()
{
    QPushButton *button = (QPushButton*) this->sender() ;

    if(button == NULL)
    {
        return;
    }
    QString str = button->text();
    setInputStr(str);
}

void WordsInputDialog::on_buttonShift_clicked()
{
    m_shift = -1 * m_shift;

    for(int i = 0; i < m_buttonWordList.size(); i++)
    {
        if(m_shift == -1)
        {
            QString lostr=m_buttonWordList[i]->text().toLower();
            m_buttonWordList[i]->setText(lostr);
        }
        else if(m_shift == 1)
        {
            QString upstr=m_buttonWordList[i]->text().toUpper();
            m_buttonWordList[i]->setText(upstr);
        }
    }
}

void WordsInputDialog::on_buttonBack_clicked()
{
    if(m_inputStr.length() <= 0)
    {
        return;
    }

    m_inputStr = m_inputStr.remove(m_inputStr.length()-1,m_inputStr.length());    // 删除最后一位字符
    ui->labelNameNewWords->setText(m_inputStr);

    if(m_inputStr.length() <= 0)
    {
        ui->buttonOk->setEnabled(false);
    }
}

void WordsInputDialog::on_buttonOk_clicked()
{
    done(1);
}

void WordsInputDialog::on_buttonCancel_clicked()
{
    clean();
    done(0);
}
