#include "UI.h"



enum WINDOWS_STATES_t {
	MEASURE_WINDOW,
	MAIN_WINDOW,
	SETTINGS_WINDOW,
};

extern volatile uint32_t aktualizovat_cas;
extern RTC_time_t prijaty_cas;
extern	RTC_time_t cas;
extern volatile uint8_t jas;
extern volatile uint32_t vystup;

uint32_t windows_state=MAIN_WINDOW;

void UI_process(void)	//User Interface process
{
	ADE7953_meas();
	if(aktualizovat_cas){DS3231_send_time(&prijaty_cas);aktualizovat_cas=0;}
	DS3231_read_time(&cas);

	uint32_t tlacitko=CAP1296_read_keys();
	LEDS_GreenRedGraph(active_power);
	switch(windows_state)
	{
	case MEASURE_WINDOW:
				if(tlacitko>3)
				{
					windows_state=MAIN_WINDOW;
					break;
				}
				show_idle_window();
				break;

	case MAIN_WINDOW:
				if(tlacitko==4) {windows_state=MEASURE_WINDOW;}								//pusteno tlacitko 1 = navrat na MEASURE
				if(tlacitko==5) {if(vystup){vystup=0;RELAY(0);}else {vystup=1;RELAY(1);}}	//pusteno tlacitko 2 = vypnout/zapnout vystup
				if(tlacitko==6) {windows_state=SETTINGS_WINDOW;}							//pusteno tlacitko 3 = nastaveni
				show_main_window(tlacitko); //zadny stisk nebo drzeni tlacitka 1, 2 nebo 3
				break;

	case SETTINGS_WINDOW:
				if(tlacitko==4) {windows_state=MAIN_WINDOW;}								//pusteno tlacitko 1 = navrat na MAIN
			//	if(tlacitko==5) {if(vystup){vystup=0;RELAY(0);}else {vystup=1;RELAY(1);}}	//pusteno tlacitko 2 = nastaveni wifi
			//	if(tlacitko==6) {windows_state=SETTINGS_WINDOW;}							//pusteno tlacitko 3 = nastaveni jasu
				show_settings_window(tlacitko); //zadny stisk nebo drzeni tlacitka 1, 2 nebo 3
				break;

	default: break;
	}

	update_display();
}

void show_idle_window(void)
{
	uint8_t textbuff[200];
	UG_FontSelect(&FONT_6X8_CZECH);
	UG_FillScreen(0);

	uint8_t ucinik_buff[10];
	if(ucinik==10000)moba_siprintf(ucinik_buff,"+1,000");
	else if(ucinik==-10000)moba_siprintf(ucinik_buff,"-1,000");
	else if(ucinik<0)
	{
		ucinik=abs(ucinik);
		moba_siprintf(ucinik_buff,"-0,%03d",(int32_t)ucinik/10);
	}
	else
	{
		moba_siprintf(ucinik_buff,"+0,%03d",(int32_t)ucinik/10);
	}


	moba_siprintf(textbuff,"Proud: %d.%03dA\r\nNapětí: %d.%1dV\r\nPříkon: %d.%03dW\r\nPF: %s\r\nSpotřeba:",(uint32_t) proud, proud_desetiny,(uint32_t) napeti, napeti_desetiny,(int32_t) active_power, active_power_desetiny,ucinik_buff, (int32_t) active_energy, active_energy_desetiny);
	UG_PutString(0,0,textbuff);
	UG_FontSelect(&FONT_10X16);
	moba_siprintf(textbuff,"%d.%03dWh\r\n",(int32_t) active_energy, active_energy_desetiny);
	UG_PutString(2,48,textbuff);
}

void show_main_window(uint32_t tlacitko)
{
	static uint32_t obr1=0;
	static uint32_t obr2=0;
	static uint32_t obr3=0;
	uint8_t textbuff[40];

	UG_FillScreen(0);

	UG_FontSelect(&FONT_5X12);
	moba_siprintf(textbuff,"%02d.%02d.%04d",cas.Day,cas.Month,cas.Year+2000);
	UG_PutString(0,0,textbuff);

	moba_siprintf(textbuff,"%02d:%02d:%02d",cas.Hour,cas.Minute,cas.Second);
	UG_PutString(80,0,textbuff);

	UG_FontSelect(&FONT_10X16);
	if(vystup)UG_PutString(26,13,"ZAPNUTO");
	else UG_PutString(26,13,"VYPNUTO");

	if(tlacitko==1) {obr1=1; obr2=0; obr3=0;}
	if(tlacitko==2) {obr1=0; obr2=1; obr3=0;}
	if(tlacitko==3) {obr1=0; obr2=0; obr3=1;}
	if(tlacitko>3) {obr1=0; obr2=0; obr3=0;}

	if(obr1) add_picture_measuring_i(); else add_picture_measuring();
	if(obr2) add_picture_output_i(); else add_picture_output();
	if(obr3) add_picture_settings_i(); else add_picture_settings();
}

void show_settings_window(uint32_t tlacitko)
{
	static uint32_t obr1=0;
	static uint32_t obr2=0;
	static uint32_t obr3=0;
	uint8_t textbuff[40];

	UG_FillScreen(0);

	UG_FontSelect(&FONT_5X12);
	moba_siprintf(textbuff,"%02d.%02d.%04d",cas.Day,cas.Month,cas.Year+2000);
	UG_PutString(0,0,textbuff);

	moba_siprintf(textbuff,"%02d:%02d:%02d",cas.Hour,cas.Minute,cas.Second);
	UG_PutString(80,0,textbuff);

	UG_FontSelect(&FONT_10X16);
	UG_PutString(16,13,"NASTAVENI");


	if(tlacitko==1) {obr1=1; obr2=0; obr3=0;}
	if(tlacitko==2) {obr1=0; obr2=1; obr3=0;}
	if(tlacitko==3) {obr1=0; obr2=0; obr3=1;}
	if(tlacitko>3) {obr1=0; obr2=0; obr3=0;}

	if(obr1) add_picture_backarrow_i(); else add_picture_backarrow();
	if(obr2) add_picture_wifi_i(); else add_picture_wifi();
	if(obr3) add_picture_brightness_i(); else add_picture_brightness();
}

void UI_TIMER_Init(void)
{
	#define UI_TIMER_PERIOD ((48000000 / 1000 ) - 1)

	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE);

	TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

	TIM_TimeBaseStructure.TIM_Prescaler = 80;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_Period = UI_TIMER_PERIOD;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

	TIM_TimeBaseInit(TIM15, &TIM_TimeBaseStructure);

	TIM_ITConfig(TIM15, TIM_IT_Update, ENABLE);

	NVIC_InitStructure.NVIC_IRQChannel = TIM15_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	TIM_Cmd(TIM15, ENABLE);
}

void TIM15_IRQHandler(void)
{
	 if (TIM_GetITStatus(TIM15, TIM_IT_Update) != RESET)
	{
		TIM_ClearITPendingBit(TIM15, TIM_IT_Update);
		UI_process();
	}
}
