#include "Comunication.h"

#include "usart.h"
#include "rtc.h"

#include "Heure.h"
#include "Status.h"
#include "Fille.h"

extern Actionneur Osmolateur;
extern Actionneur Brassage1;
extern Actionneur Brassage2;
extern Actionneur Chauffage;
extern Actionneur Ecumeur;
extern Actionneur Remonter;
extern Actionneur LumiereDec;
extern enum Etat BrassageGeneral;
extern uint8_t gl_powerVentil1,gl_powerVentil2;
extern uint8_t gl_Notification,gl_Erreur;
extern uint8_t IntervalBrassage;

uint8_t parse(uint8_t* mes) {

	switch (mes[0] & MASKMES) {
	case (STATUS):
		Status();
		break;
	case (READACTI):
		ReadAction();
		break;
	case (WRITACTI):
		WriteAction(mes);
		break;
	case (READVENT):
		ReadVent();
		break;
	case (WRITVENT):
		WriteVent(mes);
		break;
	case (DELAYBRA):
		DelayBra(mes);
		break;
	case (SYNCRTC):
		SyncRTC(mes);
		break;
	default:
		return -1; //TODO Erreur de cas (voir si reset du micro)

	}
	return 0;
}

void Status() {
	uint8_t mes[8];
	mes[0] = STATUS;
	mes[1] = readTempAir();
	mes[2] = readTempEau();
	mes[3] = readPH();
	mes[4] = readSalinity();
	mes[5] = readNiveau();
	mes[6] = gl_Notification;
	mes[7] = gl_Erreur;
	HAL_UART_Transmit(&huart1, mes, 8, 500);
}

void ReadAction() {
	uint8_t mesRet[8];
	HAL_StatusTypeDef res;
	uint8_t val[2];
	res = readActitoneur(val);
	mesRet[0] = READACTI | res;
	mesRet[1] = val[0];
	mesRet[2] = val[1];
	HAL_UART_Transmit(&huart1, mesRet, 8, 500);
}

void WriteAction(uint8_t* mes) {
	//Todo (Gestion d'erreur) faire le res de mes[0]
	HAL_StatusTypeDef res;

	if (mes[1] & MASKNOUR)
		res = fc_Nourrisage(mes[3]);

	if (mes[1] & MASKOSMO)
		Osmolateur.On = mes[3];

	if (mes[1] & MASKBRA1) {
		Brassage1.On = mes[3];
		res = fc_Brassage();
	}

	if (mes[1] & MASKBRA2) {
		Brassage2.On = mes[3];
		res = fc_Brassage();
	}

	if (mes[1] & MASKBRAG) {
		BrassageGeneral = mes[3];
		res = fc_Brassage();
	}

	if (mes[1] & MASKCHAU)
		res = fc_Chauffage(mes[3]);

	if (mes[1] & MASKECUM)
		res = fc_Ecumeur(mes[3]);

	if (mes[1] & MASKREME)
		res = fc_Remonter(mes[3]);

	if (mes[2] & MASKLUMI)
		res = fc_Lumiere(mes[3]);

	if (mes[2] & MASKOSMM)
		res = fc_OsmolateurManu(mes[3]);

	uint8_t mesRet[3];
	uint8_t val[2];

	res = readActitoneur(val);
	mesRet[1] = val[0];
	mesRet[2] = val[1];

	mesRet[0] = WRITACTI | res;
	HAL_UART_Transmit(&huart1, mesRet, 8, 500);
}

void ReadVent() {
	uint8_t mes[3];
	HAL_StatusTypeDef res = HAL_OK;
	mes[0] = READVENT | res;
	mes[1]= gl_powerVentil1;
	mes[2]= gl_powerVentil2;
	HAL_UART_Transmit(&huart1, mes, 8, 500);
}

void WriteVent(uint8_t* mes) {
	HAL_StatusTypeDef res;
	gl_powerVentil1 = mes[1];
	gl_powerVentil2 = mes[2];
	res = fc_Ventilation();
	mes[0]=WRITVENT | res;
	HAL_UART_Transmit(&huart1, mes, 8, 500);
}

void DelayBra(uint8_t* mes) {
	IntervalBrassage = mes[1];
	fc_Brassage();
	uint8_t mesRet[1];
	mesRet[0] = DELAYBRA;
	HAL_UART_Transmit(&huart1, mesRet, 8, 500);
}

void SyncRTC(uint8_t* mes) {
	/*
	Date date;
	uint8_t mesRet[1];
	date.annee = mes[1];
	date.mois = mes[2];
	date.jour = mes[3];
	date.joursemaine = mes[4];
	date.heure = mes[5];
	date.minute = mes[6];
	date.second = mes[7];
	HAL_StatusTypeDef res = writeDate(&date);
	mesRet[0] = SYNCRTC | res;
	HAL_UART_Transmit(&huart1, mesRet, 8, 500);
	*/
	//TODO
}
