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.ethernet.EthernetCtrl;
import kr.co.icontrols.ilp300_700.ethernet.iMAPCommon.iMAPResult;
import kr.co.icontrols.ilp300_700.global.Configure.ConfigValue;
import kr.co.icontrols.ilp300_700.global.Configure.SetValue;
import kr.co.icontrols.ilp300_700.global.DataClasses.POSITION;
import kr.co.icontrols.ilp300_700.global.DataClasses.RemoteIDInfo;
import kr.co.icontrols.ilp300_700.global.Declare;
import kr.co.icontrols.ilp300_700.global.Declare.AccessValues;
import kr.co.icontrols.ilp300_700.global.Declare.DeviceType;
import kr.co.icontrols.ilp300_700.global.Enumeration.TARGETINFOINPUTSTEP;
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.ilp300_700.imap.Request_check_lobby_passwd;
import kr.co.icontrols.ilp300_700.imap.iMAPResultCtrl;
import kr.co.icontrols.wallpadsupport.WpadImageView;
import kr.co.icontrols.wallpadsupport.WpadScreen;
import kr.co.icontrols.wallpadsupport.WpadTextView;

public class AccessResidencePW extends WpadScreen {

    String TAG = "AccessResidencePW";

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

    Context mContext;

    WpadImageView IMG_BACKGROUND;
    WpadImageView IMG_ACCESSRESIDENCEPWINPUT_01;
    WpadImageView IMG_ACCESSRESIDENCEPWINPUT_02;
    WpadImageView IMG_ACCESSRESIDENCEPWINPUT_03;
    WpadImageView IMG_ACCESSRESIDENCEPWINPUT_04;
    WpadImageView IMG_PW_01;
    WpadImageView IMG_PW_02;
    WpadImageView IMG_PW_03;
    WpadImageView IMG_PW_04;
    WpadImageView IMG_RESIDENCE_DONG;   // "동"
    WpadImageView IMG_RESIDENCE_HO;   // "호"
    WpadTextView TXT_RESIDENCEINFO_DONG;   // 동정보
    WpadTextView TXT_RESIDENCEINFO_HO;   // 호정보
    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[] DongImages = {R.drawable.image_dong, R.drawable.image_dong_700};
    int[] HoImages = {R.drawable.image_ho, R.drawable.image_ho_700};
    int[] nFONTSIZE_DONGHO = {30, 54};
    int[] nFONTSIZE_DONGHO_RENT = {20, 36};
    int[] nFONTSIZE_INSTRUCTION = {26, 46};
    int MAXLENGTH_DONG = 4;
    int MAXLENGTH_HO = 4;
    boolean bFUNCBlock = false;
    String strTXT_INSTRUCTION = "";

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

            Initialize();
            mContext = context;

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

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

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

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

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

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

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

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

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

            TXT_RESIDENCEINFO_DONG = new WpadTextView(context, false, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.DONG_INFO).W, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.DONG_INFO).H, Gravity.CENTER, Color.WHITE, nFONTSIZE_DONGHO[ConfigValue.DEVICE_MODELTYPE], true, ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.DONG_INFO);
            ViewRegistration(layout, TXT_RESIDENCEINFO_DONG, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.DONG_INFO).X, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.DONG_INFO).Y);

            IMG_RESIDENCE_DONG = new WpadImageView(context, 0, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_DONG).W, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_DONG).H, DongImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_DONG);
            ViewRegistration(layout, IMG_RESIDENCE_DONG, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_DONG).X, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_DONG).Y);

            TXT_RESIDENCEINFO_HO = new WpadTextView(context, false, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.HO_INFO).W, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.HO_INFO).H, Gravity.CENTER, Color.WHITE, nFONTSIZE_DONGHO[ConfigValue.DEVICE_MODELTYPE], true, ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.HO_INFO);
            ViewRegistration(layout, TXT_RESIDENCEINFO_HO, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.HO_INFO).X, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.HO_INFO).Y);

            IMG_RESIDENCE_HO = new WpadImageView(context, 0, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_HO).W, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_HO).H, HoImages[ConfigValue.DEVICE_MODELTYPE], 0, ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_HO);
            ViewRegistration(layout, IMG_RESIDENCE_HO, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_HO).X, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_HO).Y);

            TXT_INSTRUCTION = new WpadTextView(context, false, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.INSTRUCTION).W, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.INSTRUCTION).H, Gravity.CENTER, Color.WHITE, nFONTSIZE_INSTRUCTION[ConfigValue.DEVICE_MODELTYPE], false, ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.INSTRUCTION);
            ViewRegistration(layout, TXT_INSTRUCTION, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.INSTRUCTION).X, getPosition(ID.SCREENS.ACCESSRESIDENCEPW.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] AccessResidencePW(...) : " + 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 {
            setCallLevel();
            initCallVariables();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize() : " + e);
            e.printStackTrace();
        }
    }

    @Override
    protected void onClose() {
        try {
            LOG("------------------ [onClose - AccessResidencePW] -------------------");
            RemoteIDInfo.InitVariables();
            Global.strInputPW = "";
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onClose(...) : " + e);
            e.printStackTrace();
        }
    }

    @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.TargetInputStep == TARGETINFOINPUTSTEP.DONG) {
                    RemoteIDInfo.strDong += Integer.toString(keyCode - 7);

                    if (RemoteIDInfo.strDong.length() > MAXLENGTH_DONG) {
                        RemoteIDInfo.strDong = RemoteIDInfo.strDong.substring(1, 5);
                    }
                } else if (Global.TargetInputStep == TARGETINFOINPUTSTEP.HO) {
                    RemoteIDInfo.strHo += Integer.toString(keyCode - 7);

                    if (RemoteIDInfo.strHo.length() > MAXLENGTH_HO) {
                        RemoteIDInfo.strHo = RemoteIDInfo.strHo.substring(1, 5);
                    }
                } else if (Global.TargetInputStep == TARGETINFOINPUTSTEP.DONE) {
                    if (Integer.valueOf(Global.strInputPW.length()) < 4) {
                        Global.strInputPW += Integer.toString(keyCode - 7);
                    }
                }
            } else if (keyCode == Declare.Button.STAR) {
                if (Global.TargetInputStep == TARGETINFOINPUTSTEP.DONG) {
                    if (RemoteIDInfo.strDong.length() > 2) {
                        Global.TargetInputStep = TARGETINFOINPUTSTEP.HO;
                    } else {
                        initCallVariables();
                        MainActivity.mSound.Play_lobby(R.raw.retryinput);
                    }
                } else if (Global.TargetInputStep == TARGETINFOINPUTSTEP.HO) {
                    if (RemoteIDInfo.strHo.length() > 2) {
                        Global.TargetInputStep = TARGETINFOINPUTSTEP.DONE;
                    } else {
                        initCallVariables();
                        MainActivity.mSound.Play_lobby(R.raw.retryinput);
                    }
                }
            } else if (keyCode == Declare.Button.SHARP) {
                if (bFUNCBlock) return;

                if (Global.TargetInputStep == TARGETINFOINPUTSTEP.HO) {
                    if (ConfigValue.eHouseDivision == SetValue.TRUE) {
                        RemoteIDInfo.bRentRoomSelected = true;
                        RemoteIDInfo.nHo_Extension = 1;
                        Global.TargetInputStep = TARGETINFOINPUTSTEP.DONE;
                    }
                } else if (Global.TargetInputStep == TARGETINFOINPUTSTEP.DONE) {
                    if (Global.strInputPW.length() != 4) {
                        LOG_INFO("[onKeyDown] The length of Global.strInputPW is not 4!!!");
                        MainActivity.mSound.Play_lobby(R.raw.retryinput);
                        updateScreen();
                        Global.ctrlLobbyDoorNMent(false, false, 0, false, false);
                        if (RemoteIDInfo.bRentRoomSelected)
                            LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.fail, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RENTRESIDENCE, RemoteIDInfo.strDong, RemoteIDInfo.strHo), "null", 0, 0, 0, 0, Global.strInputPW);
                        else
                            LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.fail, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RESIDENCE, RemoteIDInfo.strDong, RemoteIDInfo.strHo), "null", 0, 0, 0, 0, Global.strInputPW);
                        Global.strInputPW = "";
                        return;
                    }

//                    if (Global.strInputPW.equals(Global.ZeroPadding_Head(4, RemoteIDInfo.strHo))) {
//                        LOG_INFO("[onKeyDown] Global.strInputPW and RemoteIDInfo.strHo are same!!!");
//                        MainActivity.mSound.Play_lobby(R.raw.retryinput);
//                        updateScreen();
//                        Global.ctrlLobbyDoorNMent(false, false, 0, false, false);
//                        if (RemoteIDInfo.bRentRoomSelected)
//                            LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.fail, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RENTRESIDENCE, RemoteIDInfo.strDong, RemoteIDInfo.strHo), "null", 0, 0, 0, 0, Global.strInputPW);
//                        else
//                            LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.fail, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RESIDENCE, RemoteIDInfo.strDong, RemoteIDInfo.strHo), "null", 0, 0, 0, 0, Global.strInputPW);
//                        Global.strInputPW = "";
//                        return;
//                    }
//
//                    for (int i = 0; i < Global.astrBlockedPW.length; i++) {
//                        if (Global.astrBlockedPW[i].equals(Global.strInputPW)) {
//                            LOG_INFO("[onKeyDown] Global.strInputPW is prohibited!!!");
//                            MainActivity.mSound.Play_lobby(R.raw.retryinput);
//                            updateScreen();
//                            Global.ctrlLobbyDoorNMent(false, false, 0, false, false);
//                            if (RemoteIDInfo.bRentRoomSelected)
//                                LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.fail, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RENTRESIDENCE, RemoteIDInfo.strDong, RemoteIDInfo.strHo), "null", 0, 0, 0, 0, Global.strInputPW);
//                            else
//                                LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.fail, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RESIDENCE, RemoteIDInfo.strDong, RemoteIDInfo.strHo), "null", 0, 0, 0, 0, Global.strInputPW);
//                            Global.strInputPW = "";
//                            return;
//                        }
//                    }

                    if (ConfigValue.eCallLevel == SetValue.Level_Line) {
                        if (Global.isExistResi(Integer.parseInt(RemoteIDInfo.strHo))) {
                            if (!send_check_lobby_passwd_request(RemoteIDInfo.strDong, RemoteIDInfo.strHo, Global.strInputPW, RemoteIDInfo.bRentRoomSelected)) {
                                MainActivity.mSound.Play_lobby(R.raw.trylater);
                                super.setChangeScreen(ID.SCREENS.Main);
                            }
                        } else {
                            MainActivity.mSound.Play_lobby(R.raw.confirmsetting);
                            super.setChangeScreen(ID.SCREENS.Main);
                        }
                    } else {
                        if (!send_check_lobby_passwd_request(RemoteIDInfo.strDong, RemoteIDInfo.strHo, Global.strInputPW, RemoteIDInfo.bRentRoomSelected)) {
                            MainActivity.mSound.Play_lobby(R.raw.trylater);
                            super.setChangeScreen(ID.SCREENS.Main);
                        }
                    }
                } else {
                    MainActivity.mSound.Play_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] Start!!");
            if (RemoteIDInfo.strDong != "") {
                // 첫자리에 "0"이 출력되지 않도록 숫자로 변경하면서 "0"을 제거하고, 다시 String으로 변환한다.
                RemoteIDInfo.nDong = Integer.parseInt(RemoteIDInfo.strDong);
                RemoteIDInfo.strDong = Integer.toString(RemoteIDInfo.nDong);
            }

            if (RemoteIDInfo.strHo != "") {
                // 첫자리에 "0"이 출력되지 않도록 숫자로 변경하면서 "0"을 제거하고, 다시 String으로 변환한다.
                RemoteIDInfo.nHo = Integer.parseInt(RemoteIDInfo.strHo);
                RemoteIDInfo.strHo = Integer.toString(RemoteIDInfo.nHo);
            }

            if (Global.TargetInputStep == TARGETINFOINPUTSTEP.DONG)
                strTXT_INSTRUCTION = TextResources.strInputResidenceInfo;
            else if (Global.TargetInputStep == TARGETINFOINPUTSTEP.HO)
                strTXT_INSTRUCTION = TextResources.strInputResidenceInfo;
            else if (Global.TargetInputStep == TARGETINFOINPUTSTEP.DONE)
                strTXT_INSTRUCTION = TextResources.strInputResidencePW;

            TXT_RESIDENCEINFO_DONG.setText(RemoteIDInfo.strDong);

            String strDisplayHo = "";
            if (RemoteIDInfo.bRentRoomSelected) strDisplayHo = RemoteIDInfo.strHo + "-" + RemoteIDInfo.nHo_Extension;
            else strDisplayHo = RemoteIDInfo.strHo;
            Log.w(TAG, "[updateScreen] strDisplayHo [" + strDisplayHo + "], strDisplayHo.length() [" + strDisplayHo.length() + "]");
            if (strDisplayHo.length() > MAXLENGTH_HO) {
                Log.w(TAG, "[updateScreen] ========== 1 ==========");
                TXT_RESIDENCEINFO_HO.setTextSize(0, (float) nFONTSIZE_DONGHO_RENT[ConfigValue.DEVICE_MODELTYPE]);
            }
            else {
                Log.w(TAG, "[updateScreen] ========== 2 ==========");
                TXT_RESIDENCEINFO_HO.setTextSize(0, (float) nFONTSIZE_DONGHO[ConfigValue.DEVICE_MODELTYPE]);
            }

            TXT_RESIDENCEINFO_HO.setText(strDisplayHo);
            TXT_INSTRUCTION.setText(strTXT_INSTRUCTION);

            int nPWLength = 0;
            if (Global.strInputPW != "") {
                Log.d(TAG, "[updateScreen] Global.strInputPW = " + 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.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.ACCESSRESIDENCEPW.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.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.RESIDENCEPW_INPUT_PW_01:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(160, 142, 105, 135);
                    else position.SetPosition(68, 95, 74, 94);
                    return position;

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

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

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

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

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

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

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

                case ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.DONG_INFO:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(225, 57, 126, 61);
                    else position.SetPosition(114, 41, 75, 50);
                    return position;

                case ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_DONG:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(357, 85, 37, 38);
                    else position.SetPosition(191, 45, 36, 45);
                    return position;

                case ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.TEXTS.HO_INFO:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(402, 57, 126, 61);
                    else position.SetPosition(249, 41, 75, 50);
                    return position;

                case ID.SCREENS.ACCESSRESIDENCEPW.ELEMENTS.IMAGES.IMG_HO:
                    if (ConfigValue.DEVICE_MODELTYPE == Global.MODEL_ILP_700) position.SetPosition(534, 85, 37, 38);
                    else position.SetPosition(326, 45, 36, 45);
                    return position;

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

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

    private boolean send_check_lobby_passwd_request(final String strDong, final String strHo, final String strPW, final boolean bRentHouse) {
        try {
            Request_check_lobby_passwd check_lobby_passwd = new Request_check_lobby_passwd(mContext);
            if (check_lobby_passwd.send_check_lobby_passwd_request(strDong, strHo, strPW, bRentHouse)) {
                bFUNCBlock = true;
                check_lobby_passwd.setResultCtrl(new iMAPResultCtrl() {

                    @Override
                    public void onResult(iMAPResult eResult) {
                        Log.d(TAG, "[send_check_lobby_passwd_request] (onResult) eResult =" + eResult);
                        if (eResult == iMAPResult.OK) {
                            if (ConfigValue.eParcelLink == SetValue.TRUE) {
                                Global.ctrlLobbyDoorNMent(true, false, ConfigValue.nDoorOpenDuration, false, false);
                                if (bRentHouse)
                                    LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.pass, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RENTRESIDENCE, strDong, strHo), "null", 0, 0, 0, 0, "null");
                                else
                                    LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.pass, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RESIDENCE, strDong, strHo), "null", 0, 0, 0, 0, "null");
                            } else {
                                Global.ctrlLobbyDoorNMent(true, true, ConfigValue.nDoorOpenDuration, false, false);
                                if (bRentHouse)
                                    LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.pass, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RENTRESIDENCE, strDong, strHo), "null", 0, 0, 0, 0, "null");
                                else
                                    LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.pass, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RESIDENCE, strDong, strHo), "null", 0, 0, 0, 0, "null");
                            }
                            Global.sendElevCallRequest(strDong, strHo, true);
                        } else if (eResult == iMAPResult.ResultFail) {
                            Global.ctrlLobbyDoorNMent(false, true, 0, false, false);
                            if (bRentHouse)
                                LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.fail, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RENTRESIDENCE, strDong, strHo), "null", 0, 0, 0, 0, strPW);
                            else
                                LobbyAccessCtrl.sendLobbyAccessLog(AccessValues.fail, AccessValues.passwd, EthernetCtrl.makeIPAddress(DeviceType.RESIDENCE, strDong, strHo), "null", 0, 0, 0, 0, strPW);
                            bFUNCBlock = false;
                        } else {
                            MainActivity.mSound.Play_lobby(R.raw.trylater);
                            bFUNCBlock = false;
                        }
                        ((MainActivity) mContext).setChangeScreen(ID.SCREENS.Main, false);
                    }
                });
                return true;
            }
            bFUNCBlock = false;
            return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_check_lobby_passwd_request(strDong, strHo, strPW)");
            e.printStackTrace();
            bFUNCBlock = false;
            return false;
        }
    }

    private void setCallLevel() {
        try {
            if (ConfigValue.eCallLevel == SetValue.Level_Line || ConfigValue.eCallLevel == SetValue.Level_Dong) {
                Global.TargetInputStep = TARGETINFOINPUTSTEP.HO;
                RemoteIDInfo.strDong = ConfigValue.strDevice_Dong;
            } else if (ConfigValue.eCallLevel == SetValue.Level_Entire) {
                Global.TargetInputStep = TARGETINFOINPUTSTEP.DONG;
            } else {
                Global.TargetInputStep = TARGETINFOINPUTSTEP.HO;
                RemoteIDInfo.strDong = ConfigValue.strDevice_Dong;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setCallLevel()");
            e.printStackTrace();
        }
    }

    private void initCallVariables() {
        try {
            RemoteIDInfo.InitVariables();
            setCallLevel();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initCallVariables()");
            e.printStackTrace();
        }
    }

    class TextResources {
        static final String strInputResidenceInfo = "세대정보를 입력하세요.";
        static final String strInputResidencePW = "세대출입 비밀번호를 입력하세요.";
    }

}