#include <stdio.h>
#include "gui_engine.h"
#include "device_font.h"
#include "util.h"
#include "ui_wingetinfo.inc"
#include "app_gui.h"

//#define CHK_LEN_DEBUG
#ifdef CHK_LEN_DEBUG
#define DEBUG_MSG(fmt, arg...) printf(fmt, ##arg)
#else
#define DEBUG_MSG(fmt, arg...) ((void) 0)
#endif

#define CONTROL_STATE_MAX           (4)

static int menuID = 0;
static int controlID = 0;
static int state = 0;

static char *stateArray[] = 
{
    "Normal",
    "Focused",
    "Disabled",
    "Clicked",
};

static int APP_Check_StringIDList(UINT8 ucFontSize, FixWidth_t eFixCharWidth, String_id_t *stringIDList, int totalStr, RECT textPos)
{
    int i = 0;
    UINT8 *pStr = NULL;
    int languageID = 0;
    int ret = 0;
    UINT16 width = 0, height = 0;

    DEBUG_MSG("%s-%s, total_count=%d\n", allAppletes[menuID].pMenuName, allAppletes[menuID].pWidgetName[controlID], totalStr);

    for (languageID = 0; languageID < TV_UI_LANG_NUM; languageID++)
    {
        Font_setCurLang(languageID);
        for (i = 0; i < totalStr; i++)
        {
            if (STRING_ID_INVALID == stringIDList[i] || GEL_GetStr(stringIDList[i], &pStr) != GUI_SUCCESS)
            {
                continue;
            }

            Font_getStrWH(pStr, ucFontSize, eFixCharWidth, &width, &height);
            if (width > textPos.width)
            {
                printf("%s - %s @ %s - %s - %s, string.width=%d, rect.width=%d\n", allLangName[languageID], allStrName[stringIDList[i]], 
                    allAppletes[menuID].pMenuName, allAppletes[menuID].pWidgetName[controlID], stateArray[state], width, textPos.width);
                ret = 1;
            }
        }
    }

    return ret;
}

static int APP_Check_Combox(WinControl_t *combox)
{
    combo_t *ctrl = NULL;
    UINT8 ucFontSize = 0;
    FixWidth_t eFixCharWidth = 0;
    int ret = 0;

    if (NULL == combox || (ctrl = combox->param) == NULL)
    {
        return 0;
    }

    if (NULL == ctrl->string_id)
    {
        return 0;
    }

    eFixCharWidth = ctrl->bFixCharWidth > 0 ? FIXWIDTH_ALLCHARS : 0;
    for (state = 0; state < CONTROL_STATE_MAX; state++)
    {
        ucFontSize = GUIENG_GetFontStyle(combox, STATE_NORMAL << state);
        ret = APP_Check_StringIDList(ucFontSize, eFixCharWidth, ctrl->string_id, ctrl->total_count, ctrl->text_pos);
    }

    return ret;
}

static int APP_Check_Listview(WinControl_t *listview)
{
    listview_t *ctrl = NULL;
    UINT8 ucFontSize = 0;
    FixWidth_t eFixCharWidth = 0;
    int ret = 0;

    if (NULL == listview || (ctrl = listview->param) == NULL)
    {
        return 0;
    }

    if (NULL == ctrl->string_id)
    {
        return 0;
    }

    eFixCharWidth = ctrl->bFixCharWidth > 0 ? FIXWIDTH_ALLCHARS : 0;
    for (state = 0; state < CONTROL_STATE_MAX; state++)
    {
        ucFontSize = GUIENG_GetFontStyle(listview, STATE_NORMAL << state);
        ret = APP_Check_StringIDList(ucFontSize, eFixCharWidth, ctrl->string_id, ctrl->total_count, ctrl->text_pos);
    }

    return ret;
}


static int APP_Check_ButtonGroup(WinControl_t *buttonGroup)
{
    UINT8 ucFontSize = 0;
    FixWidth_t eFixCharWidth = 0;
    int ret = 0;

    if (NULL == buttonGroup || (buttonGroup->param) == NULL)
    {
        return 0;
    }

    if (GET_WIDGET_STYLE(buttonGroup) & S_BTN_TEXT)
    {
        btng_text_t *ctrl = buttonGroup->param;
        if (NULL == ctrl->string_id)
        {

            return 0;
        }

        eFixCharWidth = ctrl->bFixCharWidth > 0 ? FIXWIDTH_ALLCHARS : 0;
        for (state = 0; state < CONTROL_STATE_MAX; state++)
        {
            ucFontSize = GUIENG_GetFontStyle(buttonGroup, STATE_NORMAL << state);
            ret = APP_Check_StringIDList(ucFontSize, eFixCharWidth, ctrl->string_id, ctrl->total_count, ctrl->text_pos);
        }
    }
    else if (GET_WIDGET_STYLE(buttonGroup) & S_BTN_ICON)
    {
        
    }
    else if (GET_WIDGET_STYLE(buttonGroup) & S_BTN_MIX)
    {
        btng_mix_t *ctrl = buttonGroup->param;
        if (NULL == ctrl->string_id)
        {

            return 0;
        }

        eFixCharWidth = ctrl->bFixCharWidth > 0 ? FIXWIDTH_ALLCHARS : 0;
        for (state = 0; state < CONTROL_STATE_MAX; state++)
        {
            ucFontSize = GUIENG_GetFontStyle(buttonGroup, STATE_NORMAL << state);
            ret = APP_Check_StringIDList(ucFontSize, eFixCharWidth, ctrl->string_id, ctrl->total_count, ctrl->fg_text_pos);
        }
    }

    return ret;
}

static int APP_Check_Button(WinControl_t *button)
{
    UINT8 ucFontSize = 0;
    FixWidth_t eFixCharWidth = 0;
    int ret = 0;

    if (NULL == button || (button->param) == NULL)
    {
        return 0;
    }

    if (GET_WIDGET_STYLE(button) & S_BTN_TEXT)
    {
        btn_text_t *ctrl = button->param;

        if (STRING_ID_INVALID == ctrl->string_id)
        {

            return 0;
        }

        eFixCharWidth = ctrl->bFixCharWidth > 0 ? FIXWIDTH_ALLCHARS : 0;
        for (state = 0; state < CONTROL_STATE_MAX; state++)
        {
            ucFontSize = GUIENG_GetFontStyle(button, STATE_NORMAL << state);
            ret = APP_Check_StringIDList(ucFontSize, eFixCharWidth, &ctrl->string_id, 1, ctrl->text_pos);
        }
    }
    else if (GET_WIDGET_STYLE(button) & S_BTN_MIX)
    {
        btn_mix_t *ctrl = button->param;
 
        if (STRING_ID_INVALID == ctrl->string_id)
        {

            return 0;
        }

        eFixCharWidth = ctrl->bFixCharWidth > 0 ? FIXWIDTH_ALLCHARS : 0;
        for (state = 0; state < CONTROL_STATE_MAX; state++)
        {
            ucFontSize = GUIENG_GetFontStyle(button, STATE_NORMAL << state);
            ret = APP_Check_StringIDList(ucFontSize, eFixCharWidth, &ctrl->string_id, 1, ctrl->fg_text_pos);
        }
    }

    return ret;
}

static int APP_Check_Textbox(WinControl_t *text)
{
    text_box_t *ctrl = NULL;
    UINT8 ucFontSize = 0;
    FixWidth_t eFixCharWidth = 0;
    int ret = 0;

    if (NULL == text || text->param == NULL)
    {
        return 0;
    }
	if (GET_WIDGET_STYLE(text) & S_TXT_MULTI)
    {
        int languageID = 0;
        TextInfo_t stTextInfo;        
        pgrhInfo_t stPgrhInfo;
        UINT8 *pStr = NULL;
        text_box_multi_t *multi = text->param;

        if (STRING_ID_INVALID == multi->text_info->string_id)
        {
            return 0;
        }
	    memset(&stTextInfo, 0, sizeof(stTextInfo));        
        stTextInfo.ucLineSpacing = multi->line_spacing;
        stTextInfo.stRect = multi->text_info->text_pos;
        stTextInfo.eFixCharWidth =  multi->text_info->bFixCharWidth > 0 ? FIXWIDTH_ALLCHARS : 0;
        stTextInfo.ucTextAlign = GUIENG_GetFontAlign(GET_COMMON_STYLE(text));
        for (state = 0; state < CONTROL_STATE_MAX; state++)
        {
            stTextInfo.ucFontSize = GUIENG_GetFontStyle(text, STATE_NORMAL << state);
            for (languageID = 0; languageID < TV_UI_LANG_NUM; languageID++)
            {
                Font_setCurLang(languageID);
                if (GEL_GetStr(multi->text_info->string_id, &pStr) != GUI_SUCCESS)
                {
                    continue;
                }
                memset(&stPgrhInfo, 0, sizeof(stPgrhInfo));
                Font_getPgrhInfo(pStr, &stTextInfo, &stPgrhInfo);
                if (stPgrhInfo.usTotalLine > stPgrhInfo.usPageLine)
                {
                    printf("%s - %s @ %s - %s - %s, usTotalLine=%d, usPageLine=%d\n", allLangName[languageID], 
                        allStrName[multi->text_info->string_id], allAppletes[menuID].pMenuName, allAppletes[menuID].pWidgetName[controlID],
                        stateArray[state], stPgrhInfo.usTotalLine, stPgrhInfo.usPageLine);
                    ret = 1;
                }
                Font_freePgrhInfo(&stPgrhInfo);
            }
        }
    }
    else
    {
        ctrl = text->param;
        if (STRING_ID_INVALID == ctrl->string_id)
        {

            return 0;
        }

        eFixCharWidth = ctrl->bFixCharWidth > 0 ? FIXWIDTH_ALLCHARS : 0;
        for (state = 0; state < CONTROL_STATE_MAX; state++)
        {
            ucFontSize = GUIENG_GetFontStyle(text, STATE_NORMAL << state);
            ret = APP_Check_StringIDList(ucFontSize, eFixCharWidth, &ctrl->string_id, 1, ctrl->text_pos);
        }
    }
    return ret;
}

int APP_Check_Control_StringSize(void)
{
    int ret = 0;    
    int languageBK = 0;
    STRUCT_APPLET *pApplet;
    WinControl_t *controlList;

    languageBK = Font_getCurLang();

    printf("Start checking...\n");
    for (menuID = 0; (pApplet = allAppletes[menuID].stApplet) != NULL; menuID++)
    {
        controlList = pApplet->control_list;
        DEBUG_MSG("[%s %d] pApplet = %p, no_of_controls = %d\n", __FUNCTION__, __LINE__, pApplet, pApplet->no_of_controls);
        for (controlID = 0; controlID < pApplet->no_of_controls; controlID++)
        {
            switch(controlList[controlID].type)
            {
                case CLASS_TEXT:
                    ret = APP_Check_Textbox(&controlList[controlID]);
                    break;
                case CLASS_BUTTON:
                    ret = APP_Check_Button(&controlList[controlID]);
                    break;
                case CLASS_BUTTON_GROUP:
                    ret = APP_Check_ButtonGroup(&controlList[controlID]);
                    break;
                case CLASS_LISTVIEW:
                    ret = APP_Check_Listview(&controlList[controlID]);
                    break;
                case CLASS_COMBO:
                    ret = APP_Check_Combox(&controlList[controlID]);
                    break;
                default:
                    break;
            }
        }
    }
    printf("Check finished!\n");
    Font_setCurLang(languageBK);

    return ret;
}

