#include "change_shift.h"
#include <common.h>
#include <dialogsetup.hpp>

extern SysConf g_sys_conf;
change_shift::change_shift(QDialog *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	this->setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);

	if (g_sys_conf.auto_shift == 1) {
		ui.radioButton_auto->setChecked(true);
	}
	else {
		ui.radioButton_auto->setChecked(false);
	}
	if (g_sys_conf.shift_byhand == 1) {
		ui.radioButton_byhand->setChecked(true);
	}
	else {
		ui.radioButton_byhand->setChecked(false);
	}
	if (g_sys_conf.timing_shift == 1) {
		ui.radioButton_time->setChecked(true);
	}
	else {
		ui.radioButton_time->setChecked(false);
	}
}

change_shift::~change_shift()
{
}

void change_shift::on_pushButton_apply_released()
{
	QTime timeA;
	QTime timeB;
	QTime timeC;
	if (ui.radioButton_auto->isChecked()) {	/// زرز‏²ط
		g_sys_conf.auto_shift = 1;
		g_sys_conf.timing_shift = 0;
		g_sys_conf.shift_byhand = 0;

		timeA.setHMS(ui.A_hour->minimum(), ui.A_minute->minimum(), 0);
		timeB.setHMS(ui.B_hour->minimum(), ui.B_minute->minimum(), 0);
		timeC.setHMS(ui.C_hour->minimum(), ui.C_minute->minimum(), 0);
		//emit sendMsgToDialogSetup(timeA, timeB, timeC);
		//this->close();
	}
	else if (ui.radioButton_time->isChecked()) {
		g_sys_conf.auto_shift = 0;
		g_sys_conf.timing_shift = 1;
		g_sys_conf.shift_byhand = 0;

		timeA.setHMS(ui.A_hour->text().toInt(), ui.A_minute->text().toInt(), 0);
		timeB.setHMS(ui.B_hour->text().toInt(), ui.B_minute->text().toInt(), 0);
		timeC.setHMS(ui.C_hour->text().toInt(), ui.C_minute->text().toInt(), 0);
		//emit sendMsgToDialogSetup(timeA, timeB, timeC);
		//this->close();
	}
	else if (ui.radioButton_byhand->isChecked()) {
		g_sys_conf.auto_shift = 0;
		g_sys_conf.timing_shift = 0;
		g_sys_conf.shift_byhand = 1;

		timeA.setHMS(ui.A_hour->minimum(), ui.A_minute->minimum(), 0);
		timeB.setHMS(ui.B_hour->minimum(), ui.B_minute->minimum(), 0);
		timeC.setHMS(ui.C_hour->minimum(), ui.C_minute->minimum(), 0);

	}
	emit sendMsgToDialogSetup(timeA, timeB, timeC);
	this->close();
	DialogSetup::write_config();
}

void change_shift::recMsgFromDialogSetup(QTime timeA, QTime timeB, QTime timeC)
{
	ui.A_hour->setValue(timeA.hour());
	ui.A_minute->setValue(timeA.minute());

	ui.B_hour->setValue(timeB.hour());
	ui.B_minute->setValue(timeB.minute());

	ui.C_hour->setValue(timeC.hour());
	ui.C_minute->setValue(timeC.minute());

	this->show();
}