#include "frmsetting.h"
#include "ui_frmsetting.h"
#include <QDebug>

FrmSetting::FrmSetting(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FrmSetting)
{
    ui->setupUi(this);
    initForm();
    initWidget();
}

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

void FrmSetting::initForm()
{
    ui->schRename->setBgColorOn(QColor("#409EFF"));
    ui->schRename->enableText(false);
    ui->schRename->setAnimation(true);


    ui->schAutoSetup->setBgColorOn(QColor("#409EFF"));
    ui->schAutoSetup->enableText(false);
    ui->schAutoSetup->setAnimation(true);


    ui->schTimeRename->setBgColorOn(QColor("#409EFF"));
    ui->schTimeRename->enableText(false);
    ui->schTimeRename->setAnimation(true);


    ui->schEnableSsl->setBgColorOn(QColor("#409EFF"));
    ui->schEnableSsl->enableText(false);
    ui->schEnableSsl->setAnimation(true);

    ui->btnServer->setCursor(Qt::PointingHandCursor);
    ui->schRename->setCursor(Qt::PointingHandCursor);


    serversetting = new ServerSetting();
    connect(serversetting, &ServerSetting::signal_updateServerConfig, this, &FrmSetting::slot_updateServerConfig);
    serversetting->hide();

    QString address = FileConfigDecode::getInstance()->getAddress();
    QString username = FileConfigDecode::getInstance()->getUserName();
    QString password = FileConfigDecode::getInstance()->getPassword();

    serversetting->setAddress(address);
    serversetting->setPassword(password);
    serversetting->setUserName(username);
}

void FrmSetting::initWidget()
{
    if (FileConfigDecode::getInstance()->getRename()) {
        ui->labRenameOpen->setStyleSheet(OPENQSS);
        ui->labRenameClose->setStyleSheet(CLOSEQSS);
    }
    else {
        ui->labRenameOpen->setStyleSheet(CLOSEQSS);
        ui->labRenameClose->setStyleSheet(OPENQSS);
    }
    if (FileConfigDecode::getInstance()->getAutoSetup()) {
        ui->labAutoSetupOpen->setStyleSheet(OPENQSS);
        ui->labAutoSetupClose->setStyleSheet(CLOSEQSS);
    }
    else {
        ui->labAutoSetupOpen->setStyleSheet(CLOSEQSS);
        ui->labAutoSetupClose->setStyleSheet(OPENQSS);
    }
    if (FileConfigDecode::getInstance()->getTimeRename()) {
        ui->labTimeRenameOpen->setStyleSheet(OPENQSS);
        ui->labTimeRenameClose->setStyleSheet(CLOSEQSS);
    }
    else {
        ui->labTimeRenameOpen->setStyleSheet(CLOSEQSS);
        ui->labTimeRenameClose->setStyleSheet(OPENQSS);
    }
    if (FileConfigDecode::getInstance()->getAutoCopy()) {
        ui->labEnableSsl->setStyleSheet(OPENQSS);
        ui->labEnableSsl->setStyleSheet(CLOSEQSS);
    }
    else {
        ui->labEnableSsl->setStyleSheet(CLOSEQSS);
        ui->labEnableSsl->setStyleSheet(OPENQSS);
    }

    ui->schRename->setChecked(FileConfigDecode::getInstance()->getRename());
    ui->schAutoSetup->setChecked(FileConfigDecode::getInstance()->getAutoSetup());
    ui->schTimeRename->setChecked(FileConfigDecode::getInstance()->getTimeRename());
    ui->schEnableSsl->setChecked(FileConfigDecode::getInstance()->getAutoCopy());

    connect(ui->schRename, &SwitchButton::checkedChanged, this, &FrmSetting::schRenameSlot);
    connect(ui->schAutoSetup, &SwitchButton::checkedChanged, this, &FrmSetting::schAutoSetup);
    connect(ui->schTimeRename, &SwitchButton::checkedChanged, this, &FrmSetting::schTimeRename);
    connect(ui->schEnableSsl, &SwitchButton::checkedChanged, this, &FrmSetting::schEnableSsl);
    connect(ui->btnServer, &QPushButton::clicked, serversetting, &ServerSetting::show);

}

void FrmSetting::schRenameSlot(bool checked)
{
    FileConfigDecode::getInstance()->setRename(checked);
    ui->labRenameOpen->setStyleSheet(checked?OPENQSS:CLOSEQSS);
    ui->labRenameClose->setStyleSheet(checked?CLOSEQSS:OPENQSS);
}

void FrmSetting::schAutoSetup(bool checked)
{
    FileConfigDecode::getInstance()->setAutoSetup(checked);
    ui->labAutoSetupOpen->setStyleSheet(checked?OPENQSS:CLOSEQSS);
    ui->labAutoSetupClose->setStyleSheet(checked?CLOSEQSS:OPENQSS);
}

void FrmSetting::schTimeRename(bool checked)
{
    if (ui->schRename->getChecked())
        FileConfigDecode::getInstance()->setTimeRename(checked, 1);
    else
        FileConfigDecode::getInstance()->setTimeRename(checked, 0);
    ui->labTimeRenameOpen->setStyleSheet(checked?OPENQSS:CLOSEQSS);
    ui->labTimeRenameClose->setStyleSheet(checked?CLOSEQSS:OPENQSS);
}

void FrmSetting::schEnableSsl(bool checked)
{
    FileConfigDecode::getInstance()->setAutoCopy(checked);
    ui->labEnableSslOpen->setStyleSheet(checked?OPENQSS:CLOSEQSS);
    ui->labEnableSslClose->setStyleSheet(checked?CLOSEQSS:OPENQSS);

    TCHttpService::getInstance()->setSsl(checked);

}

void FrmSetting::slot_updateServerConfig(QString address, QString username, QString password)
{
    FileConfigDecode::getInstance()->setAddress(address);
    FileConfigDecode::getInstance()->setUserName(username);
    FileConfigDecode::getInstance()->setPassword(password);
}



