#ifndef _TL_TIMA_TUI_H_
#define _TL_TIMA_TUI_H_

#include <stdbool.h>
#include "tlPinpadLayout.h"
#include "TuiLayout.h"

/* IDs for build-in image resources */
#define IDX_PINPAD_STAR                  0
#define IDX_PINPAD_BLANK                 1
#define IDX_PINPAD_0                     2
#define IDX_PINPAD_1                     3
#define IDX_PINPAD_2                     4
#define IDX_PINPAD_3                     5
#define IDX_PINPAD_4                     6
#define IDX_PINPAD_5                     7
#define IDX_PINPAD_6                     8
#define IDX_PINPAD_7                     9
#define IDX_PINPAD_8                     10
#define IDX_PINPAD_9                     11
#define IDX_CORRECT                      12
#define IDX_CANCEL                       13
#define IDX_VALID                        14
#define IDX_PINPAD_0_PRESSED             15
#define IDX_PINPAD_1_PRESSED             16
#define IDX_PINPAD_2_PRESSED             17
#define IDX_PINPAD_3_PRESSED             18
#define IDX_PINPAD_4_PRESSED             19
#define IDX_PINPAD_5_PRESSED             20
#define IDX_PINPAD_6_PRESSED             21
#define IDX_PINPAD_7_PRESSED             22
#define IDX_PINPAD_8_PRESSED             23
#define IDX_PINPAD_9_PRESSED             24
#define IDX_CORRECT_PRESSED              25
#define IDX_CANCEL_PRESSED               26
#define IDX_VALID_PRESSED                27
#define IDX_CLEAR_TEXT                   28
#define IDX_NORMAL_PINBOX                29
#define IDX_ERROR_PINBOX                 30
#define IDX_DISABLED_PINBOX              31
#define IDX_PINPAD_STAR_ERROR            32
#define IDX_SECURE_ICON                  33

#define IDX_INAPP_PIN_KEYPAD		      (IDX_SECURE_ICON + 1)
#define IDX_REGISTER_PIN_TOP              (IDX_INAPP_PIN_KEYPAD + 1)

#define IDX_PIN_SOFTKEY_BACK              (IDX_REGISTER_PIN_TOP + 1)
#define IDX_PIN_SOFTKEY_BACK_PRESSED      (IDX_PIN_SOFTKEY_BACK + 1)

#define IDX_PINPAD_PINPAD                 (IDX_PIN_SOFTKEY_BACK_PRESSED + 1)
#define IDX_PINPAD_PINPAD_BHR		      (IDX_PINPAD_PINPAD + 1)



/*
#define IDX_PINPAD_PINPAD                0
#define IDX_PINPAD_PINPAD_RTL            1
#define IDX_PINPAD_PINPAD_BHR		 42
#define IDX_PINPAD_PINPAD_BHR_RTL	 43
#define IDX_SOFTKEY_BACK                 44
#define IDX_SOFTKEY_BACK_RTL             45
#define IDX_SOFTKEY_BACK_PRESSED         46
#define IDX_SOFTKEY_BACK_PRESSED_RTL     47
#define IDX_SOFTKEY                      48
#define IDX_SOFTKEY_RTL			 49
#define IDX_SOFTKEY_BHR                  50
#define IDX_SOFTKEY_BHR_RTL              51
*/

#define IDX_DELTA_PRESSED     (IDX_PINPAD_0_PRESSED - IDX_PINPAD_0)

typedef struct coordinate {
	uint32_t x;
	uint32_t y;
} coordinate_t;

typedef struct {
	uint8_t *image;
	int image_len;
	uint8_t *info;
	int info_len;
	int x;
	int y;
} textinfo_t;

enum pin_box_type {
	TYPE_NORMAL_PINBOX = 0,
	TYPE_ERROR_PINBOX,
	TYPE_DISABLED_PINBOX
};

// types of resources loadable from normal world
enum loadable_res_type {
	RES_BACKGROUND = 0,
	RES_ACTION_BAR_TEXT,
	RES_SECURE_MODE_TEXT,
	RES_PROMPT_ABOVE_PINBOX,
	RES_PROMPT_BELOW_PINBOX,
	RES_CANCEL_BUTTON,
	RES_CANCEL_BUTTON_PRESSED,
	RES_NORMAL_PINBOX,
	RES_ERROR_PINBOX,
	RES_DISABLED_PINBOX,
	RES_CONFIRM,
	RES_END
};

typedef struct loadable_img {
	uint8_t *pointer;	// must point to somewhere in g_resource_buffer
	uint32_t len;		// the buffer length
	uint32_t width;		// image width
	uint32_t height;	// image height
	uint32_t x;		// x coordinate of the top left corner
	uint32_t y;		// y coordinate
	bool to_display;	// had this image already been displayed?
	bool new_prompt_covers_old_prompt;	// whether the new prompts can completely cover the old prompt?
} loadable_img_t;

typedef struct preference {
	uint8_t isRTL;
	uint8_t isBHR;
} preference_t;

typedef struct{
    sPinPadKey_t softKey;
    uint32_t id;
    uint8_t* eventResource;
    uint32_t eventResourceSize;
    uint8_t* originResource;
    uint32_t originResourceSize;
    uint8_t* extraResource;
    uint32_t extraResourceSize;
} sPinSoftKey;

#define NUM_IMAGE	50

#define BACKSPACE_LONG_PRESS	500

#define US_PIN_SIZE	6

#define FPPRELOAD_SECDISOFF	3.0
#define SEC_SCREEN_ON		0.2

#define IMAGE_BUFFER_SIZE       2 * 1024 * 1024
#define KEYPAD_SIZE             100

uint32_t setReleasedButtonImage(uint8_t value);

uint32_t loadPinpadImage(uint32_t type, uint8_t * buf, uint32_t len, uint32_t x_dp, uint32_t y_dp);
uint32_t removeAllPin(void);
uint32_t updatePinpadScreen(enum pin_box_type pinbox_type);
uint32_t launchPinpad(bool start_tui_session, enum pin_box_type pinbox_type);
void clearPinData(void);
void clearLoadableResource(void);
uint32_t setPinpadTimer(void);
bool isPinpadTimerExpired(void);

void clearPinBackgroundResource(void);
uint32_t setPinBackground(uint32_t imageSize, uint32_t imageWidth, uint32_t imageHeight);
bool isExsitPinBackground(void);
uint32_t savePinBackgroundResource(uint32_t index, uint8_t* bgData, uint32_t dataLen);
uint32_t setPinScreenView(uint32_t id, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint32_t eventImageSize, uint32_t originImageSize, uint32_t extraImageSize);
bool isExsitPinScreenView(void);
uint32_t savePinScreenViewResource(uint8_t* viewData, uint32_t dataLen);
uint32_t setNavigationGesture(uint8_t value);
void setPinpadImages(uint32_t type, uint8_t *buf);
uint32_t getTouchedKey(uint32_t x, uint32_t y, sPinPadKey_t *const touched_key);

#endif
