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.Declare.AccessValues;
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.ilp300_700.global.LobbyAccessCtrl;
import kr.co.icontrols.wallpadsupport.WpadImageView;
import kr.co.icontrols.wallpadsupport.WpadScreen;
import kr.co.icontrols.wallpadsupport.WpadTextView;

public class AccessAdminPW extends WpadScreen {

    String TAG = "AccessAdminPW";

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

    Context mContext;

    WpadImageView IMG_BACKGROUND;
    WpadImageView IMG_ACCESSADMINPWINPUT_01;
    WpadImageView IMG_ACCESSADMINPWINPUT_02;
    WpadImageView IMG_ACCESSADMINPWINPUT_03;
    WpadImageView IMG_ACCESSADMINPWINPUT_04;
    WpadImageView IMG_PW_01;
    WpadImageView IMG_PW_02;
    WpadImageView IMG_PW_03;
    WpadImageView IMG_PW_04;
    WpadTextView TXT_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 = {26, 46};
    String strTXT_INSTRUCTION = "관리자출입 비밀번호를 입력하세요.";

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

            mContext = context;
            Initialize();

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

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

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

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

            IMG_ACCESSADMINPWINPUT_04 = new WpadImageView(context, 0, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.IMAGES.ADMINPW_INPUT_04).W, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.IMAGES.ADMINPW_INPUT_04).H, BlankImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ACCESSADMINPW.ELEMENTS.IMAGES.ADMINPW_INPUT_04);
            ViewRegistration(layout, IMG_ACCESSADMINPWINPUT_04, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.IMAGES.ADMINPW_INPUT_04).X, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.IMAGES.ADMINPW_INPUT_04).Y);
            
            IMG_PW_01 = new WpadImageView(context, 0, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.IMAGES.PW_01).W, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.IMAGES.PW_01).H, PWImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ACCESSADMINPW.ELEMENTS.IMAGES.PW_01);
            ViewRegistration(layout, IMG_PW_01, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.IMAGES.PW_01).X, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.IMAGES.PW_01).Y);

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

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

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

            TXT_INSTRUCTION = new WpadTextView(context, false, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.TEXTS.INSTRUCTION).W, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.TEXTS.INSTRUCTION).H, Gravity.CENTER, Color.WHITE, nFONTSIZE[ConfigValue.DEVICE_MODELTYPE], false, ID.SCREENS.ACCESSADMINPW.ELEMENTS.TEXTS.INSTRUCTION);
            ViewRegistration(layout, TXT_INSTRUCTION, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.TEXTS.INSTRUCTION).X, getPosition(ID.SCREENS.ACCESSADMINPW.ELEMENTS.TEXTS.INSTRUCTION).Y);
            TXT_INSTRUCTION.setText(strTXT_INSTRUCTION);

            UpdateScreen();
            ((MainActivity) mContext).setAutoScreenChange(ID.SCREENS.Main, MainActivity.nDefaultScreenTimeout);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] AccessAdminPW(...) : " + 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() {
        try {
            LOG("------------------ [onClose - AccessAdminPW] -------------------");
            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) {
                if (Integer.valueOf(Global.strInputPW.length()) < 4) {
                    Global.strInputPW += Integer.toString(keyCode - 7);
                }
            } else if (keyCode == Declare.Button.SHARP) {
                if (Global.ComparePW(PWTYPE.ACCESS_ADMINPW, Global.strInputPW)) {
                    LOG("[onKeyDown] Password OK!!");
                    // 비밀번호 동일함
                    // 로비폰 문을 연다.
                    Global.ctrlLobbyDoorNMent(true, true, ConfigValue.nDoorOpenDuration, false, false);
                    LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.pass, AccessValues.adminpw, ConfigValue.strDeviceIPAddress, "null", 0, 0, 0, 0, "null");
                    Global.sendElevCallRequest("null", "null", true);
                    super.setChangeScreen(ID.SCREENS.Main);
                } else {
                    LOG("[onKeyDown] Password different!!");
                    Global.ctrlLobbyDoorNMent(false, true, 0, false, false);
                    LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.fail, AccessValues.adminpw, ConfigValue.strDeviceIPAddress, "null", 0, 0, 0, 0, Global.strInputPW);
                    Initialize();
                    MainActivity.mSound.PlayMent_lobby(R.raw.retryinput);
                }
            } 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) {
                IMG_PW_01.setVisibility(View.INVISIBLE);
                IMG_PW_02.setVisibility(View.INVISIBLE);
                IMG_PW_03.setVisibility(View.INVISIBLE);
                IMG_PW_04.setVisibility(View.INVISIBLE);
            } else if (nPWLength == 1) {
                IMG_PW_01.setVisibility(View.VISIBLE);
                IMG_PW_02.setVisibility(View.INVISIBLE);
                IMG_PW_03.setVisibility(View.INVISIBLE);
                IMG_PW_04.setVisibility(View.INVISIBLE);
            } else if (nPWLength == 2) {
                IMG_PW_01.setVisibility(View.VISIBLE);
                IMG_PW_02.setVisibility(View.VISIBLE);
                IMG_PW_03.setVisibility(View.INVISIBLE);
                IMG_PW_04.setVisibility(View.INVISIBLE);
            } else if (nPWLength == 3) {
                IMG_PW_01.setVisibility(View.VISIBLE);
                IMG_PW_02.setVisibility(View.VISIBLE);
                IMG_PW_03.setVisibility(View.VISIBLE);
                IMG_PW_04.setVisibility(View.INVISIBLE);
            } else if (nPWLength == 4) {
                IMG_PW_01.setVisibility(View.VISIBLE);
                IMG_PW_02.setVisibility(View.VISIBLE);
                IMG_PW_03.setVisibility(View.VISIBLE);
                IMG_PW_04.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateScreen() : " + e);
            e.printStackTrace();
        }
    }

    public POSITION getPosition(int nID) {
        POSITION position = new POSITION();
        position.SetPosition(0, 0, 480, 272);
        try {
            switch (nID) {
                case ID.SCREENS.ACCESSADMINPW.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.ACCESSADMINPW.ELEMENTS.IMAGES.ADMINPW_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.ACCESSADMINPW.ELEMENTS.IMAGES.ADMINPW_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.ACCESSADMINPW.ELEMENTS.IMAGES.ADMINPW_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.ACCESSADMINPW.ELEMENTS.IMAGES.ADMINPW_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.ACCESSADMINPW.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.ACCESSADMINPW.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.ACCESSADMINPW.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.ACCESSADMINPW.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.ACCESSADMINPW.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:
                    return position;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getPosition() : " + e);
            e.printStackTrace();
            return position;
        }
    }
    
}