#include "inputchange.h"
#include "ui_inputchange.h"
#include <QDebug>
#include <QVBoxLayout>
#include "src/po/translation.h"
#include "src/model/logicmain.h"
#include <QSize>
#include <QFontMetrics>
#include <QPushButton>
#include <QLineEdit>
#include <ukuistylehelper/ukuistylehelper.h>
using namespace kdk;

#define ICON_SIZE QSize(16, 16)

InputChange::InputChange(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::InputChange)
{
    this->setAccessibleName("kylin-burner_InputChange");
    ui->setupUi(this);
    setModal(true);
    kdk::UkuiStyleHelper::self()->removeHeader(this);

    //判断是否是wayland环境
    if(QString(qgetenv("XDG_SESSION_TYPE")) == "wayland") {
        m_bIsWayland = true;
    } else {
        m_bIsWayland = false;
        MotifWmHints hints;
        hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
        hints.functions = MWM_FUNC_ALL;
        hints.decorations = MWM_DECOR_BORDER;
        XAtomHelper::getInstance()->setWindowMotifHint(winId(), hints);
    }

    ui->buttonBox->setAccessibleName("kylin-burner_QDialogButtonBox_InputChange_buttonBox");
    ui->labelContent->setAccessibleName("kylin-burner_QLabel_InputChange_labelContent");
    ui->lineEditResult->setAccessibleName("kylin-burner_QLineEdit_InputChange_lineEditResult");
    ui->title->setAccessibleName("kylin-burner_WindowTitle_InputChange_title");

    ui->title->onlyShowInDialog();
    connect(ui->title, SIGNAL(close()), this, SLOT(slotClose()));
    connect(this, SIGNAL(windowIconChanged(QIcon)), ui->title, SLOT(onWindowIconChange(QIcon)));
    connect(this, SIGNAL(windowTitleChanged(QString)), ui->title, SLOT(onWindowTitleChange(QString)));
    connect(ui->lineEditResult, &QLineEdit::textChanged, this, &InputChange::slotTextChanged);
    connect(ui->lineEditResult, &QLineEdit::textEdited, this, &InputChange::slotTextChanged);

    ui->lineEditResult->setFocus();
    ui->labelContent->setAlignment(Qt::AlignLeft | Qt::AlignTop);

    ui->buttonBox->clear();					//删除ui文件内置的两个按钮
    m_oCancelBtn = ui->buttonBox->addButton(_("Cancel"), QDialogButtonBox::RejectRole);
    m_oEnsureBtn = ui->buttonBox->addButton(_("OK"), QDialogButtonBox::AcceptRole);

    slotChangeUIFontSize(LogicMain::instance()->GetSystemFontSize());
    connect(LogicMain::instance(), &LogicMain::systemFontSizeChanged, this, &InputChange::slotChangeUIFontSize);
}

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

QString InputChange::result()
{
    qDebug()<<__LINE__<<__func__<<ui->lineEditResult->text();
    return ui->lineEditResult->text();
}
QString InputChange::content()
{
    return ui->labelContent->text();
}

bool InputChange::input(QString &inputed, QString title , QString content, QString fileName)
{
    inputed.clear();
    if(content == _("File name duplicate, need to rename:"))
    {
        qDebug()<<__LINE__<<__func__<<content;
        m_oEnsureBtn->setEnabled(false);
    }

    if (this->result().isEmpty())
    {
        m_oEnsureBtn->setEnabled(false);
    }

    ui->labelContent->setText(content);
    ui->lineEditResult->setText(QString("%1").arg(fileName));
    this->setLineEditSelect(fileName);
    ui->lineEditResult->setFocus();
    if(m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    if (exec())
    {
        inputed =this->result();
        return true;
    }
    return false;
}

void InputChange::setLineEditSelect(QString fileName)
{
    if(fileName.isEmpty()) {
        return;
    }

    if(fileName.contains(".")) {
        ui->lineEditResult->setSelection(0, indexSuffix(fileName));
    } else {
        ui->lineEditResult->selectAll();
    }
}

int InputChange::indexSuffix(QString fileName)
{
    int selectRightPos = fileName.lastIndexOf(".");
    if (fileName.contains(".tar.") && 4 == (selectRightPos - fileName.lastIndexOf(".tar."))) {
        selectRightPos -= 4;
    }
    return selectRightPos;
}

void InputChange::slotTextChanged(QString text){

    if (!text.isEmpty()) {
        m_oEnsureBtn->setEnabled(true);
        m_oEnsureBtn->setProperty("isImportant", true);
    } else {
        m_oEnsureBtn->setEnabled(false);
    }
}

void InputChange::slotClose(){
    this->reject();
}

void InputChange::slotChangeUIFontSize(int fontSize)
{
     int scaleRatio = qAbs(fontSize - 10.0);

     QFont font;
     font = ui->labelContent->font();
     font.setPixelSize(14 + scaleRatio);

     ui->labelContent->setFont(font);
     ui->lineEditResult->setFont(font);

}
void InputChange::keyPressEvent(QKeyEvent *event)
{

    switch (event->key())
    {
    case Qt::Key_Escape:
        this->reject();
        break;
    case  Qt::Key_Enter:
        this->accept();
        break;
    case Qt::Key_Return:
        this->accept();
        break;
    }
}
