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.DataClasses.POSITION;
import kr.co.icontrols.ilp300_700.global.Declare;
import kr.co.icontrols.ilp300_700.global.Global;
import kr.co.icontrols.ilp300_700.global.ID;
import kr.co.icontrols.ilp300_700.ifconverter.SmartAutoDoorIFManager.RS485EventListener;
import kr.co.icontrols.wallpadsupport.WpadImageView;
import kr.co.icontrols.wallpadsupport.WpadScreen;
import kr.co.icontrols.wallpadsupport.WpadTextView;

public class AdminSet_Test_RS485 extends WpadScreen implements RS485EventListener {

    String TAG = "AdminSet_Test_RS485";

    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;
    int TextColor_Normal = Color.WHITE;
    int TextColor_Highlight = Color.GREEN;
    int nFontSize_menu = 28;
    int nFontSize_rcv = 20;
    boolean bFontNonBold = false;
    boolean bFontBold = true;
    boolean bReadtToRCV = false;
    String strRcvAutoendRun = "2] 수신(자동종료 실행중)";
    String strRcvAutoEndStop = "2] 수신(자동종료 멈춤)";
    String strText_Title = "RS-485 통신 시험";
    String strText_Line_01_Item = "1] 송신";
    String strText_Line_02_Item = strRcvAutoendRun;
    String strText_Line_03_Item = "";
    public Handler MainHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message msg) {
            LOG("[MainHandler] what [" + msg.what + "], arg1 [" + msg.arg1 + "], arg2 [" + msg.arg2 + "], obj [" + msg.obj + "]");
            switch (msg.what) {
                case HANDLERMSG.RCVD_DATA:
                    strText_Line_03_Item = (String) msg.obj;
                    Log.i(TAG, "[on485RcvEvent] strText_Line_03_Item [" + strText_Line_03_Item + "]");
                    Text_Line_03_Item.setText(strText_Line_03_Item);
                    break;

                default:
                    LOG("MainHandler : " + msg.what);
                    break;
            }
        }
    };
    public AdminSet_Test_RS485(Context context, RelativeLayout layout) {
        super(context, layout);
        try {
            LOG("------------------ [START - AdminSet_Test_RS485] -------------------");

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

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

            Text_Line_01_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_01_Item).W, getPosition(ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_01_Item).H, Gravity.LEFT, Color.WHITE, nFontSize_menu, bFontNonBold, ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_01_Item);
            ViewRegistration(layout, Text_Line_01_Item, getPosition(ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_01_Item).X, getPosition(ID.SCREENS.ADMINSET_TEST_RS485.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_RS485.ELEMENTS.TEXTS.LINE_02_Item).W, getPosition(ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_02_Item).H, Gravity.LEFT, Color.WHITE, nFontSize_menu, bFontNonBold, ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_02_Item);
            ViewRegistration(layout, Text_Line_02_Item, getPosition(ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_02_Item).X, getPosition(ID.SCREENS.ADMINSET_TEST_RS485.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_RS485.ELEMENTS.TEXTS.LINE_03_Item).W, getPosition(ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_03_Item).H, Gravity.CENTER, Color.CYAN, nFontSize_rcv, bFontNonBold, ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_03_Item);
            ViewRegistration(layout, Text_Line_03_Item, getPosition(ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_03_Item).X, getPosition(ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_03_Item).Y);
            Text_Line_03_Item.setText(strText_Line_03_Item);

            ((MainActivity) mContext).setAutoScreenChange(ID.SCREENS.Main, MainActivity.nAdminScreenTimeout);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] AdminSet_Test_RS485(Context context, RelativeLayout layout)");
            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 =====");
            ((MainActivity) mContext).mSmartAutoDoorIFManager.setRS485EventListener(this);
            bReadtToRCV = false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize()");
            e.printStackTrace();
        }
    }

    @Override
    protected void onClose() {
        try {
            LOG("------------------ [onClose - AdminSet_Test_RS485] -------------------");
            ((MainActivity) mContext).mSmartAutoDoorIFManager.releaseRS485EventListener();
        } 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(Intent intent)");
            e.printStackTrace();
        }
        super.onBrReceive(intent);
    }

    @Override
    protected void onKeyDown(int keyCode, KeyEvent event) {
        try {
//            LOG"[onKeyDown] keyCode [" + keyCode + "], event [" + event + "]");
            if (keyCode == Declare.Button.NUMBER_01) {
                ((MainActivity) mContext).mSmartAutoDoorIFManager.sendSamplePacketMsg();
            } else if (keyCode == Declare.Button.NUMBER_02) {
                if (bReadtToRCV) {
                    bReadtToRCV = false;
                    ((MainActivity) mContext).setAutoScreenChange(ID.SCREENS.Main, MainActivity.nAdminScreenTimeout);
                    strText_Line_02_Item = strRcvAutoendRun;
                    Text_Line_02_Item.setText(strText_Line_02_Item);
                    Text_Line_02_Item.setTextColor(Color.WHITE);
                } else {
                    bReadtToRCV = true;
                    ((MainActivity) mContext).cancelAutoScreenChange();
                    strText_Line_02_Item = strRcvAutoEndStop;
                    Text_Line_02_Item.setText(strText_Line_02_Item);
                    Text_Line_02_Item.setTextColor(Color.GREEN);
                }
            } 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(int keyCode, KeyEvent event)");
            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_RS485.ELEMENTS.IMAGES.BACKGROUND:
                    position.SetPosition(0, 0, 480, 272);
                    return position;

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

                case ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_01_Item:
                    position.SetPosition(30, 84, 420, 40);
                    return position;

                case ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_02_Item:
                    position.SetPosition(30, 134, 420, 40);
                    return position;

                case ID.SCREENS.ADMINSET_TEST_RS485.ELEMENTS.TEXTS.LINE_03_Item:
                    position.SetPosition(30, 184, 420, 40);
                    return position;

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

    @Override
    public void on485RcvEvent(byte[] hData) {
        try {
            MainActivity.mSound.Play_lobby(R.raw.resultok);
            String strData = Global.printHexs(hData);
            Log.i(TAG, "[on485RcvEvent] strData [" + strData + "]");
            sendHandlerMsg(HANDLERMSG.RCVD_DATA, 0, 0, strData);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] on485RcvEvent(byte[] hData)");
            e.printStackTrace();
        }
    }

    public void sendHandlerMsg(int what, int arg1, int arg2, Object obj) {
        try {
            LOG("[sendHandlerMsg] what [" + what + "], arg1 [" + arg1 + "], arg2 [" + arg2 + "], obj [" + obj + "]");
            Message HandleMsg = MainHandler.obtainMessage();
            HandleMsg.what = what;
            HandleMsg.arg1 = arg1;
            HandleMsg.arg2 = arg2;
            HandleMsg.obj = obj;
            MainHandler.sendMessage(HandleMsg);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] sendHandlerMsg(int what, int arg1, int arg2, Object obj)");
            e.printStackTrace();
        }
    }

    public void sendHandlerMsgDelayed(int what, int arg1, int arg2, Object obj, long delay) {
        try {
            LOG("[sendHandlerMsgDelayed] what [" + what + "], arg1 [" + arg1 + "], arg2 [" + arg2 + "], obj [" + obj + "], delay [" + delay + "]");
            Message HandleMsg = MainHandler.obtainMessage();
            HandleMsg.what = what;
            HandleMsg.arg1 = arg1;
            HandleMsg.arg2 = arg2;
            HandleMsg.obj = obj;
            MainHandler.sendMessageDelayed(HandleMsg, delay);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] sendHandlerMsgDelayed(int what, int arg1, int arg2, Object obj, long delay)");
            e.printStackTrace();
        }
    }

    public final class HANDLERMSG {
        public final static int RCVD_DATA = 100;
    }


}