/**
 * @file lv_zh_keyboard.h
 *
 */

#ifndef LV_ZH_KEYBOARD_H
#define LV_ZH_KEYBOARD_H

#ifdef __cplusplus
extern "C" {
#endif

/*********************
 *      INCLUDES
 *********************/
#include "../../../widgets/lv_btnmatrix.h"

#if LV_USE_ZH_KEYBOARD
/*Testing of dependencies*/
#if LV_USE_BTNMATRIX == 0
#error "lv_kb: lv_btnm is required. Enable it in lv_conf.h (LV_USE_BTNMATRIX  1) "
#endif

#if LV_USE_TEXTAREA == 0
#error "lv_kb: lv_ta is required. Enable it in lv_conf.h (LV_USE_TEXTAREA  1) "
#endif

/*********************
 *      DEFINES
 *********************/
#define LV_ZH_KEYBOARD_CTRL_BTN_FLAGS (LV_BTNMATRIX_CTRL_NO_REPEAT | LV_BTNMATRIX_CTRL_CLICK_TRIG | LV_BTNMATRIX_CTRL_CHECKED)

/**********************
 *      TYPEDEFS
 **********************/

/** Current keyboard mode.*/
enum {
    LV_ZH_KEYBOARD_MODE_TEXT_LOWER,
    LV_ZH_KEYBOARD_MODE_TEXT_UPPER,
    LV_ZH_KEYBOARD_MODE_SPECIAL,
    LV_ZH_KEYBOARD_MODE_NUMBER,
    LV_ZH_KEYBOARD_MODE_USER_1,
    LV_ZH_KEYBOARD_MODE_USER_2,
    LV_ZH_KEYBOARD_MODE_USER_3,
    LV_ZH_KEYBOARD_MODE_USER_4,
};
typedef uint8_t lv_zh_keyboard_mode_t;

/*Data of keyboard*/
typedef struct {
    lv_btnmatrix_t btnm;
    lv_obj_t * ta;              /*Pointer to the assigned text area*/
    lv_zh_keyboard_mode_t mode;    /*Key map type*/
    uint8_t popovers : 1;       /*Show button titles in popovers on press*/
} lv_zh_keyboard_t;

extern const lv_obj_class_t lv_zh_keyboard_class;

/**********************
 * GLOBAL PROTOTYPES
 **********************/

/**
 * Create a Keyboard object
 * @param parent pointer to an object, it will be the parent of the new keyboard
 * @param zh_font pointer to an font object.
 * @return pointer to the created keyboard
 */
lv_obj_t * lv_zh_keyboard_create(lv_obj_t * parent, const lv_font_t * zh_font);

/*=====================
 * Setter functions
 *====================*/

/**
 * Assign a Text Area to the Keyboard. The pressed characters will be put there.
 * @param kb pointer to a Keyboard object
 * @param ta pointer to a Text Area object to write there
 */
void lv_zh_keyboard_set_textarea(lv_obj_t * kb, lv_obj_t * ta);

/**
 * Set a new a mode (text or number map)
 * @param kb pointer to a Keyboard object
 * @param mode the mode from 'lv_keyboard_mode_t'
 */
void lv_zh_keyboard_set_mode(lv_obj_t * kb, lv_zh_keyboard_mode_t mode);

/**
 * Show the button title in a popover when pressed.
 * @param kb pointer to a Keyboard object
 * @param en whether "popovers" mode is enabled
 */
void lv_zh_keyboard_set_popovers(lv_obj_t * kb, bool en);

/**
 * Set a new map for the keyboard
 * @param kb pointer to a Keyboard object
 * @param mode keyboard map to alter 'lv_keyboard_mode_t'
 * @param map pointer to a string array to describe the map.
 *            See 'lv_btnmatrix_set_map()' for more info.
 */
void lv_zh_keyboard_set_map(lv_obj_t * kb, lv_zh_keyboard_mode_t mode, const char * map[],
                            const lv_btnmatrix_ctrl_t ctrl_map[]);

/*=====================
 * Getter functions
 *====================*/

/**
 * Assign a Text Area to the Keyboard. The pressed characters will be put there.
 * @param kb pointer to a Keyboard object
 * @return pointer to the assigned Text Area object
 */
lv_obj_t * lv_zh_keyboard_get_textarea(const lv_obj_t * kb);

/**
 * Set a new a mode (text or number map)
 * @param kb pointer to a Keyboard object
 * @return the current mode from 'lv_keyboard_mode_t'
 */
lv_zh_keyboard_mode_t lv_zh_keyboard_get_mode(const lv_obj_t * kb);

/**
 * Tell whether "popovers" mode is enabled or not.
 * @param kb pointer to a Keyboard object
 * @return true: "popovers" mode is enabled; false: disabled
 */
bool lv_zh_btnmatrix_get_popovers(const lv_obj_t * obj);

/**
 * Get the current map of a keyboard
 * @param kb pointer to a keyboard object
 * @return the current map
 */
static inline const char ** lv_zh_keyboard_get_map_array(const lv_obj_t * kb)
{
    return lv_btnmatrix_get_map(kb);
}

/**
 * Get the index of the lastly "activated" button by the user (pressed, released, focused etc)
 * Useful in the `event_cb` to get the text of the button, check if hidden etc.
 * @param obj       pointer to button matrix object
 * @return          index of the last released button (LV_BTNMATRIX_BTN_NONE: if unset)
 */
static inline uint16_t lv_zh_keyboard_get_selected_btn(const lv_obj_t * obj)
{
    return lv_btnmatrix_get_selected_btn(obj);
}

/**
 * Get the button's text
 * @param obj       pointer to button matrix object
 * @param btn_id    the index a button not counting new line characters.
 * @return          text of btn_index` button
 */
static inline const char * lv_zh_keyboard_get_btn_text(const lv_obj_t * obj, uint16_t btn_id)
{
    return lv_btnmatrix_get_btn_text(obj, btn_id);
}


/*=====================
 * Other functions
 *====================*/

/**
 * Default keyboard event to add characters to the Text area and change the map.
 * If a custom `event_cb` is added to the keyboard this function can be called from it to handle the
 * button clicks
 * @param kb pointer to a keyboard
 * @param event the triggering event
 */
void lv_zh_keyboard_def_event_cb(lv_event_t * e);

char * get_chinese(char * input_string);

/**********************
 *      MACROS
 **********************/

#endif  /*LV_USE_KEYBOARD*/

#ifdef __cplusplus
} /*extern "C"*/
#endif

#endif /*LV_KEYBOARD_H*/
