#include "editpatientform.h"
#include "ui_editpatientform.h"
#include <QListView>
#include <QHBoxLayout>
#include <QToolButton>
#include <QButtonGroup>
#include "guimacros.h"
#include <qdebug.h>
#include "event/EventCenter.h"

EditPatientForm::EditPatientForm(QWidget* parent) :
	QWidget(parent),
	ui(new Ui::EditPatientForm)
{
//    this->layout()->setContentsMargins(5,5,5,5);
	ui->setupUi(this);
	//ui->lbl_Sex->setText(tr("Gender"));
	QHBoxLayout* sexlayout = new QHBoxLayout(ui->sexpanelwidget);
	sexlayout->setMargin(6);
	ADD_TOOL_SIZE_BTN_TO_LAYOUT(F, ":/icons/female_d.png", 30, sexlayout);
	ADD_TOOL_SIZE_BTN_TO_LAYOUT(M, ":/icons/male_d.png", 30, sexlayout);
	btnF->setText(tr("Female"));
	btnM->setText(tr("Male"));

    ui->hideBtn->setSizePolicy(QSizePolicy::Policy::Expanding,QSizePolicy::Policy::Preferred);
    ui->hideBtn->setObjectName("hideeditBtn");
    ui->hideBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    ui->hideBtn->setIcon(QIcon(":/icons/hidearrow.png"));
    ui->hideBtn->setIconSize({30,30});
	ui->hideBtn->setText(tr("   Hide Panel"));
    connect(ui->hideBtn,&QToolButton::clicked,[=](){
        emit hideBtnClicked();
    });

	//    btnFemale->setToolButtonStyle(Qt::ToolButtonIconOnly);
	//    btnMale->setToolButtonStyle(Qt::ToolButtonIconOnly);

	btnF->setObjectName("sexBtn");
	btnM->setObjectName("sexBtn");
	//btnF->setText("Female");
	//btnM->setText("Male");
	ui->sexpanelwidget->setEnabled(editEnable);
	btnF->setEnabled(editEnable);
	btnM->setEnabled(editEnable);
	btnF->setCheckable(true);
	btnM->setCheckable(true);
	QButtonGroup* group = new QButtonGroup(this);
	group->addButton(btnF);
	group->addButton(btnM);
	btnF->setChecked(true);
	btnFemale = btnF;
	btnMale = btnM;
	QHBoxLayout* layout = new QHBoxLayout(this->ui->editcmdWidget);

	ADD_TOOL_BTN(Cancel, ":/icons/close_circle.png");
	ADD_TOOL_BTN(Accpet, ":/icons/selected.png");
	btnCancel->setText(tr("Cancel"));
	btnAccpet->setText(tr("Accept"));
	btnCancel->setEnabled(editEnable);
	btnCancel->setToolButtonStyle(Qt::ToolButtonIconOnly);
	btnCancel->setIcon(QIcon(editEnable ? ":/icons/close_circle.png" : ":/icons/close_circle_d.png"));
	btnAccpet->setEnabled(editEnable);
	btnAccpet->setToolButtonStyle(Qt::ToolButtonIconOnly);
	btnAccpet->setIcon(QIcon(editEnable ? ":/icons/selected.png" : ":/icons/selected_d.png"));
	btnEditAccept = btnAccpet;
	btnEditCancel = btnCancel;
	connect(btnEditCancel, &QToolButton::clicked, [=]() {
		clearPatientInformation();
		this->setEditEnable(false);
		restorePatientInformation();
		emit editCancel();
		});
	connect(btnEditAccept, &QToolButton::clicked, [=]() {
		if (ui->tbx_ID->text().isEmpty())return;
		if (ui->tbx_Name->text().isEmpty())return;
		storePatientInformation();
		bool result = true;
		emit editAccept(getPatientInformation(), result);
		if (result) this->setEditEnable(false);
		});
	ui->tbx_Dob->setDisplayFormat("yyyy/MM/dd");

	connect(EventCenter::Default(), &EventCenter::ReloadLanguage, [=]() {

		ui->retranslateUi(this);
		btnF->setText(tr("Female"));
		btnM->setText(tr("Male"));
		btnCancel->setText(tr("Cancel"));
		btnAccpet->setText(tr("Accept"));
		});



}

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

void EditPatientForm::setPatientInformation(PatientInformation* information) {
	if (information)
	{
		ui->tbx_ID->setText(information->ID);
		ui->tbx_Dob->setDate(QDate::fromString(information->BirthDate, "yyyy-MM-dd"));
		ui->tbx_Name->setText(information->Name);
		ui->rtbx_Comment->setText(information->Comment);
		btnFemale->setChecked(information->Sex == "F");
		btnMale->setChecked(information->Sex != "F");
		currentPatientUID = information->PatientUID;
		AddDate = information->AddDate;
		storePatientInformation();
	}
}

void EditPatientForm::clearPatientInformation() {
	ui->tbx_ID->setText("");
	ui->tbx_Dob->setDate(QDate::currentDate());
	ui->tbx_Name->setText("");
	btnFemale->setChecked(true);
	btnMale->setChecked(false);
	ui->rtbx_Comment->setText("");
	currentPatientUID = "";
	AddDate = "";
}

void EditPatientForm::setEditEnable(bool enable) {
	ui->tbx_ID->setEnabled(enable);
	ui->tbx_Dob->setEnabled(enable);
	ui->tbx_Name->setEnabled(enable);
	ui->sexpanelwidget->setEnabled(enable);
	btnFemale->setEnabled(enable);
	btnFemale->setIcon(QIcon(enable ? ":/icons/female.png" : ":/icons/female_d.png"));
	btnMale->setEnabled(enable);
	btnMale->setIcon(QIcon(enable ? ":/icons/male.png" : ":/icons/male_d.png"));
	ui->rtbx_Comment->setEnabled(enable);
	btnEditAccept->setEnabled(enable);
	btnEditCancel->setEnabled(enable);
	btnEditCancel->setIcon(QIcon(enable ? ":/icons/close_circle.png" : ":/icons/close_circle_d.png"));
	btnEditAccept->setIcon(QIcon(enable ? ":/icons/selected.png" : ":/icons/selected_d.png"));
	editEnable = enable;
	//    ui->->setEnabled(enable);
}

void EditPatientForm::storePatientInformation() {
	store.PatientUID = currentPatientUID;
	store.AddDate = AddDate;
	store.ID = ui->tbx_ID->text();
	store.BirthDate = ui->tbx_Dob->date().toString("yyyy-MM-dd");
	store.Name = ui->tbx_Name->text();
	store.Sex = btnFemale->isChecked() ? "F" : "M";
	store.Comment = ui->rtbx_Comment->toPlainText();
	qDebug() << store.PatientUID << "," << store.ID << "," << store.BirthDate << "," << store.Name << "," << store.Sex;
}

void EditPatientForm::restorePatientInformation() {
	currentPatientUID = store.PatientUID;
	AddDate = store.AddDate;
	ui->tbx_ID->setText(store.ID);
	ui->tbx_Dob->setDate(QDate::fromString(store.BirthDate, "yyyy-MM-dd"));
	ui->tbx_Name->setText(store.Name);
	ui->rtbx_Comment->setText(store.Comment);
	btnFemale->setChecked(store.Sex == "F");
	btnMale->setChecked(store.Sex != "F");
	//    ui->cb_Sex->setCurrentText(store.Sex=="F"?"Female":(store.Name=="M"?"Male":"Other"));

}
