#include "burndata.h"
#include "filefilterrules.h"
#include "mainwindow.h"
#include "ui_filefilterrules.h"
#include "src/frame/xatom-helper.h"
#include "src/frame/logger.h"
#include "src/frame/configuration.h"
#include "src/po/translation.h"
#include "src/model/logicmain.h"
#include "src/data/datadirectory.h"
#include "src/frame/signals.h"
#include "KF5/KWindowSystem/kwindowsystem.h"
#include <QPushButton>
#include <QDebug>
#include <ukuistylehelper/ukuistylehelper.h>
#include <windowmanager/windowmanager.h>
#include <QDesktopWidget>
using namespace kdk;

FileFilterRules *FileFilterRules::m_oInstance = nullptr;
QMutex           FileFilterRules::m_oMutex;

FileFilterRules::FileFilterRules(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FileFilterRules)
{
    this->setObjectName("kylin-burner_FileFilterRules");
    ui->setupUi(this);

    //判断是否是wayland环境
    if(QString(qgetenv("XDG_SESSION_TYPE")) == "wayland") {
        m_bIsWayland = true;
    } else {
        m_bIsWayland = true;
        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_FileFilterRules_buttonBox");
    ui->checkBoxHidden->setAccessibleName("kylin-burner_QCheckBox_FileFilterRules_checkBoxHidden");
    ui->checkBoxRepleace->setAccessibleName("kylin-burner_QCheckBox_FileFilterRules_checkBoxRepleace");
    ui->checkBoxSymbolLink->setAccessibleName("kylin-burner_QCheckBox_FileFilterRules_checkBoxSymbolLink");
    ui->labelTitle->setAccessibleName("kylin-burner_QLabel_FileFilterRules_labelTitle");
    ui->title->setAccessibleName("kylin-burner_WindowTitle_FileFilterRules_title");
    ui->widget_2->setAccessibleName("kylin-burner_QWidget_FileFilterRules_widget2");
    ui->widget_3->setAccessibleName("kylin-burner_QWidget_FileFilterRules_widget3");
    ui->widget_4->setAccessibleName("kylin-burner_QWidget_FileFilterRules_widget4");
    ui->widget_5->setAccessibleName("kylin-burner_QWidget_FileFilterRules_widget5");

    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)));

    setWindowIcon(QIcon::fromTheme("burner"));
    setWindowTitle(_("Burner")/* + " - " + _("FileFilterRules")*/);
    connect(g_user_signal, &GlobalUserSignal::iconChange, [=](QIcon icon){
        setWindowIcon(icon);
    });

    ui->labelTitle->setText(_("FileFilterRules"));

    ui->checkBoxHidden->setText(_("Filter hidden file(s)."));
    ui->checkBoxSymbolLink->setText(_("Filter broken symbol links."));
    ui->checkBoxRepleace->setText(_("Repleace symbol link file(s)."));

    m_ishidden=false;
    m_isbroken=false;
    m_isrepleace=false;

    connect(ui->checkBoxHidden, &QCheckBox::toggled, this, &FileFilterRules::hidden,Qt::UniqueConnection);
    connect(ui->checkBoxSymbolLink, &QCheckBox::toggled, this, &FileFilterRules::broken,Qt::UniqueConnection);
    connect(ui->checkBoxRepleace, &QCheckBox::toggled, this, &FileFilterRules::repleace,Qt::UniqueConnection);

    uiAdjust();

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

FileFilterRules *FileFilterRules::instance()
{
    if (nullptr == m_oInstance)
    {
        QMutexLocker locker(&m_oMutex);
        if (nullptr == m_oInstance)
        {
            m_oInstance = new FileFilterRules();
        }
    }
    return m_oInstance;
}

void FileFilterRules::init(void *data)
{
    bool isHidden = Configuration::instance()->get("FilterFile","hidden",true).toBool();
    DataDirectory::setFilterHidden(isHidden);
    ui->checkBoxHidden->setChecked(isHidden);

    bool isBroken = Configuration::instance()->get("FilterFile","broken",true).toBool();
    DataDirectory::setFilterBrokenSymbolLinks(isBroken);
    ui->checkBoxSymbolLink->setChecked(isBroken);

    bool isReplace = Configuration::instance()->get("FilterFile","Repleace",true).toBool();
    DataDirectory::setRepleaceSymbolLinks(isReplace);
    ui->checkBoxRepleace->setChecked(isReplace);

    DataDirectory *node = (DataDirectory *)data;
    if (node)
    {
        node->execHidden(node->filterHidden());
        node->execBroken(node->filterBrokenSymbolLinks());
        node->execRepleace(node->repleaceSymbolLinks());
    }
}

void FileFilterRules::show()
{
    ui->checkBoxHidden->setChecked(m_ishidden);
    ui->checkBoxSymbolLink->setChecked(m_isbroken);
    ui->checkBoxRepleace->setChecked(m_isrepleace);
    KWindowSystem::setState(this->winId(), NET::SkipPager | NET::SkipSwitcher | NET::SkipTaskbar);
    m_oEnsureBtn->setProperty("isImportant", true);
    move(m_oPosition);
    if(m_bIsWayland) {
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    }else{
        QPoint position = MainWindow::Instance()->frameGeometry().topLeft();
        move(position.x()+(MainWindow::Instance()->width()-width())/2, position.y()+(MainWindow::Instance()->height()-height())/2);
        kdk::WindowManager::setGeometry(windowHandle(),geometry());
    }
    QDialog::exec();
}

void FileFilterRules::updatePosition(QPoint p)
{
    m_oPosition.setX(p.x() - width() / 2);
    m_oPosition.setY(p.y() - height() / 2);
}


void FileFilterRules::paintEvent(QPaintEvent *event)
{
    QPalette pal = palette();
    if ("ukui-black" == MAIN->theme())
    {
        pal.setBrush(QPalette::Background,QColor("#1D1D1D"));
    }
    else
    {
        pal.setBrush(QPalette::Background, QColor("#FFFFFF"));
    }
    setPalette(pal);
    QDialog::paintEvent(event);
}

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


void FileFilterRules::uiAdjust()
{
    setFixedSize(376,321+32);
    ui->title->setFixedSize(380,40);
    ui->title->setContentsMargins(8,4,8,4);
    ui->widget_3->setContentsMargins(15,11,24,0);
    ui->widget_2->setContentsMargins(6,0,0,0);
    ui->buttonBox->clear();


    m_oCancelBtn = ui->buttonBox->addButton(_("Cancel"), QDialogButtonBox::RejectRole);
    m_oEnsureBtn = ui->buttonBox->addButton(_("OK"), QDialogButtonBox::AcceptRole);

    connect(m_oCancelBtn,&QPushButton::clicked,this,&FileFilterRules::slotscancel);
    connect(m_oEnsureBtn,&QPushButton::clicked,this,&FileFilterRules::slotsensure);

    ui->checkBoxHidden->setFixedHeight(21);
    ui->checkBoxRepleace->setFixedHeight(21);
    ui->checkBoxSymbolLink->setFixedHeight(21);
}

void FileFilterRules::slotChangeUIFontSize(int fontSize)
{
     int scaleRatio = qAbs(fontSize - 10.0);
     QFont font = ui->labelTitle->font();
     font.setPixelSize(16 + scaleRatio);
     font.setBold(true);
     ui->labelTitle->setFont(font);

     font.setPixelSize(14 + scaleRatio);
     font.setBold(false);

     ui->checkBoxHidden->setFont(font);
     ui->checkBoxSymbolLink->setFont(font);
     ui->checkBoxRepleace->setFont(font);

     m_oCancelBtn->setFont(font);
     m_oEnsureBtn->setFont(font);
}

void FileFilterRules::slotscancel()
{
    close();
}
void FileFilterRules::slotclose()
{
    close();
}
void FileFilterRules::slotsensure(){

    m_ishidden = Configuration::instance()->get("FilterFile","hidden",true).toBool();
    m_isbroken = Configuration::instance()->get("FilterFile","broken",true).toBool();
    m_isrepleace = Configuration::instance()->get("FilterFile","Repleace",true).toBool();

    close();
}

