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

FrmSetting::FrmSetting(FileConfigDecode* fileConfig, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FrmSetting)
{
    ui->setupUi(this);
    this->fileConfig = fileConfig;
    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();
    serversetting->hide();

}

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

    ui->schRename->setChecked(fileConfig->getRename());
    ui->schAutoSetup->setChecked(fileConfig->getAutoSetup());
    ui->schTimeRename->setChecked(fileConfig->getTimeRename());
    ui->schEnableSsl->setChecked(fileConfig->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)
{
    fileConfig->setRename(checked);
    ui->labRenameOpen->setStyleSheet(checked?OPENQSS:CLOSEQSS);
    ui->labRenameClose->setStyleSheet(checked?CLOSEQSS:OPENQSS);
}

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

void FrmSetting::schTimeRename(bool checked)
{
    fileConfig->setTimeRename(checked);
    ui->labTimeRenameOpen->setStyleSheet(checked?OPENQSS:CLOSEQSS);
    ui->labTimeRenameClose->setStyleSheet(checked?CLOSEQSS:OPENQSS);
}

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

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



