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.ID;
import kr.co.icontrols.wallpadsupport.WpadImageView;
import kr.co.icontrols.wallpadsupport.WpadScreen;
import kr.co.icontrols.wallpadsupport.WpadTextView;

public class AdminSet_Inquiry_LineInfo extends WpadScreen {

    String TAG = "AdminSet_Inquiry_LineInfo";

    boolean bLOG = true;   // true, false
    boolean bLOG_DEBUG = false;   // true, false
    boolean bLOG_INFO = true;   // true, false
    Context mContext;
    WpadImageView Image_Background;
    WpadTextView Text_Title;
    WpadTextView Text_Line_01_Item;
    WpadTextView Text_Line_02_Item;
    WpadTextView Text_Line_03_Item;
    WpadTextView Text_Line_04_Item;
    int TextColor_Normal = Color.WHITE;
    int TextColor_Highlight = Color.GREEN;
    int TextColor_Red = Color.RED;
    int nFontSize_Instruction = 28;
    int nFontSize_Info = 22;
    boolean bFontNonBold = false;
    boolean bFontBold = true;
    String strText_Title = "라인 정보 조회";
    String strText_Line_01_Item = "라인 개수";
    String strText_Line_02_Item = "";
    String strText_Line_03_Item = "";
    String strText_Line_04_Item = "";
    String[] aLineInfo;
    String[] aLineInfoString = new String[3];
    int nTotalPageNum = 0;
    int nCurrentPageNum = 1;
    public AdminSet_Inquiry_LineInfo(Context context, RelativeLayout layout) {
        super(context, layout);
        try {
            LOG("------------------ [START - AdminSet_Inquiry_LineInfo] -------------------");

            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.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.IMAGES.BACKGROUND);
            ViewRegistration(layout, Image_Background, 0, 0);

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

            Text_Line_01_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_01_Item).W, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_01_Item).H, Gravity.CENTER, Color.WHITE, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_01_Item);
            ViewRegistration(layout, Text_Line_01_Item, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_01_Item).X, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_01_Item).Y);
            Text_Line_01_Item.setText(strText_Line_01_Item);

            Text_Line_02_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_02_Item).W, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_02_Item).H, Gravity.LEFT, Color.WHITE, nFontSize_Info, bFontNonBold, ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_02_Item);
            ViewRegistration(layout, Text_Line_02_Item, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_02_Item).X, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_02_Item).Y);
            Text_Line_02_Item.setText(strText_Line_02_Item);

            Text_Line_03_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_03_Item).W, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_03_Item).H, Gravity.LEFT, Color.WHITE, nFontSize_Info, bFontNonBold, ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_03_Item);
            ViewRegistration(layout, Text_Line_03_Item, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_03_Item).X, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_03_Item).Y);
            Text_Line_03_Item.setText(strText_Line_03_Item);

            Text_Line_04_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_04_Item).W, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_04_Item).H, Gravity.LEFT, Color.WHITE, nFontSize_Info, bFontNonBold, ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_04_Item);
            ViewRegistration(layout, Text_Line_04_Item, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_04_Item).X, getPosition(ID.SCREENS.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.TEXTS.LINE_04_Item).Y);
            Text_Line_04_Item.setText(strText_Line_04_Item);

            UpdateScreen();
            ((MainActivity) mContext).setAutoScreenChange(ID.SCREENS.Main, MainActivity.nAdminScreenTimeout);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] AdminSet_Inquiry_LineInfo(...) : " + 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 {
            LOG("[Initialize ===== START =====");
            int nLineCnt = ConfigValue.LineInfo.getCnt();
            if (nLineCnt != 0) {
                nCurrentPageNum = 1;
                if ((nLineCnt % 9) == 0) {
                    nTotalPageNum = nLineCnt / 9;
                } else if ((nLineCnt % 9) != 0) {
                    nTotalPageNum = (nLineCnt / 9) + 1;
                }
            } else {
                nTotalPageNum = 1;
                nCurrentPageNum = 1;
            }

            aLineInfo = new String[nLineCnt];
            for (int i = 0; i < aLineInfo.length; i++) {
                aLineInfo[i] = "(" + ConfigValue.LineInfo.getLineInfo(i).NAME() + ") " + ConfigValue.LineInfo.getLineInfo(i).LOW() + "~" + ConfigValue.LineInfo.getLineInfo(i).HIGH();
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize() : " + e);
            e.printStackTrace();
        }
    }

    @Override
    protected void onClose() {
        try {
            LOG("------------------ [onClose - AdminSet_Inquiry_LineInfo] -------------------");
        } 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 (keyCode == Declare.Button.SHARP) {
                PageUp();
            } else if (keyCode == Declare.Button.STAR) {
                PageDown();
            } else if (keyCode == Declare.Button.CANCEL || keyCode == Declare.Button.TACT_CANCEL) {
                super.setChangeScreen(ID.SCREENS.AdminSet_Inquiry);
            }
            UpdateScreen();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onKeyDown(...) : " + e);
            e.printStackTrace();
        }
        super.onKeyDown(keyCode, event);
    }

    private void UpdateScreen() {
        try {
//          LOG("[updateScreen] Start!!");
            if (ConfigValue.LineInfo.getCnt() != 0) {
                LineInfoPrint();

                strText_Title = "라인 정보 " + nCurrentPageNum + " / " + nTotalPageNum;
                strText_Line_01_Item = "라인 개수 : " + ConfigValue.LineInfo.getCnt();
                strText_Line_02_Item = aLineInfoString[0];
                strText_Line_03_Item = aLineInfoString[1];
                strText_Line_04_Item = aLineInfoString[2];
            } else {
                strText_Title = "라인 정보 " + nCurrentPageNum + " / " + nTotalPageNum;
                strText_Line_01_Item = "설정된 라인정보가 없습니다.";
                strText_Line_02_Item = "";
                strText_Line_03_Item = "";
                strText_Line_04_Item = "";
            }

            Text_Title.setText(strText_Title);
            Text_Line_01_Item.setText(strText_Line_01_Item);
            Text_Line_02_Item.setText(strText_Line_02_Item);
            Text_Line_03_Item.setText(strText_Line_03_Item);
            Text_Line_04_Item.setText(strText_Line_04_Item);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateScreen() : " + 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.ADMINSET_INQUIRY_LINEINFO.ELEMENTS.IMAGES.BACKGROUND:
                    position.SetPosition(0, 0, 480, 272);
                    return position;

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

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

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

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

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

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

    private void PageUp() {
        try {
            if (nCurrentPageNum < nTotalPageNum) {
                nCurrentPageNum++;
            } else {
                nCurrentPageNum = 1;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] PageUp() : " + e);
            e.printStackTrace();
        }
    }

    private void PageDown() {
        try {
            if (nCurrentPageNum > 1) {
                nCurrentPageNum--;
            } else {
                nCurrentPageNum = nTotalPageNum;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] PageDown() : " + e);
            e.printStackTrace();
        }
    }

    private void LineInfoPrint() {
        try {
            int nIndex = (nCurrentPageNum * 9) - 9;
            int nCnt = -1;
            clearLineInfo();

            for (int i = nIndex; i < (nCurrentPageNum * 9); i++) {
                if (ConfigValue.LineInfo.getCnt() > i) {
                    if ((i == nIndex) || (i == nIndex + 3) || (i == nIndex + 6)) {
                        nCnt++;
                        aLineInfoString[nCnt] = aLineInfo[i];
                    } else {
                        aLineInfoString[nCnt] += " / " + aLineInfo[i];
                    }
                } else {
                    return;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] LineInfoPrint() : " + e);
            e.printStackTrace();
        }
    }

    private void clearLineInfo() {
        try {
            for (int i = 0; i < aLineInfoString.length; i++) {
                aLineInfoString[i] = "";
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] clearLineInfo() : " + e);
            e.printStackTrace();
        }
    }

//    private void testLineInfo() {
//        ConfigValue.LineInfo.setCnt(6);
//        for (int i = 0; i < ConfigValue.LineInfo.getCnt(); i++) {
//            ConfigValue.LineInfo.aLineName[i] = i;
//            ConfigValue.LineInfo.aLineFloorLow[i] = i;
//            ConfigValue.LineInfo.aLineFloorHigh[i] = i;
//        }
//    }


}