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

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.global.Configure;
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.wallpadsupport.WpadImageView;
import kr.co.icontrols.wallpadsupport.WpadScreen;
import kr.co.icontrols.wallpadsupport.WpadTextView;

public class AdminSet_Setting_BLEStrength extends WpadScreen {

    String TAG = "AdminSet_Setting_BLEStrength";

    WpadImageView Image_Background;
    WpadTextView Text_Title;
    WpadTextView Text_Line_01_Input;
    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_Instruction = 22;
    boolean bFontNonBold = false;
    boolean bFontBold = true;

    String strText_Title = "BLE 출력 설정";
    
    Context mContext;

    int nBLEStrength = 0;
    int MIN_BLE_STRENGTH = 0;
    int MAX_BLE_STRENGTH = 20;

    String strText_Line_01_Input = "";
    String strText_Line_01_Item = "1: ↓                  3: ↑";
    String strText_Line_02_Item = "";

    String strText_LINE_02_ITEM_MIM = "최소값입니다.";
    String strText_LINE_02_ITEM_MAX = "최대값입니다.";
    String strText_Line_03_Item = "- BLE 출력 선택 후 '경비실'버튼을 누르세요. -";
    
    public AdminSet_Setting_BLEStrength(Context context, RelativeLayout layout) {
        super(context, layout);
        try {
            Log.d(TAG, "------------------ [START - AdminSet_Setting_BLEStrength] -------------------");

            mContext = context;
            Initialize();
            
            Image_Background = new WpadImageView(context, 0, Declare.DeviceInfo.getScreenSizeFullW(Configure.ConfigValue.DEVICE_MODELTYPE), Declare.DeviceInfo.getScreenSizeFullH(Configure.ConfigValue.DEVICE_MODELTYPE), R.drawable.bg_back, 0, ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.IMAGES.BACKGROUND);
            ViewRegistration(layout, Image_Background, 0, 0);

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

            Text_Line_01_Input = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_01_INPUT).W, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_01_INPUT).H, Gravity.CENTER, TextColor_Normal, nFontSize_Menu, bFontNonBold, ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_01_INPUT);
            ViewRegistration(layout, Text_Line_01_Input, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_01_INPUT).X, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_01_INPUT).Y);
            Text_Line_01_Input.setText(strText_Line_01_Input);

            Text_Line_01_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_01_ITEM).W, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_01_ITEM).H, Gravity.CENTER, TextColor_Normal, nFontSize_Menu, bFontNonBold, ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_01_ITEM);
            ViewRegistration(layout, Text_Line_01_Item, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_01_ITEM).X, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.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_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_02_ITEM).W, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_02_ITEM).H, Gravity.CENTER, TextColor_Highlight, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_02_ITEM);
            ViewRegistration(layout, Text_Line_02_Item, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_02_ITEM).X, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_02_ITEM).Y);
            Text_Line_02_Item.setText("");

            Text_Line_03_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_03_ITEM).W, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_03_ITEM).H, Gravity.CENTER, TextColor_Normal, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_03_ITEM);
            ViewRegistration(layout, Text_Line_03_Item, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_03_ITEM).X, getPosition(ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.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_Setting_BLEStrength(Context context, RelativeLayout layout)");
            e.printStackTrace();
        }
    }

    protected void Initialize() {
        try {
            Log.d(TAG, "[Initialize] ===== START =====");
            nBLEStrength = Configure.ConfigValue.nBLEStrength;
            strText_Line_01_Input = Integer.toString(nBLEStrength);
            Log.d(TAG, "[Initialize] strText_Line_01_Input = " + strText_Line_01_Input);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize()");
            e.printStackTrace();
        }
    }

    @Override
    protected void onClose() {
        try {
            Log.d(TAG, "------------------ [onClose - AdminSet_Setting_BLEStrength] -------------------");
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onClose()");
            e.printStackTrace();
        }
        super.onClose();
    }

    @Override
    protected void onBrReceive(Intent intent) {
        try {
            Log.d(TAG, "[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) {
                ctrlBLEStrength(false);
                UpdateScreen();
            } else if (keyCode == Declare.Button.NUMBER_03) {
                ctrlBLEStrength(true);
                UpdateScreen();
            } else if (keyCode == Declare.Button.GUARD) {
                // 설정
                if (!setBLEStrength(nBLEStrength)) {
                    MainActivity.mSound.PlayMent_lobby(R.raw.trylater);
                } else {
                    MainActivity.mSound.PlayMent_lobby(R.raw.setting);
                }
                super.setChangeScreen(ID.SCREENS.AdminSet_Setting);
            } else if (keyCode == Declare.Button.CANCEL || keyCode == Declare.Button.TACT_CANCEL) {
                // 메뉴화면으로 이동
                super.setChangeScreen(ID.SCREENS.AdminSet_Setting);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "[Exception] onKeyDown(int keyCode, KeyEvent event)");
        }
    }

    private void ctrlBLEStrength(boolean bUp) {
        try {
            if (bUp) {
                if (nBLEStrength < MAX_BLE_STRENGTH) {
                    nBLEStrength++;
                    strText_Line_02_Item = "";
                } else if (nBLEStrength == MAX_BLE_STRENGTH) {
                    nBLEStrength = MAX_BLE_STRENGTH;
                    strText_Line_02_Item = strText_LINE_02_ITEM_MAX;
                }
            }
            else {
                if (nBLEStrength > MIN_BLE_STRENGTH) {
                    nBLEStrength--;
                    strText_Line_02_Item = "";
                } else if (nBLEStrength == MIN_BLE_STRENGTH) {
                    nBLEStrength = MIN_BLE_STRENGTH;
                    strText_Line_02_Item = strText_LINE_02_ITEM_MIM;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "[Exception] ctrlBLEStrength(boolean bUp)");
        }
    }

    protected void UpdateScreen() {
        try {
            strText_Line_01_Input = String.valueOf(nBLEStrength);
            Text_Line_01_Input.setText(strText_Line_01_Input);
            Text_Line_02_Item.setText(strText_Line_02_Item);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateScreen()");
            e.printStackTrace();
        }
    }

    private POSITION getPosition(int nID) {
        POSITION position = new POSITION();
        position.SetPosition(0, 0, 480, 272);
        try {
            switch (nID) {
                case ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.IMAGES.BACKGROUND:
                    position.SetPosition(0, 0, 480, 272);
                    return position;

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

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

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

                case ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_02_ITEM:
                    position.SetPosition(30, 180, 420, 40);
                    return position;

                case ID.SCREENS.ADMINSET_SETIING_BLESTRENGTH.ELEMENTS.TEXTS.LINE_03_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;
        }
    }

    // DB SET
    private boolean setBLEStrength(int nStrenth) {
        try {
            Log.d(TAG, "[setBLEStrength] nStrenth [" + nStrenth + "]");
            Configure.ConfigValue.nBLEStrength = nStrenth;
            if (!MainActivity.ConfigDBManager.setData(Declare.DBInfo.TABLE_NAME_CONFIG, Configure.ConfigValKey.BLEStrength, Configure.ConfigValue.nBLEStrength)) return false;

            Global.checkRFReaderErrorMode();

            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setBLEStrength(int nStrenth)");
            e.printStackTrace();
            return false;
        }
    }
}
