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

#define ICON_SIZE QSize(16, 16)

InputChange::InputChange(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::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);
    }

    QFont f;
    f = ui->labelContent->font();
    f.setPixelSize(14);
    ui->title->onlyShowInDialog();
    connect(ui->title, SIGNAL(close()), this, SLOT(close()));
    connect(this, SIGNAL(windowIconChanged(QIcon)), ui->title, SLOT(onWindowIconChange(QIcon)));
    connect(this, SIGNAL(windowTitleChanged(QString)), ui->title, SLOT(onWindowTitleChange(QString)));
    connect(ui->lineEditResult,SIGNAL(textEdited(QString)),this,SLOT(setText(QString)));
    ui->labelContent->setFont(f);
    ui->lineEditResult->setFocus();
    ui->lineEditResult->setFont(f);
    ui->labelContent->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    ui->buttonBox->clear();					//删除ui文件内置的两个按钮
    m_oCancelBtn = ui->buttonBox->addButton(_("Cancel"), QDialogButtonBox::AcceptRole);
    m_oEnsureBtn = ui->buttonBox->addButton(_("OK"), QDialogButtonBox::RejectRole);
}

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

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

QString InputChange::input(QString title, QString content, QString result)
{
    m_result=result;
    ui->lineEditResult->setFocus();
    ui->lineEditResult->setText(result);
    ui->lineEditResult->setFocus();
    m_labelContent = content;
    QFontMetrics fontMetrics(this->font());
    int fontSize = fontMetrics.width(m_labelContent);
    ui->labelContent->setMinimumWidth(fontSize);
    ui->labelContent->setWordWrap(false);
    this->setMinimumWidth(ui->labelContent->width() + 45);
    ui->labelContent->setText(content);
    if(m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    if (!exec())
    {
        return this->result();
    }
    return QString();
}

bool InputChange::input(QString &inputed, QString title , QString content, QString result)
{
    inputed.clear();
    if(content==_("File name duplicate, need to rename:")){
        m_oEnsureBtn->setEnabled(false);
    }
    inputed=result;
    ui->lineEditResult->setFocus();
    ui->lineEditResult->setText(result);
    ui->lineEditResult->setFocus();
    connect(ui->lineEditResult,SIGNAL(textEdited(QString)),this,SLOT(setText(QString)));
    ui->labelContent->setText(content);
    if(m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    show();
    ui->lineEditResult->setSelection(result.length(), 0);
    if (!exec())
    {
        inputed =this->result();
        return true;
    }
    return false;
}

void InputChange::setText(QString text){
    m_oEnsureBtn->setEnabled(true);
}

void InputChange::close(){
    this->accept();
}

void InputChange::keyPressEvent(QKeyEvent *event)
{

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