#ifndef __TUI_CONTROL_H__
#define __TUI_CONTROL_H__

#include "stdbool.h"
#include "stdint.h"
#include "TuiTouch.h"
#include "TuiState.h"
#ifndef NULL
#define NULL ((void*)0)
#endif

#define ID_CONTROL_KEYPAD_LAYOUT						9000
#define ID_CONTROL_KEYPAD_BACK_SPACE					9050
#define ID_CONTROL_BUTTON_RESTORE_DELETE_WORD			9051
#define ID_CONTROL_BUTTON_SOFTKEY_CANCEL				9100
#define ID_CONTROL_BUTTON_TITLEBAR_CANCEL				9101
#define ID_CONTROL_BUTTON_RESTORE_NEXT					9110
#define ID_CONTROL_BUTTON_RESTORE_ENTER					9111
#define ID_CONTROL_BUTTON_RESTORE_TEXTLINK				9112

#define ID_CONTROL_BUTTON_BACKUP_OK						9120
#define ID_CONTROL_BUTTON_CONFIRM_OK					9130
#define ID_CONTROL_BUTTON_POPUP_OK						9140
#define ID_CONTROl_BUTTON_POPUP_CANCEL					9141
#define ID_CONTROL_BUTTON_MNEMONIC_PHRASE_PREVIOUS		9142
#define ID_CONTROL_BUTTON_MNEMONIC_PHRASE_NEXT			9143
#define ID_CONTROL_IMAGE_VIEW_MNEMONIC_PHRASE_SCREENING 9223
#define ID_CONTROL_IMAGE_VIEW_MNEMONIC_PHRASE_NUMBER_SCREENING  9224
#define ID_CONTROL_IMAGE_VIEW_RESTORE_WRONG_INPUT		9210
//#define ID_CONTROL_IMAGE_VIEW_INVALID_COUNT_MNEMONIC		9211
//#define ID_CONTROL_IMAGE_VIEW_FAILED_CHECKSUM_MNEMONIC	9212
#define ID_CONTROL_IMAGE_VIEW_BACKUP_QUIZ_FIRST_MESSAGE			9213
#define ID_CONTROL_IMAGE_VIEW_BACKUP_QUIZ_SECOND_MESSAGE		9214
#define ID_CONTROL_IMAGE_VIEW_BACKUP_QUIZ_THIRD_MESSAGE			9215
#define ID_CONTROL_IMAGE_VIEW_BACKUP_QUIZ_FIRST_WRONG_INPUT		9216
#define ID_CONTROL_IMAGE_VIEW_BACKUP_QUIZ_SECOND_WRONG_INPUT	9217
#define ID_CONTROL_IMAGE_VIEW_BACKUP_QUIZ_THIRD_WRONG_INPUT		9218

#define ID_CONTROL_IMAGE_CHECKBOX_ON	    	9220
#define ID_CONTROL_IMAGE_CHECKBOX_OFF	    	9221
#define ID_CONTROL_IMAGE_WRITE_IT_DOWN			9222
#define ID_CONTROL_IMAGE_CHECKBOX_SCREENING 	9225

#define TRANSACTION_SO_BUFFER_SIZE	2048

#if !defined(CONFIG_MT6768)
#define TRUE 1
#define FALSE 0
#endif 

#define MAX_CONTROL 100

typedef enum EControl
{
	NONE_CONTROL_TYPE, 
	TEXT_EDIT_BOX_CONTROL_TYPE, 
	TEXT_VIEW_BOX_CONTROL_TYPE, 
	ICON_CONTROL_TYPE, 
	SOFT_KEY_CONTROL_TYPE, 
	BUTTON_CONTROL_TYPE, 
	WINDOW_CONTROL_TYPE, 
	KEYPAD_CONTROL_TYPE, 
	IMAGE_VIEW_CONTROL_TYPE
} EControl;

//
typedef enum EControlState
{
	NONE_CONTROL_STATE,
	ENABLE_CONTROL_STATE,
	PRESSED_CONTROL_STATE,
	RELEASED_CONTROL_STATE,
	DISABLE_CONTROL_STATE,
	INVISIBLE_CONTROL_STATE,
	UPDATED_CONTROL_STATE,
} EControlState;

typedef enum EContentAlign
{
	LEFT_CONTENT_ALIGN, CENTER_CONTENT_ALIGN, RIGHT_CONTENT_ALIGN
} EContentAlign;

typedef enum EContentVerticalAlign
{
	VERTICAL_TOP_CONTENT_ALIGN,
	VERTICAL_CENTER_CONTENT_ALIGN
} EContentVerticalAlign;

typedef struct Location {
	uint32_t x1, x2;
	uint32_t y1, y2;
	uint32_t dx1, dx2;
	uint32_t dy1, dy2;
} Location;

typedef struct ScreenInfo {
	uint32_t width;
	uint32_t height;
	uint32_t softkeyHeight;
} ScreenInfo;

typedef struct Point {
	uint32_t x, y;
}Point;

typedef enum ETEXTINPUTSIZE
{
	MNEMONIC_MAX_SIZE = 8,
	TEXT_MAX_SIZE = 255
} ETextInputSize;

typedef enum EFONTTYPE
{
	FONT_TYPE_01,
	FONT_TYPE_02,
	FONT_TYPE_03,
	FONT_TYPE_04,
	FONT_TYPE_05,
	FONT_TYPE_06,
	FONT_TYPE_07,
	FONT_TYPE_08,
	FONT_TYPE_09
} EFontType;

typedef struct TEXTINFO
{
	uint8_t* text;
	EFontType font;
} TextInfo;

typedef struct CONTROL {
	EControl kindOfControl;
	Location location;
	Location imageMargin;
	uint32_t id;
	uint8_t* eventResource;
	uint32_t eventResourceSize;
	uint8_t* originResource;
	uint32_t originResourceSize;
	uint8_t* extraResource;
	uint32_t extraResourceSize;
	uint32_t width;
	uint32_t height;
	uint8_t focused;
	EControlState state;
	EContentAlign align;
	EContentVerticalAlign verticalAlign;
	uint8_t charValue;
	TextInfo textInfo;
	TuiState changeState;
	uint32_t childCount;
	int32_t isDrawingWhenAdded;
	int32_t isTouchable;
	struct RESPONSE_CONTROL(*execute)(struct CONTROL* );
} Control;

typedef struct RESPONSE_CONTROL{
	uint8_t charValue;
	TextInfo textInfo;
	TuiState changeState;
}ResponseControl;

typedef struct NODE {
	struct NODE* pre;
	struct NODE* next;
	Control control;
} Node;

typedef struct CONTROLGROUP {
	uint32_t startId;
	uint32_t groupSize;
} ControlGroup;

typedef struct RESPONSE_CONTROL(*EXEC_FUNC)(struct CONTROL*);

#ifdef __cplusplus
extern "C" {
#endif
	void setControlNode(Node* controlNode);
	bool addControl(Control *control);
	uint32_t makeNode(uint32_t nodeNumber, Control *control);
	ResponseControl executeControl(TouchEvent touchEvent);
	ResponseControl noneExecution(Control *control);
	Node* pickingFocusedControl(EControl controlType);
	Node* pickingControlById(uint32_t controlId);
	Node* pickingControlByState(EControlState state);
	void releaseAllControl(void);
	void clearNode(Node* node);
	void deleteControl(uint32_t id);
	void deleteAllControl(void);
	void setFocus(Control* control, uint32_t focus);
	void initResponseControl(ResponseControl* rc_out);
	void drawControlNode(void);

#ifdef __cplusplus
}
#endif

#endif
