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

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.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.Declare;
import kr.co.icontrols.ilp300_700.global.ID;
import kr.co.icontrols.ilp300_700.imap.iMAPResultCtrl;
import kr.co.icontrols.ilp300_700.rfmsg.Request_MultiRF_FWVer;
import kr.co.icontrols.ilp300_700.rfmsg.Request_MultiRF_Status;
import kr.co.icontrols.wallpadsupport.WpadImageView;
import kr.co.icontrols.wallpadsupport.WpadScreen;
import kr.co.icontrols.wallpadsupport.WpadTextView;

public class AdminSet_Test_RFReader extends WpadScreen {

    String TAG = "AdminSet_Test_RFReader";

    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_Instruction = 28;
    boolean bFontNonBold = false;
    boolean bFontBold = true;
    String strText_Title = "카드리더";
    String strText_Line_01_Item = "1] 통신 상태";
    String strText_Line_02_Item = "2] 버전 정보";
    String strText_Line_03_Item = "3] 카드 읽기";
    String strText_Line_04_Item = "항목을 선택하세요.";
    String strInfoMent_NotSupported = "지원하지 않는 기기입니다.";
    String strInfoMent_PleaseWait = "잠시만 기다리세요.";
    String strInfoMent_ResultOK = "결과 : 성공";
    String strInfoMent_ResultFail = "결과 : 실패";
    boolean bBTNBlock = false;

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

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

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

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

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

    @Override
    protected void onClose() {
        try {
            LOG("------------------ [onClose - AdminSet_Test_RFReader] -------------------");
        } 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 (bBTNBlock) return;

            if (keyCode == Declare.Button.NUMBER_01) {
                // 카드리더 통신 시험
                Text_Line_04_Item.setTextColor(Color.YELLOW);
                Text_Line_04_Item.setText(strInfoMent_PleaseWait);
                if (ConfigValue.eCardReaderType != SetValue.CardReaderType_Standard) {
                    // 지원하지 않는 경우
                    MainActivity.mSound.Play_lobby(R.raw.confirmsetting);
                    Text_Line_04_Item.setTextColor(Color.RED);
                    Text_Line_04_Item.setText(strInfoMent_NotSupported);
                    return;
                }
                if (!send_multirf_status_request()) {
                    MainActivity.mSound.Play_lobby(R.raw.retryinput);
                    Text_Line_04_Item.setTextColor(Color.RED);
                    Text_Line_04_Item.setText(strInfoMent_ResultFail);
                }
            } else if (keyCode == Declare.Button.NUMBER_02) {
                // 카드리더 버전정보
                Text_Line_04_Item.setTextColor(Color.YELLOW);
                Text_Line_04_Item.setText(strInfoMent_PleaseWait);
                if (ConfigValue.eCardReaderType != SetValue.CardReaderType_Standard) {
                    // 지원하지 않는 경우
                    MainActivity.mSound.Play_lobby(R.raw.confirmsetting);
                    Text_Line_04_Item.setTextColor(Color.RED);
                    Text_Line_04_Item.setText(strInfoMent_NotSupported);
                    return;
                }
                if (!send_multirf_fwver_request()) {
                    MainActivity.mSound.Play_lobby(R.raw.retryinput);
                    Text_Line_04_Item.setTextColor(Color.RED);
                    Text_Line_04_Item.setText(strInfoMent_ResultFail);
                }
            } else if (keyCode == Declare.Button.NUMBER_03) {
                // 카드 읽기
                super.setChangeScreen(ID.SCREENS.AdminSet_Test_RFReader_Read);
            } else if (keyCode == Declare.Button.CANCEL || keyCode == Declare.Button.TACT_CANCEL) {
                super.setChangeScreen(ID.SCREENS.AdminSet_Test);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onKeyDown(...) : " + e);
            e.printStackTrace();
        }
        super.onKeyDown(keyCode, event);
    }

    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_TEST_RFREADER.ELEMENTS.IMAGES.BACKGROUND:
                    position.SetPosition(0, 0, 480, 272);
                    return position;

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

                case ID.SCREENS.ADMINSET_TEST_RFREADER.ELEMENTS.TEXTS.LINE_01_Item:
                    position.SetPosition(30, 73, 450, 40);
                    return position;

                case ID.SCREENS.ADMINSET_TEST_RFREADER.ELEMENTS.TEXTS.LINE_02_Item:
                    position.SetPosition(30, 122, 450, 40);
                    return position;

                case ID.SCREENS.ADMINSET_TEST_RFREADER.ELEMENTS.TEXTS.LINE_03_Item:
                    position.SetPosition(30, 171, 450, 40);
                    return position;

                case ID.SCREENS.ADMINSET_TEST_RFREADER.ELEMENTS.TEXTS.LINE_04_Item:
                    position.SetPosition(30, 220, 450, 40);
                    return position;

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

    private String extractFWVerInfo(String strDeviceInfo) {
        try {
            String[] strParse_01 = strDeviceInfo.split("_");
            int nModel = Integer.parseInt(strParse_01[1]);
            String[] strParse_02 = strParse_01[2].split("\\.");
            int nOver = Integer.parseInt(strParse_02[0]);
            int nUnder = Integer.parseInt(strParse_02[1]);
            String strFWVer = Integer.toString(nOver) + "." + Integer.toString(nUnder);
            LOG("[extractFWVerInfo] nModel [" + nModel + "], strFWVer [" + strFWVer + "]");
            return "Model : " + Integer.toString(nModel) + ", FW Ver. : " + strFWVer;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] extractFWVerInfo(strFWVer)");
            e.printStackTrace();
            return null;
        }
    }

    private boolean send_multirf_status_request() {
        try {
            bBTNBlock = true;
            final Request_MultiRF_Status multirf_status = new Request_MultiRF_Status(mContext);
            if (multirf_status.send_multirf_status_request()) {
                multirf_status.setResultCtrl(new iMAPResultCtrl() {

                    @Override
                    public void onResult(iMAPResult eResult) {
                        LOG_INFO("[send_multirf_status_request] (onResult) eResult =" + eResult);
                        MainActivity.mSound.Play_lobby(R.raw.resultok);
                        if (eResult == iMAPResult.OK) {
                            Text_Line_04_Item.setTextColor(Color.GREEN);
                            Text_Line_04_Item.setText(strInfoMent_ResultOK);
                            MainActivity.showRFReaderErrorImage(false);
                        } else {
                            Text_Line_04_Item.setTextColor(Color.RED);
                            Text_Line_04_Item.setText(strInfoMent_ResultFail);
                            MainActivity.showRFReaderErrorImage(true);
                        }
                        bBTNBlock = false;
                    }
                });
                return true;
            }
            return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_multirf_status_request()");
            e.printStackTrace();
            bBTNBlock = false;
            return false;
        }
    }

    private boolean send_multirf_fwver_request() {
        try {
            bBTNBlock = true;
            final Request_MultiRF_FWVer multirf_fwver = new Request_MultiRF_FWVer(mContext);
            if (multirf_fwver.send_multirf_fwver_request()) {
                multirf_fwver.setResultCtrl(new iMAPResultCtrl() {

                    @Override
                    public void onResult(iMAPResult eResult) {
                        LOG_INFO("[send_multirf_status_request] (onResult) eResult =" + eResult);
                        MainActivity.mSound.Play_lobby(R.raw.resultok);
                        if (eResult == iMAPResult.OK) {
                            Text_Line_04_Item.setTextColor(Color.GREEN);
                            Text_Line_04_Item.setText(extractFWVerInfo(multirf_fwver.strReplyResult));
                            MainActivity.showRFReaderErrorImage(false);
                        } else {
                            Text_Line_04_Item.setTextColor(Color.RED);
                            Text_Line_04_Item.setText(strInfoMent_ResultFail);
                            MainActivity.showRFReaderErrorImage(true);
                        }
                        bBTNBlock = false;
                    }
                });
                return true;
            }
            return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_multirf_fwver_request()");
            e.printStackTrace();
            bBTNBlock = false;
            return false;
        }
    }


}