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

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
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.Configure.SetValue;
import kr.co.icontrols.ilp300_700.global.DataClasses.MultiReaderKeyInfo;
import kr.co.icontrols.ilp300_700.global.DataClasses.POSITION;
import kr.co.icontrols.ilp300_700.global.DataClasses.STDReaderKeyInfo;
import kr.co.icontrols.ilp300_700.global.Declare;
import kr.co.icontrols.ilp300_700.global.Declare.MultiReaderPacket;
import kr.co.icontrols.ilp300_700.global.Global;
import kr.co.icontrols.ilp300_700.global.ID;
import kr.co.icontrols.ilp300_700.rfreader.HDCIPARKManager;
import kr.co.icontrols.ilp300_700.rfreader.HDCStandardManager;
import kr.co.icontrols.ilp300_700.rfreader.RFReaderManager;
import kr.co.icontrols.wallpadsupport.WpadImageView;
import kr.co.icontrols.wallpadsupport.WpadScreen;
import kr.co.icontrols.wallpadsupport.WpadTextView;

public class AdminSet_Test_RFReader_Read extends WpadScreen {

    String TAG = "AdminSet_Test_RFReader_Read";

    boolean bLOG = true;   // true, false
    boolean bLOG_DEBUG = true;   // 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 nFontSize_Title = 28;
    int nFontSize_Instruction = 24;
    boolean bFontNonBold = false;
    boolean bFontBold = true;
    String strText_Title = "카드읽기";
    String strText_Line_01_Item = "";
    String strText_Line_02_Item = "키를 인식시키세요.";
    String strText_Line_03_Item = "";
    public Handler RFKeyReadTestHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message msg) {
            if (!Global.getEnableDeviceInput()) {
                Log.w(TAG, "[RFKeyReadTestHandler] Global.getEnableDeviceInput() = " + Global.getEnableDeviceInput());
                return;
            }
            ((MainActivity) mContext).extendAutoScreenChangeTime();
            MainActivity.mSound.Play_lobby(R.raw.resultok);
            LOG_INFO("RFKeyReadTestHandler : " + msg.what);
            if (ConfigValue.eCardReaderType == SetValue.CardReaderType_Standard) {
                // 멀티리더, 스마트폰 원패스 등
                if (msg.what != RFReaderManager.HANDLE_RFREADER_STANDDARD) {
                    MainActivity.mSound.Play_lobby(R.raw.confirmsetting);
                    return;
                }
                MultiReaderKeyInfo KeyInfo = (MultiReaderKeyInfo) msg.obj;
                displyRFKeyInfoMulti(KeyInfo);
            }
            else {
                if (msg.what != RFReaderManager.HANDLE_RFREADER_HDCIPARK) {
                    MainActivity.mSound.Play_lobby(R.raw.confirmsetting);
                    return;
                }
                STDReaderKeyInfo KeyInfo = (STDReaderKeyInfo) msg.obj;
                displyRFKeyInfoSTD(KeyInfo);
            }
        }
    };
    String strText_Line_04_Item = "";
    String strInfoMent_NotSupported = "지원하지 않는 기기입니다.";
    String strInfoMent_PleaseWait = "잠시만 기다리세요.";
    String strInfoMent_ResultOK = "결과 : 성공";
    String strInfoMent_ResultFail = "결과 : 실패";
    public AdminSet_Test_RFReader_Read(Context context, RelativeLayout layout) {
        super(context, layout);
        try {
            LOG("------------------ [START - AdminSet_Test_RFReader_Read] -------------------");

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

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

            Text_Line_01_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_01_Item).W, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_01_Item).H, Gravity.CENTER, Color.GREEN, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_01_Item);
            ViewRegistration(layout, Text_Line_01_Item, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_01_Item).X, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.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_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_02_Item).W, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_02_Item).H, Gravity.CENTER, Color.GREEN, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_02_Item);
            ViewRegistration(layout, Text_Line_02_Item, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_02_Item).X, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.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_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_03_Item).W, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_03_Item).H, Gravity.CENTER, Color.GREEN, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_03_Item);
            ViewRegistration(layout, Text_Line_03_Item, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_03_Item).X, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.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_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_04_Item).W, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_04_Item).H, Gravity.CENTER, Color.GREEN, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_04_Item);
            ViewRegistration(layout, Text_Line_04_Item, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_04_Item).X, getPosition(ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_04_Item).Y);
            Text_Line_04_Item.setText(strText_Line_04_Item);

            if (Global.bSmartKeyTestMode) ((MainActivity) mContext).cancelAutoScreenChange();
            else ((MainActivity) mContext).setAutoScreenChange(ID.SCREENS.Main, MainActivity.nAdminScreenTimeout);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] AdminSet_Test_RFReader_Read(...) : " + 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 =====");
            setKeyTestHandler();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize() : " + e);
            e.printStackTrace();
        }
    }

    @Override
    protected void onClose() {
        try {
            LOG("------------------ [onClose - AdminSet_Test_RFReader_Read] -------------------");
            releaseKeyTestHandler();
        } 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.CANCEL || keyCode == Declare.Button.TACT_CANCEL) {
                super.setChangeScreen(ID.SCREENS.AdminSet_Test_RFReader);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onKeyDown(...) : " + e);
            e.printStackTrace();
        }
        super.onKeyDown(keyCode, event);
    }

    public POSITION getPosition(int nID) {
//        LOG("[getPosition] ===== START =====");
        POSITION position = new POSITION();
        position.SetPosition(0, 0, 480, 272);
        try {
            switch (nID) {
                case ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.IMAGES.BACKGROUND:
                    position.SetPosition(0, 0, 480, 272);
                    return position;

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

                case ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_01_Item:
                    position.SetPosition(0, 73, 480, 40);
                    return position;

                case ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_02_Item:
                    position.SetPosition(0, 122, 480, 40);
                    return position;

                case ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_03_Item:
                    position.SetPosition(0, 171, 480, 40);
                    return position;

                case ID.SCREENS.ADMINSET_TEST_RFREADER_READ.ELEMENTS.TEXTS.LINE_04_Item:
                    position.SetPosition(0, 220, 480, 40);
                    return position;

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

    private void setKeyTestHandler() {
        try {
            LOG("[setKeyTestHandler] ===== START =====");
            if (ConfigValue.eCardReaderType == SetValue.CardReaderType_Standard) {
                HDCStandardManager.setHandler(RFKeyReadTestHandler);
            }
            else  {
                HDCIPARKManager.setHandler(RFKeyReadTestHandler);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setKeyTestHandler() : " + e);
            e.printStackTrace();
        }
    }

    private void releaseKeyTestHandler() {
        try {
            LOG("[releaseKeyTestHandler] ===== START =====");
            if (ConfigValue.eCardReaderType == SetValue.CardReaderType_Standard) {
                HDCStandardManager.releaseHandler();
            }
            else {
                HDCIPARKManager.releaseHandler();
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] releaseKeyTestHandler() : " + e);
            e.printStackTrace();
        }
    }

    private void displyRFKeyInfoSTD(STDReaderKeyInfo KeyInfo) {
        try {
            LOG("[displyRFKeyInfoSTD] ===== START =====");
            strText_Line_02_Item = "RF Type [ I-PARK ]";
            Text_Line_02_Item.setGravity(Gravity.CENTER);
            Text_Line_02_Item.setText(strText_Line_02_Item);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] displyRFKeyInfoSTD(KeyInfo)");
            e.printStackTrace();
        }
    }

    private void displyRFKeyInfoMulti(MultiReaderKeyInfo KeyInfo) {
        try {
            LOG("[displyRFKeyInfoMulti] ===== START =====");
            KeyInfo.printValue();

            switch (KeyInfo.getCMD()) {
                case MultiReaderPacket.CMD_IPARK:
                    String strKeyType = "";   // 0x00 : 입주자 카드, 0x01 : 마스터 카드, 0x02 : 방문자 카드
                    if (KeyInfo.getIDType() == 0x00) strKeyType = "Resident";
                    else if (KeyInfo.getIDType() == 0x01) strKeyType = "Admin";
                    else if (KeyInfo.getIDType() == 0x02) strKeyType = "Visitor";
                    strText_Line_02_Item = "RF Type [ I-PARK ]";
                    strText_Line_03_Item = "KeyInfo is not displayed for security.";
//                strText_Line_01_Item = "RF Type [ I-PARK ], KeyType [ " + strKeyType + " ]";
//                strText_Line_02_Item = "Sitecode [ " + KeyInfo.getSitecode() + " ]";
//                strText_Line_03_Item = "Dong [ " + KeyInfo.getDong() + "], Ho [ " + KeyInfo.getHo() + " ]";
//                strText_Line_04_Item = "KeyNo [ " + KeyInfo.getKeyNo() + "], IssueCnt [ " + KeyInfo.getIssueCnt() +" ]";
                    break;

                case MultiReaderPacket.CMD_SMARTKEY:
                    strText_Line_02_Item = "RF Type [ SMARTKEY ]";
                    strText_Line_03_Item = "KeyInfo is not displayed for security.";
//                strText_Line_02_Item = "Serial No. [ " + KeyInfo.getSN() + " ]";
//                strText_Line_03_Item = "";
//                strText_Line_04_Item = "";
                    break;

                case MultiReaderPacket.CMD_MIFARE:
                    strText_Line_02_Item = "RF Type [ MIFARE ]";
                    strText_Line_03_Item = "KeyInfo is not displayed for security.";
                    break;

                case MultiReaderPacket.CMD_TMONEY:
                    strText_Line_02_Item = "RF Type [ T-MONEY ]";
                    strText_Line_03_Item = "KeyInfo is not displayed for security.";
//                strText_Line_02_Item = "Serial No. [ " + KeyInfo.getSN() + " ]";
//                strText_Line_03_Item = "";
//                strText_Line_04_Item = "";
                    break;
            }
            Text_Line_02_Item.setText(strText_Line_02_Item);
            Text_Line_03_Item.setText(strText_Line_03_Item);
//            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] displyRFKeyInfoSTD(KeyInfo)");
            e.printStackTrace();
        }
    }


}