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

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.ConfigValKey;
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.Enumeration.DATAINPUTSTEP;
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 AdminSet_Setting_AdminAccessPWChange extends WpadScreen {

    String TAG = "AdminSet_Setting_AdminAccessPWChange";

    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_01_Input;
    WpadTextView Text_Line_02_Input;
    int TextColor_Normal = Color.WHITE;
    int TextColor_Highlight = Color.GREEN;
    int nFontSize_Instruction = 28;
    boolean bFontNonBold = false;
    boolean bFontBold = true;
    String strText_Title = "관리자 출입 비밀번호";
    String strText_Line_01_Item = "새 비밀번호";
    String strText_Line_02_Item = "확인 비밀번호";
    String strText_Line_01_Input = "";
    String strText_Line_02_Input = "";
    public AdminSet_Setting_AdminAccessPWChange(Context context, RelativeLayout layout) {
        super(context, layout);
        try {
            LOG("------------------ [START - AdminSet_Setting_AdminAccessPWChange] -------------------");

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

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

            Text_Line_01_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Item).W, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Item).H, Gravity.LEFT, Color.WHITE, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Item);
            ViewRegistration(layout, Text_Line_01_Item, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Item).X, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.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_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Item).W, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Item).H, Gravity.LEFT, Color.WHITE, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Item);
            ViewRegistration(layout, Text_Line_02_Item, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Item).X, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Item).Y);
            Text_Line_02_Item.setText(strText_Line_02_Item);

            Text_Line_01_Input = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Input).W, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Input).H, Gravity.LEFT, Color.WHITE, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Input);
            ViewRegistration(layout, Text_Line_01_Input, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Input).X, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Input).Y);
            Text_Line_01_Input.setText("[ " + strText_Line_01_Input + " ]");

            Text_Line_02_Input = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Input).W, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Input).H, Gravity.LEFT, Color.WHITE, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Input);
            ViewRegistration(layout, Text_Line_02_Input, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Input).X, getPosition(ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Input).Y);
            Text_Line_02_Input.setText("[ " + strText_Line_02_Input + " ]");

            UpdateScreen();

            ((MainActivity) mContext).setAutoScreenChange(ID.SCREENS.Main, MainActivity.nAdminScreenTimeout);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] AdminSet_Setting_AdminAccessPWChange(...) : " + 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 =====");
            Global.DataInputStep = DATAINPUTSTEP.INPUT_01;

            strText_Line_01_Input = "0000";
            strText_Line_02_Input = "0000";
            Text_Line_01_Input.setText("[ " + strText_Line_01_Input + " ]");
            Text_Line_02_Input.setText("[ " + strText_Line_02_Input + " ]");
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize() : " + e);
            e.printStackTrace();
        }
    }

    @Override
    protected void onClose() {
        try {
            LOG("------------------ [onClose - AdminSet_Setting_AdminAccessPWChange] -------------------");
        } 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 (Global.DataInputStep == DATAINPUTSTEP.INPUT_01) {
                    strText_Line_01_Input += keyCode - 7;

                    if (strText_Line_01_Input.length() > 4) {
                        strText_Line_01_Input = strText_Line_01_Input.substring(1);
                    }
                } else if (Global.DataInputStep == DATAINPUTSTEP.INPUT_02) {
                    strText_Line_02_Input += keyCode - 7;

                    if (strText_Line_02_Input.length() > 4) {
                        strText_Line_02_Input = strText_Line_02_Input.substring(1);
                    }
                }
                UpdateScreen();
            } else if (keyCode == Declare.Button.STAR) {
                if (Global.DataInputStep == DATAINPUTSTEP.INPUT_01) {
                    Global.DataInputStep = DATAINPUTSTEP.INPUT_02;
                } else if (Global.DataInputStep == DATAINPUTSTEP.INPUT_02) {
                    Global.DataInputStep = DATAINPUTSTEP.INPUT_01;
                }
                UpdateScreen();
            } else if (keyCode == Declare.Button.GUARD) {
                // 입력된 비밀번호를 비교하여 동일한 값이면 저장한다.
                if (setAdminAccessPW(strText_Line_01_Input, strText_Line_02_Input)) {
                    MainActivity.mSound.PlayMent_lobby(R.raw.setting);
                    super.setChangeScreen(ID.SCREENS.AdminSet_Setting_AdminAccessPW);
                } else {
                    MainActivity.mSound.PlayMent_lobby(R.raw.retryinput);
                    Initialize();
                    UpdateScreen();
                }
            } else if (keyCode == Declare.Button.CANCEL || keyCode == Declare.Button.TACT_CANCEL) {
                super.setChangeScreen(ID.SCREENS.AdminSet_Setting_AdminAccessPW);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onKeyDown(...) : " + e);
            e.printStackTrace();
        }
        super.onKeyDown(keyCode, event);
    }

    protected void UpdateScreen() {
        try {
//          LOG("[updateScreen] Start!!");
            if (Global.DataInputStep == DATAINPUTSTEP.INPUT_01) {
                Text_Line_01_Input.setText("[ " + strText_Line_01_Input + " ]");

                Text_Line_01_Item.setTextColor(TextColor_Highlight);
                Text_Line_02_Item.setTextColor(TextColor_Normal);
                Text_Line_01_Input.setTextColor(TextColor_Highlight);
                Text_Line_02_Input.setTextColor(TextColor_Normal);
            } else if (Global.DataInputStep == DATAINPUTSTEP.INPUT_02) {
                Text_Line_02_Input.setText("[ " + strText_Line_02_Input + " ]");

                Text_Line_01_Item.setTextColor(TextColor_Normal);
                Text_Line_02_Item.setTextColor(TextColor_Highlight);
                Text_Line_01_Input.setTextColor(TextColor_Normal);
                Text_Line_02_Input.setTextColor(TextColor_Highlight);
            } else {
                Text_Line_01_Item.setTextColor(TextColor_Normal);
                Text_Line_02_Item.setTextColor(TextColor_Normal);
                Text_Line_01_Input.setTextColor(TextColor_Normal);
                Text_Line_02_Input.setTextColor(TextColor_Normal);
            }
        } 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_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.IMAGES.BACKGROUND:
                    position.SetPosition(0, 0, 480, 272);
                    return position;

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

                case ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Item:
                    position.SetPosition(50, 104, 145, 40);
                    return position;

                case ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Item:
                    position.SetPosition(50, 154, 145, 40);
                    //position.SetPosition(25, 154, 170, 40);
                    return position;

                case ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_01_Input:
                    position.SetPosition(205, 104, 205, 40);
                    return position;

                case ID.SCREENS.ADMINSET_SETTING_ADMINACCESSPWCHANGE.ELEMENTS.TEXTS.LINE_02_Input:
                    position.SetPosition(205, 154, 205, 40);
                    return position;

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

    private boolean setAdminAccessPW(String pw_01, String pw_02) {
        try {
            LOG("[setAdminAccessPW] pw_01 [" + pw_01 + "], pw_02 [" + pw_02 + "]");
            if (!pw_01.equals(pw_02)) return false;   // compare passwords inputed
            for (int i = 0; i < Global.astrBlockedPW.length; i++) {
                if (Global.astrBlockedPW[i].equals(pw_01)) return false;
            }
            ConfigValue.strAdminAccessPW = pw_01;
            if (!MainActivity.ConfigDBManager.setData(Declare.DBInfo.TABLE_NAME_CONFIG, ConfigValKey.AdminAccessPW, ConfigValue.strAdminAccessPW))
                return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setAdminAccessPW(pw_01, pw_02)");
            e.printStackTrace();
            return false;
        }
    }

}