package kr.co.icontrols.ilp300_700.admin.inquiry;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.RelativeLayout;

import kr.co.icontrols.ilp300_700.MainActivity;
import kr.co.icontrols.ilp300_700.R;
import kr.co.icontrols.ilp300_700.global.Configure.ConfigValue;
import kr.co.icontrols.ilp300_700.global.DataClasses.POSITION;
import kr.co.icontrols.ilp300_700.global.Declare;
import kr.co.icontrols.ilp300_700.global.Global;
import kr.co.icontrols.ilp300_700.global.ID;
import kr.co.icontrols.wallpadsupport.WpadImageView;
import kr.co.icontrols.wallpadsupport.WpadScreen;
import kr.co.icontrols.wallpadsupport.WpadTextView;

public class AdminSetInquiryScreen extends WpadScreen {

    String TAG = "AdminSetInquiryScreen";

    boolean bLOG = true;   // true, false
    boolean bLOG_DEBUG = false;   // true, false
    boolean bLOG_INFO = true;   // true, false

    WpadImageView Image_Background;
    WpadTextView Text_Title;
    WpadTextView Text_Guide;
    WpadTextView Text_Line_01;
    WpadTextView Text_Line_02;
    WpadTextView Text_Line_03;
    WpadTextView Text_Line_04;

    int nFontSize_Guide = 14;
    int nFontSize_Instruction = 28;
    boolean bFontNonBold = false;
    boolean bFontBold = true;

    String strText_Title = "";
    String strText_Guide = " * : 이전 페이지 이동\n# : 다음 페이지 이동";
    String strText_Line_01 = "";
    String strText_Line_02 = "";
    String strText_Line_03 = "";
    String strText_Line_04 = "";
    String strTitle_AdminSetting = "조회기능";
    String[] AstrMenu_AdminInquiry = {
            "로비폰 정보",   // 1
            "라인 정보",   // 2
            "서브 로비폰 정보",   // 3
            "Elev. 비연동층 정보",   // 4
            "네트워크 정보",   // 5
            "버전 정보"   // 6
    };

    int nTotalPage = 0;
    int nCurrentPage = 0;
    int nMenuCnt = 0;
    int nMenuPerPage = 4;

    Context mContext;

    public AdminSetInquiryScreen(Context context, RelativeLayout layout) {
        super(context, layout);
        try {
            LOG("------------------ [START - AdminSetInquiryScreen] -------------------");

            mContext = context;
            Initialize();

            Image_Background = new WpadImageView(context, 0, Declare.DeviceInfo.getScreenSizeFullW(ConfigValue.DEVICE_MODELTYPE), Declare.DeviceInfo.getScreenSizeFullH(ConfigValue.DEVICE_MODELTYPE), R.drawable.bg_back, 0, ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.IMAGES.BACKGROUND);
            ViewRegistration(layout, Image_Background, 0, 0);

            Text_Title = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.TITLE).W, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.TITLE).H, Gravity.CENTER, Color.WHITE, nFontSize_Instruction, bFontBold, ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.TITLE);
            ViewRegistration(layout, Text_Title, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.TITLE).X, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.TITLE).Y);
            Text_Title.setText(strText_Title);

            Text_Guide = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.GUIDE).W, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.GUIDE).H, Gravity.LEFT, Color.parseColor("#7d7d7d"), nFontSize_Guide, bFontNonBold, ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.GUIDE);
            ViewRegistration(layout, Text_Guide, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.GUIDE).X, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.GUIDE).Y);
            Text_Guide.setText(strText_Guide);

            Text_Line_01 = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_01).W, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_01).H, Gravity.LEFT, Color.WHITE, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_01);
            ViewRegistration(layout, Text_Line_01, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_01).X, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_01).Y);
            Text_Line_01.setText(strText_Line_01);

            Text_Line_02 = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_02).W, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_02).H, Gravity.LEFT, Color.WHITE, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_02);
            ViewRegistration(layout, Text_Line_02, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_02).X, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_02).Y);
            Text_Line_02.setText(strText_Line_02);

            Text_Line_03 = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_03).W, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_03).H, Gravity.LEFT, Color.WHITE, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_03);
            ViewRegistration(layout, Text_Line_03, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_03).X, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_03).Y);
            Text_Line_03.setText(strText_Line_03);

            Text_Line_04 = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_04).W, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_04).H, Gravity.LEFT, Color.WHITE, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_04);
            ViewRegistration(layout, Text_Line_04, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_04).X, getPosition(ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_04).Y);
            Text_Line_04.setText(strText_Line_04);

            UpdateScreen();
            ((MainActivity) mContext).setAutoScreenChange(ID.SCREENS.Main, MainActivity.nAdminScreenTimeout);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] AdminSetInquiryScreen(...) : " + e);
            e.printStackTrace();
        }
    }

    private void LOG(String log) {
        if (bLOG) Log.v(TAG, log);
    }

    private void LOG_DEBUG(String log) {
        if (bLOG_DEBUG) Log.d(TAG, log);
    }

    private void LOG_INFO(String log) {
        if (bLOG_INFO) Log.i(TAG, log);
    }

    protected void Initialize() {
        try {
            nCurrentPage = Global.nAdminSetPageNumber;

            strText_Title = strTitle_AdminSetting;
            strText_Line_01 = AstrMenu_AdminInquiry[0];
            strText_Line_02 = AstrMenu_AdminInquiry[1];
            strText_Line_03 = AstrMenu_AdminInquiry[2];
            strText_Line_04 = AstrMenu_AdminInquiry[3];

            // 페이지를 계산한다.
            nMenuCnt = AstrMenu_AdminInquiry.length;
            Log.d(TAG, "[Initialize] nMenuCnt = " + nMenuCnt);
            if ((nMenuCnt % nMenuPerPage) != 0) nTotalPage = (nMenuCnt / nMenuPerPage) + 1;
            else nTotalPage = nMenuCnt / nMenuPerPage;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize() : " + e);
            e.printStackTrace();
        }
    }

    @Override
    protected void onClose() {
        try {
            LOG("------------------ [onClose - AdminSetInquiryScreen] -------------------");
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onClose() : " + e);
            e.printStackTrace();
        }
        super.onClose();
    }

    @Override
    protected void onBrReceive(Intent intent) {
        try {
            LOG("[onBrReceive] Intent : " + intent);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onBrReceive(...) : " + e);
            e.printStackTrace();
        }
        super.onBrReceive(intent);
    }

    @Override
    protected void onKeyDown(int keyCode, KeyEvent event) {
        try {
//            LOG("[onKeyDown] keyCode [" + keyCode + "], event [" + event + "]");
            if (Declare.Button.NUMBER_00 <= keyCode && keyCode <= Declare.Button.NUMBER_09) {
                if (nCurrentPage == 1) {
                    if (keyCode == Declare.Button.NUMBER_01) {
                        Global.nAdminSetPageNumber = nCurrentPage;
                        super.setChangeScreen(ID.SCREENS.AdminSet_Inquiry_LobbyPhoneInfo);
                    } else if (keyCode == Declare.Button.NUMBER_02) {
                        Global.nAdminSetPageNumber = nCurrentPage;
                        super.setChangeScreen(ID.SCREENS.AdminSet_Inquiry_LineInfo);
                    } else if (keyCode == Declare.Button.NUMBER_03) {
                        Global.nAdminSetPageNumber = nCurrentPage;
                        super.setChangeScreen(ID.SCREENS.AdminSet_Inquiry_SubLobbyInfo);
                    } else if (keyCode == Declare.Button.NUMBER_04) {
                        Global.nAdminSetPageNumber = nCurrentPage;
                        super.setChangeScreen(ID.SCREENS.AdminSet_Inquiry_ElevDisableFloor);
                    }
                } else if (nCurrentPage == 2) {
                    if (keyCode == Declare.Button.NUMBER_05) {
                        Global.nAdminSetPageNumber = nCurrentPage;
                        super.setChangeScreen(ID.SCREENS.AdminSet_Inquiry_NetworkInfo);
                    } else if (keyCode == Declare.Button.NUMBER_06) {
                        Global.nAdminSetPageNumber = nCurrentPage;
                        super.setChangeScreen(ID.SCREENS.AdminSet_Inquiry_VerInfo);
                    }
                }
            } else if (keyCode == Declare.Button.STAR) {
                PageDown();
            } else if (keyCode == Declare.Button.SHARP) {
                PageUp();
            } else if (keyCode == Declare.Button.CANCEL || keyCode == Declare.Button.TACT_CANCEL) {
                super.setChangeScreen(ID.SCREENS.AdminSet_Main);
            }
            UpdateScreen();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onKeyDown(...) : " + e);
            e.printStackTrace();
        }
        super.onKeyDown(keyCode, event);
    }

    private void UpdateScreen() {
        try {
            LOG("[updateScreen] Start!!");
            SetPageScreen();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateScreen() : " + e);
            e.printStackTrace();
        }
    }

    protected void PageUp() {
        try {
            LOG("[PageUp] nCurrentPage = " + nCurrentPage);
            LOG("[PageUp] nTotalPage = " + nTotalPage);
            if (nCurrentPage < nTotalPage) nCurrentPage++;
            else nCurrentPage = 1;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] PageUp() : " + e);
            e.printStackTrace();
        }
    }

    protected void PageDown() {
        try {
            LOG("[PageDown] nCurrentPage = " + nCurrentPage);
            LOG("[PageDown] nTotalPage = " + nTotalPage);
            if (nCurrentPage > 1) nCurrentPage--;
            else nCurrentPage = nTotalPage;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] PageDown() : " + e);
            e.printStackTrace();
        }
    }

    protected void SetPageScreen() {
        try {
            int nMenuIndex = (nCurrentPage * nMenuPerPage) - nMenuPerPage;
            LOG("[SetPageScreen] nCurrentPage = " + nCurrentPage);
            LOG("[SetPageScreen] nMenuIndex = " + nMenuIndex);
            LOG("[SetPageScreen] nMenuCnt = " + nMenuCnt);

            if (nMenuIndex < nMenuCnt) {
                strText_Line_01 = nMenuIndex + 1 + "] " + AstrMenu_AdminInquiry[nMenuIndex];
                nMenuIndex++;
            } else {
                strText_Line_01 = "";
            }

            if (nMenuIndex < nMenuCnt) {
                strText_Line_02 = nMenuIndex + 1 + "] " + AstrMenu_AdminInquiry[nMenuIndex];
                nMenuIndex++;
            } else {
                strText_Line_02 = "";
            }

            if (nMenuIndex < nMenuCnt) {
                strText_Line_03 = nMenuIndex + 1 + "] " + AstrMenu_AdminInquiry[nMenuIndex];
                nMenuIndex++;
            } else {
                strText_Line_03 = "";
            }

            if (nMenuIndex < nMenuCnt) {
                strText_Line_04 = nMenuIndex + 1 + "] " + AstrMenu_AdminInquiry[nMenuIndex];
                nMenuIndex++;
            } else {
                strText_Line_04 = "";
            }

            Text_Line_01.setText(strText_Line_01);
            Text_Line_02.setText(strText_Line_02);
            Text_Line_03.setText(strText_Line_03);
            Text_Line_04.setText(strText_Line_04);

        } catch (Exception e) {
            Log.e(TAG, "[Exception] SetPageScreen() : " + e);
            e.printStackTrace();
        }
    }

    private POSITION getPosition(int nID) {
//        LOG("[getPosition] Start!!");
        POSITION position = new POSITION();
        position.SetPosition(0, 0, 480, 272);
        try {
            switch (nID) {
                case ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.IMAGES.BACKGROUND:
                    position.SetPosition(0, 0, 480, 272);
                    return position;

                case ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.TITLE:
                    position.SetPosition(0, 24, 480, 40);
                    return position;

                case ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.GUIDE:
                    position.SetPosition(350, 25, 130, 40);
                    return position;

                case ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_01:
                    position.SetPosition(30, 73, 450, 40);
                    return position;

                case ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_02:
                    position.SetPosition(30, 122, 450, 40);
                    return position;

                case ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_03:
                    position.SetPosition(30, 171, 450, 40);
                    return position;

                case ID.SCREENS.ADMINSETINQUIRY.ELEMENTS.TEXTS.LINE_04:
                    position.SetPosition(30, 220, 450, 40);
                    return position;

                default:
                    return position;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getPosition() : " + e);
            e.printStackTrace();
            return position;
        }
    }


}