#include "Vendor_Interface.h"
#include "TZ_Vendor_debug_tl.h"
#include "tl_tui_bc_error_msg.h"
#include "TuiButton.h"
#include "TuiTextBox.h"
#include "TuiControl.h"

Control* initButton(Control* control, uint32_t id, Location loc, uint32_t width, uint32_t height, 
		uint8_t* pressImg, uint32_t pressImgLen, uint8_t* relImg, uint32_t relImgLen, uint8_t* disableImg, uint32_t disableImgLen, uint8_t focused) {
	control->id = id;
	control->location = loc;
	control->width = width;
	control->height = height;
	control->eventResource = pressImg;
	control->eventResourceSize = pressImgLen;
	control->originResource = relImg;
	control->originResourceSize = relImgLen;
	control->extraResource = disableImg;
	control->extraResourceSize = disableImgLen;
	control->focused = focused;
	control->state = ENABLE_CONTROL_STATE;
	control->changeState = NONE_TUI_STATE;
	control->charValue = 0;
	control->childCount = 0;
	control->imageMargin.x1 = 0;
	control->imageMargin.y1 = 0;
	control->isDrawingWhenAdded = 1;
	control->isTouchable = 1;

	return control;
}

void addButton(Control *control, EXEC_FUNC func) {
	control->kindOfControl = BUTTON_CONTROL_TYPE;
	if (func == NULL) {
		control->execute = &executeButton;
	} else {
		control->execute = func;
	}
	addControl(control);
	if (control->isDrawingWhenAdded == 1) {
		drawButton(control);
	}
	return;
}

// NONE_CONTROL_STATE, ENABLE_CONTROL_STATE, FOCUSED_CONTROL_STATE, PRESSED_CONTROL_STATE, RELEASED_CONTROL_STATE, DISABLE_CONTROL_STATE
ResponseControl executeButton(Control *control) { // exceute then draw.
	ResponseControl rc;
	initResponseControl(&rc);

	TTY_LOG("executeButton!!");
	switch (control->state) {
		case PRESSED_CONTROL_STATE:
			break;
		case RELEASED_CONTROL_STATE:
			break;
		case NONE_CONTROL_STATE:
			break;
		default:
			break;
	}
	drawButton(control);

	return rc;
}

void drawButton(Control* control) {
	switch (control->state) {
		case PRESSED_CONTROL_STATE:
			if (control->charValue != 0) {
				DBG_LOG("Pressed Keypad : %c", control->charValue);
			}
			if (control->eventResource != NULL) {
				drawImage(control->location.x1 + control->imageMargin.x1, control->location.y1 + control->imageMargin.y1, control->eventResource, control->eventResourceSize);
			}
			break;
		case RELEASED_CONTROL_STATE:
		case ENABLE_CONTROL_STATE:
			if (control->charValue != 0) {
				DBG_LOG("Released Keypad : %c", control->charValue);
			}
			if (control->originResource != NULL) {
				drawImage(control->location.x1 + control->imageMargin.x1, control->location.y1 + control->imageMargin.y1, control->originResource, control->originResourceSize);
			}
			control->state = ENABLE_CONTROL_STATE;
			break;
		case DISABLE_CONTROL_STATE:
			if (control->extraResource != NULL) {
				drawImage(control->location.x1 + control->imageMargin.x1, control->location.y1 + control->imageMargin.y1, control->extraResource, control->extraResourceSize);
			} else if(control->originResource != NULL) {
				drawImage(control->location.x1 + control->imageMargin.x1, control->location.y1 + control->imageMargin.y1, control->originResource, control->originResourceSize);
			}
			control->state = DISABLE_CONTROL_STATE;
			break;
		default:
			TTY_LOG("%s drawButton unknown state! : %d", LOG_TAG, control->state);
			return;
	}
	return;
}