#include "alarmwidget.h"
#include "ui_alarmwidget.h"

AlarmWidget::AlarmWidget(std::shared_ptr<AlarmTime> alarm, QWidget *parent) :
	QWidget(parent),
	alarm_(alarm),
	ui(new Ui::AlarmWidget)
{
	ui->setupUi(this);

	ui->dateTimeEdit->setDateTime(alarm->getDateTime());

	if(alarm_->getRepeat() == AlarmTime::REPEAT_NEVER)
	{
		ui->radioButton->setEnabled(false);
		ui->radioButton_2->setEnabled(false);
		ui->radioButton_3->setEnabled(false);
		ui->radioButton_4->setEnabled(false);
	}
	else
	{
		ui->checkBox->setChecked(true);
		ui->radioButton->setEnabled(true);
		ui->radioButton_2->setEnabled(true);
		ui->radioButton_3->setEnabled(true);
		ui->radioButton_4->setEnabled(true);
	}

	if(alarm_->getRepeat() == AlarmTime::REPEAT_DAILY)
		ui->radioButton->setChecked(true);
	else if(alarm_->getRepeat() == AlarmTime::REPEAT_WEEKLY)
		ui->radioButton_2->setChecked(true);
	else if(alarm_->getRepeat() == AlarmTime::REPEAT_MONTHLY)
		ui->radioButton_3->setChecked(true);
	else if(alarm_->getRepeat() == AlarmTime::REPEAT_YEARLY)
		ui->radioButton_4->setChecked(true);

	connect(ui->checkBox, &QCheckBox::stateChanged, this, &AlarmWidget::toggleRepeating);
	connect(ui->radioButton, &QRadioButton::clicked, this, &AlarmWidget::setRepeatingType);
	connect(ui->radioButton_2, &QRadioButton::clicked, this, &AlarmWidget::setRepeatingType);
	connect(ui->radioButton_3, &QRadioButton::clicked, this, &AlarmWidget::setRepeatingType);
	connect(ui->radioButton_4, &QRadioButton::clicked, this, &AlarmWidget::setRepeatingType);
	connect(ui->dateTimeEdit, &QDateTimeEdit::dateTimeChanged, alarm.get(), &AlarmTime::changeTime);
}

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

void AlarmWidget::setRepeatingType()
{
	if(ui->radioButton->isChecked())
		alarm_->setRepeat(AlarmTime::REPEAT_DAILY);
	if(ui->radioButton_2->isChecked())
		alarm_->setRepeat(AlarmTime::REPEAT_WEEKLY);
	if(ui->radioButton_3->isChecked())
		alarm_->setRepeat(AlarmTime::REPEAT_MONTHLY);
	if(ui->radioButton_4->isChecked())
		alarm_->setRepeat(AlarmTime::REPEAT_YEARLY);
}

void AlarmWidget::toggleRepeating(int state)
{
	if(state)
	{
		ui->radioButton->setEnabled(true);
		ui->radioButton_2->setEnabled(true);
		ui->radioButton_3->setEnabled(true);
		ui->radioButton_4->setEnabled(true);
		setRepeatingType();
	}
	else
	{
		alarm_->setRepeat(AlarmTime::REPEAT_NEVER);
		ui->radioButton->setEnabled(false);
		ui->radioButton_2->setEnabled(false);
		ui->radioButton_3->setEnabled(false);
		ui->radioButton_4->setEnabled(false);
	}
}

