#include "filefilterrules.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 <QPushButton>
#include <QDebug>
#include <ukuistylehelper.h>
using namespace kdk;

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

FileFilterRules::FileFilterRules(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FileFilterRules)
{
    QFont f;
    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->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(_("KylinBurner")/* + " - " + _("FileFilterRules")*/);
    connect(g_user_signal, &GlobalUserSignal::iconChange, [=](QIcon icon){
        setWindowIcon(icon);
    });
    f = ui->labelTitle->font();
    f.setPixelSize(16);
    f.setBold(true);
    ui->labelTitle->setFont(f);
    ui->labelTitle->setText(_("FileFilterRules"));

    f.setPixelSize(14);
    f.setBold(false);

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

    m_ishidden=false;
    m_isbroken=false;
    m_isrepleace=false;
    connect(ui->checkBoxHidden, SIGNAL(toggled(bool)), this, SIGNAL(hidden(bool)));
    connect(ui->checkBoxSymbolLink, SIGNAL(toggled(bool)), this, SIGNAL(broken(bool)));
    connect(ui->checkBoxRepleace, SIGNAL(toggled(bool)), this, SIGNAL(repleace(bool)));
    uiAdjust();
}

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)
{
    DataDirectory *node;
    if (CFG->get("FilterFile", "range", true).toBool()){
        if (CFG->get("FilterFile","hidden",true).toBool()){
            DataDirectory::setFilterHidden(true);
            ui->checkBoxHidden->setChecked(true);
        }else{
            DataDirectory::setFilterHidden(false);
            ui->checkBoxHidden->setChecked(false);
        }
        if(CFG->get("FilterFile","broken",true).toBool()){
            DataDirectory::setFilterBrokenSymbolLinks(true);
            ui->checkBoxSymbolLink->setChecked(true);
        }else{
            DataDirectory::setFilterBrokenSymbolLinks(false);
            ui->checkBoxSymbolLink->setChecked(false);
        }
        if( CFG->get("FilterFile","Repleace",true).toBool()){
            DataDirectory::setRepleaceSymbolLinks(true);
            ui->checkBoxRepleace->setChecked(true);
        }else{
            DataDirectory::setRepleaceSymbolLinks(false);
            ui->checkBoxRepleace->setChecked(false);
        }
    }
    else{
    }

    node = (DataDirectory *)data;
    if (!node) return;
    ui->checkBoxHidden->setChecked(node->filterHidden());
    ui->checkBoxSymbolLink->setChecked(node->filterBrokenSymbolLinks());
    ui->checkBoxRepleace->setChecked(node->repleaceSymbolLinks());
    node->execHidden(node->filterHidden());
    node->execBroken(node->filterBrokenSymbolLinks());
    node->execRepleace(node->repleaceSymbolLinks());
    //  }
}

void FileFilterRules::show()
{
    move(m_oPosition);
    if(m_bIsWayland) {
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    }
//    QDialog::show();
    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("#484848"));
        pal.setBrush(QPalette::Background,QColor("#1D1D1D"));
    else
        pal.setBrush(QPalette::Background, QColor("#FFFFFF"));
    setPalette(pal);
    QDialog::paintEvent(event);
}

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

/** ui调整
*/
void FileFilterRules::uiAdjust(){
    setFixedSize(404,345);
    ui->title->setFixedSize(408,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::AcceptRole);
    m_oEnsureBtn = ui->buttonBox->addButton(_("OK"), QDialogButtonBox::RejectRole);
    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::slotscancel()
{
    if (CFG->get("FilterFile", "range", true).toBool())
    {
        if (m_ishidden==false){
            DataDirectory::setFilterHidden(false);
            ui->checkBoxHidden->setChecked(false);
            CFG->set("FilterFile","hidden",false);
        }else if(m_ishidden==true){
            DataDirectory::setFilterHidden(true);
            ui->checkBoxHidden->setChecked(true);
            CFG->set("FilterFile","hidden",true);
        }
        if(m_isbroken==false){
            DataDirectory::setFilterBrokenSymbolLinks(false);
            ui->checkBoxSymbolLink->setChecked(false);
            CFG->set("FilterFile","broken",false);
        }else if(m_isbroken==true){
            DataDirectory::setFilterBrokenSymbolLinks(true);
            ui->checkBoxSymbolLink->setChecked(true);
            CFG->set("FilterFile","broken",true);
        }
        if(m_isrepleace==false/* CFG->get("FilterFile","Repleace",true).toBool()*/){
            DataDirectory::setRepleaceSymbolLinks(false);
            ui->checkBoxRepleace->setChecked(false);
            CFG->set("FilterFile","Repleace",false);

        }else if(m_isrepleace==true){
            DataDirectory::setRepleaceSymbolLinks(true);
            ui->checkBoxRepleace->setChecked(true);
            CFG->set("FilterFile","Repleace",true);
        }
    }
    else{
    }
    close();
}
void FileFilterRules::slotclose()
{
    if (CFG->get("FilterFile", "range", true).toBool())
    {
        if (m_ishidden==false){
            DataDirectory::setFilterHidden(false);
            ui->checkBoxHidden->setChecked(false);
            CFG->set("FilterFile","hidden",false);
        }else if(m_ishidden==true){
            DataDirectory::setFilterHidden(true);
            ui->checkBoxHidden->setChecked(true);
            CFG->set("FilterFile","hidden",true);
        }
        if(m_isbroken==false){
            DataDirectory::setFilterBrokenSymbolLinks(false);
            ui->checkBoxSymbolLink->setChecked(false);
            CFG->set("FilterFile","broken",false);
        }else if(m_isbroken==true){
            DataDirectory::setFilterBrokenSymbolLinks(true);
            ui->checkBoxSymbolLink->setChecked(true);
            CFG->set("FilterFile","broken",true);
        }
        if(m_isrepleace==false/* CFG->get("FilterFile","Repleace",true).toBool()*/){
            DataDirectory::setRepleaceSymbolLinks(false);
            ui->checkBoxRepleace->setChecked(false);
            CFG->set("FilterFile","Repleace",false);

        }else if(m_isrepleace==true){
            DataDirectory::setRepleaceSymbolLinks(true);
            ui->checkBoxRepleace->setChecked(true);
            CFG->set("FilterFile","Repleace",true);
        }
    }
    else{
    }
    close();
}
void FileFilterRules::slotsensure(){
    if(CFG->get("FilterFile","hidden",true).toBool()){
        m_ishidden=true;
    }
    else if(CFG->get("FilterFile","hidden",true).toBool()==false){
        m_ishidden=false;
    }
    if(CFG->get("FilterFile","broken",true).toBool()){
        m_isbroken=true;
    }else if(CFG->get("FilterFile","broken",true).toBool()==false){
        m_isbroken=false;
    }if(CFG->get("FilterFile","Repleace",true).toBool()){
        m_isrepleace=true;
    }else if(CFG->get("FilterFile","Repleace",true).toBool()==false){
        m_isrepleace=false;
    }
    close();
}

