package kr.co.icontrols.ilp300_700.screen;

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.view.View;
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.Enumeration.PWTYPE;
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 AdminSetAdminPW extends WpadScreen {

    String TAG = "AdminSetAdminPW";

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

    Context mContext;

    WpadImageView Image_Background;
    WpadImageView Image_AdminPWInput_01;
    WpadImageView Image_AdminPWInput_02;
    WpadImageView Image_AdminPWInput_03;
    WpadImageView Image_AdminPWInput_04;
    WpadImageView Image_PW_01;
    WpadImageView Image_PW_02;
    WpadImageView Image_PW_03;
    WpadImageView Image_PW_04;
    WpadTextView Text_Instruction;

    int[] BGImages = {R.drawable.bg_black, R.drawable.bg_black_700};
    int[] BlankImages = {R.drawable.box_blank, R.drawable.image_box_pw_blank};
    int[] PWImages = {R.drawable.box_pw, R.drawable.image_box_pw};

    int[] nFONTSIZE_INSTRUCTION = {26, 46};
    String strText_Instruction = "관리자설정 비밀번호를 입력하세요.";

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

            Initialize();
            mContext = context;

            Image_Background = new WpadImageView(context, 0, Declare.DeviceInfo.getScreenSizeFullW(ConfigValue.DEVICE_MODELTYPE), Declare.DeviceInfo.getScreenSizeFullH(ConfigValue.DEVICE_MODELTYPE), BGImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.BACKGROUND);
            ViewRegistration(layout, Image_Background, 0, 0);

            Image_AdminPWInput_01 = new WpadImageView(context, 0, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_01).W, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_01).H, BlankImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_01);
            ViewRegistration(layout, Image_AdminPWInput_01, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_01).X, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_01).Y);

            Image_AdminPWInput_02 = new WpadImageView(context, 0, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_02).W, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_02).H, BlankImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_02);
            ViewRegistration(layout, Image_AdminPWInput_02, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_02).X, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_02).Y);

            Image_AdminPWInput_03 = new WpadImageView(context, 0, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_03).W, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_03).H, BlankImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_03);
            ViewRegistration(layout, Image_AdminPWInput_03, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_03).X, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_03).Y);

            Image_AdminPWInput_04 = new WpadImageView(context, 0, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_04).W, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_04).H, BlankImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_04);
            ViewRegistration(layout, Image_AdminPWInput_04, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_04).X, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_04).Y);

            Image_PW_01 = new WpadImageView(context, 0, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_01).W, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_01).H, PWImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_01);
            ViewRegistration(layout, Image_PW_01, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_01).X, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_01).Y);

            Image_PW_02 = new WpadImageView(context, 0, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_02).W, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_02).H, PWImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_02);
            ViewRegistration(layout, Image_PW_02, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_02).X, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_02).Y);

            Image_PW_03 = new WpadImageView(context, 0, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_03).W, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_03).H, PWImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_03);
            ViewRegistration(layout, Image_PW_03, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_03).X, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_03).Y);

            Image_PW_04 = new WpadImageView(context, 0, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_04).W, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_04).H, PWImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_04);
            ViewRegistration(layout, Image_PW_04, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_04).X, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_04).Y);

            Text_Instruction = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.TEXTS.INSTRUCTION).W, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.TEXTS.INSTRUCTION).H, Gravity.CENTER, Color.WHITE, nFONTSIZE_INSTRUCTION[ConfigValue.DEVICE_MODELTYPE], false, ID.SCREENS.ADMINSETADMINPW.ELEMENTS.TEXTS.INSTRUCTION);
            ViewRegistration(layout, Text_Instruction, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.TEXTS.INSTRUCTION).X, getPosition(ID.SCREENS.ADMINSETADMINPW.ELEMENTS.TEXTS.INSTRUCTION).Y);
            Text_Instruction.setText(strText_Instruction);

            UpdateScreen();
            ((MainActivity) mContext).setAutoScreenChange(ID.SCREENS.Main, MainActivity.nDefaultScreenTimeout);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] AdminSetAdminPW(...) : " + 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 {
            Global.strInputPW = "";
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize() : " + e);
            e.printStackTrace();
        }
    }

    @Override
    protected void onClose() {
        LOG("------------------ [onClose - AdminSetAdminPW] -------------------");
        try {
            Global.strInputPW = "";
        } 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) {
                Global.strInputPW += Integer.toString(keyCode - 7);
                Log.d(TAG, "[onKeyDown] Global.strInputPW [" + Global.strInputPW + "]");
            } else if (keyCode == Declare.Button.GUARD) {
                if (Global.ComparePW(PWTYPE.ADMINSETPW, Global.strInputPW)) {
                    LOG("[onKeyDown] Password OK!!");
                    // 관리자 설정화면으로 진입
                    MainActivity.mSound.PlayMent_lobby(R.raw.resultok);
                    super.setChangeScreen(ID.SCREENS.AdminSet_Main);
                } else {
                    LOG("[onKeyDown] Password different!!");
                    Initialize();
                    MainActivity.mSound.PlayMent_lobby(R.raw.retryinput);
                    super.setChangeScreen(ID.SCREENS.Main);
                }
            } else if (keyCode == Declare.Button.CANCEL || keyCode == Declare.Button.TACT_CANCEL) {
                super.setChangeScreen(ID.SCREENS.Main);
            }
            UpdateScreen();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onKeyDown(...) : " + e);
            e.printStackTrace();
        }
        super.onKeyDown(keyCode, event);
    }

    public void UpdateScreen() {
        try {
            LOG("[updateScreen] Global.strInputPW = " + Global.strInputPW);
            int nPWLength = 0;
            if (Global.strInputPW != "") {
                nPWLength = Global.strInputPW.length();
            }

            if (nPWLength == 0) {
                Image_PW_01.setVisibility(View.INVISIBLE);
                Image_PW_02.setVisibility(View.INVISIBLE);
                Image_PW_03.setVisibility(View.INVISIBLE);
                Image_PW_04.setVisibility(View.INVISIBLE);
            } else if (nPWLength == 1) {
                Image_PW_01.setVisibility(View.VISIBLE);
                Image_PW_02.setVisibility(View.INVISIBLE);
                Image_PW_03.setVisibility(View.INVISIBLE);
                Image_PW_04.setVisibility(View.INVISIBLE);
            } else if (nPWLength == 2) {
                Image_PW_01.setVisibility(View.VISIBLE);
                Image_PW_02.setVisibility(View.VISIBLE);
                Image_PW_03.setVisibility(View.INVISIBLE);
                Image_PW_04.setVisibility(View.INVISIBLE);
            } else if (nPWLength == 3) {
                Image_PW_01.setVisibility(View.VISIBLE);
                Image_PW_02.setVisibility(View.VISIBLE);
                Image_PW_03.setVisibility(View.VISIBLE);
                Image_PW_04.setVisibility(View.INVISIBLE);
            } else if (nPWLength == 4) {
                Image_PW_01.setVisibility(View.VISIBLE);
                Image_PW_02.setVisibility(View.VISIBLE);
                Image_PW_03.setVisibility(View.VISIBLE);
                Image_PW_04.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateScreen() : " + e);
            e.printStackTrace();
        }
    }

    public POSITION getPosition(int nID) {
//      LOG("[getPosition] Start!!");
        POSITION position = new POSITION();
        position.SetPosition(0, 0, 480, 272);
        try {
            switch (nID) {
                case ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.BACKGROUND:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(0, 0, 800, 480);
                    else position.SetPosition(0, 0, 480, 272);
                    return position;

                case ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_01:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(160, 142, 105, 135);
                    else position.SetPosition(68, 65, 74, 94);
                    return position;

                case ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_02:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(285, 142, 105, 135);
                    else position.SetPosition(158, 65, 74, 94);
                    return position;

                case ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_03:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(411, 142, 105, 135);
                    else position.SetPosition(248, 65, 74, 94);
                    return position;

                case ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.ADMINSETPW_INPUT_04:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(536, 142, 105, 135);
                    else position.SetPosition(338, 65, 74, 94);
                    return position;

                case ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_01:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(160, 142, 105, 135);
                    else position.SetPosition(68, 65, 74, 94);
                    return position;

                case ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_02:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(285, 142, 105, 135);
                    else position.SetPosition(158, 65, 74, 94);
                    return position;

                case ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_03:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(411, 142, 105, 135);
                    else position.SetPosition(248, 65, 74, 94);
                    return position;

                case ID.SCREENS.ADMINSETADMINPW.ELEMENTS.IMAGES.PW_04:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(536, 142, 105, 135);
                    else position.SetPosition(338, 65, 74, 94);
                    return position;

                case ID.SCREENS.ADMINSETADMINPW.ELEMENTS.TEXTS.INSTRUCTION:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(0, 381, 800, 99);
                    else position.SetPosition(0, 216, 480, 56);
                    return position;

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