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.ConfigValKey;
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.Enumeration.DATAINPUTSTEP;
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_ECOMode extends WpadScreen {

    String TAG = "AdminSet_Setting_ECOMode";

    boolean bLOG = true;   // true, false
    boolean bLOG_DEBUG = false;   // 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_01_Input;
    int TextColor_Normal = Color.WHITE;
    int TextColor_Highlight = Color.GREEN;
    int nFontSize_Instruction = 28;
    int nFontSize_Instruction_Bottom = 20;
    boolean bFontNonBold = false;
    boolean bFontBold = true;
    String strText_Title = "절전모드";
    String strText_Line_01_Item = " 분";
    String strText_Line_02_Item = "* 0 ~ 99분까지 설정 가능합니다.";
    String strText_Line_03_Item = "* 0분 : 화면 안꺼짐.";
    String strText_Line_01_Input = "0";
    public AdminSet_Setting_ECOMode(Context context, RelativeLayout layout) {
        super(context, layout);
        try {
            LOG("------------------ [START - AdminSet_Setting_ECOMode] -------------------");

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

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

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

            Text_Line_01_Input = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_SETTING_ECOMODE.ELEMENTS.TEXTS.LINE_01_Input).W, getPosition(ID.SCREENS.ADMINSET_SETTING_ECOMODE.ELEMENTS.TEXTS.LINE_01_Input).H, Gravity.RIGHT, Color.WHITE, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_SETTING_ECOMODE.ELEMENTS.TEXTS.LINE_01_Input);
            ViewRegistration(layout, Text_Line_01_Input, getPosition(ID.SCREENS.ADMINSET_SETTING_ECOMODE.ELEMENTS.TEXTS.LINE_01_Input).X, getPosition(ID.SCREENS.ADMINSET_SETTING_ECOMODE.ELEMENTS.TEXTS.LINE_01_Input).Y);
            Text_Line_01_Input.setText("[ " + strText_Line_01_Input + " ]");
            Text_Line_01_Input.setTextColor(TextColor_Highlight);

            UpdateScreen();

            ((MainActivity) mContext).setAutoScreenChange(ID.SCREENS.Main, MainActivity.nAdminScreenTimeout);
        } catch (Exception e) {
            LOG("[Exception] AdminSet_Setting_ECOMode(...) : " + 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 =====");
            Global.DataInputStep = DATAINPUTSTEP.INPUT_01;

            strText_Line_01_Input = Integer.toString(ConfigValue.nECOTime);
            LOG("[Initialize] strText_Line_01_Input = " + strText_Line_01_Input);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize() : " + e);
            e.printStackTrace();
        }
    }

    @Override
    protected void onClose() {
        try {
            LOG("------------------ [onClose - AdminSet_Setting_ECOMode] -------------------");
        } 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 (Declare.Button.NUMBER_00 <= keyCode && keyCode <= Declare.Button.NUMBER_09) {
                strText_Line_01_Input += keyCode - 7;

                if (strText_Line_01_Input.length() > 2) {
                    strText_Line_01_Input = strText_Line_01_Input.substring(1);
                    strText_Line_01_Input = String.valueOf(Integer.parseInt(strText_Line_01_Input));
                }
                UpdateScreen();
            } else if (keyCode == Declare.Button.GUARD) {
                // 설정정보를 저장하는 기능을 정의한다.
                if (setECOMode(strText_Line_01_Input)) {
                    MainActivity.mSound.PlayMent_lobby(R.raw.setting);
                    Global.setLCDECOMode(ConfigValue.nECOTime);
                    super.setChangeScreen(ID.SCREENS.AdminSet_Setting);
                } else {
                    MainActivity.mSound.PlayMent_lobby(R.raw.retryinput);
                    Initialize();
                    UpdateScreen();
                }
            } else if (keyCode == Declare.Button.CANCEL || keyCode == Declare.Button.TACT_CANCEL) {
                super.setChangeScreen(ID.SCREENS.AdminSet_Setting);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onKeyDown(...) : " + e);
            e.printStackTrace();
        }
        super.onKeyDown(keyCode, event);
    }

    protected void UpdateScreen() {
        try {
            LOG("[updateScreen] Start!!");
            Text_Line_01_Input.setText("[ " + strText_Line_01_Input + " ]");
        } catch (Exception e) {
            Log.d(TAG, "[Exception] updateScreen() : " + e);
        }
    }

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

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

                case ID.SCREENS.ADMINSET_SETTING_ECOMODE.ELEMENTS.TEXTS.LINE_01_Item:
                    position.SetPosition(255, 104, 195, 40);
                    return position;

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

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

                case ID.SCREENS.ADMINSET_SETTING_ECOMODE.ELEMENTS.TEXTS.LINE_01_Input:
                    position.SetPosition(50, 104, 195, 40);
                    return position;

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

    private boolean setECOMode(String value) {
        try {
            LOG("[setECOMode] value [" + value + "]");
            int nvalue = Integer.parseInt(value);
            if (0 <= nvalue && nvalue <= 99) {
                ConfigValue.nECOTime = nvalue;
                if (!MainActivity.ConfigDBManager.setData(Declare.DBInfo.TABLE_NAME_CONFIG, ConfigValKey.ECOTime, ConfigValue.nECOTime))
                    return false;
                return true;
            } else return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setECOMode(value)");
            e.printStackTrace();
            return false;
        }
    }

//    private boolean setECOTime(int time) {
//        try {
//            DEBUG("[setECOTime] time [" + time + "]");
//            boolean bResult = false;
//            if (time == 0) {
//                time = 2147483647;
////                bResult = Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, -1);
//                bResult = Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, time);
////                ((MainActivity)mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//            }
//            else {
//                bResult = Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, time * 60 * 1000);
//            }
//            DEBUG("[setECOTime] bResult [" + bResult + "]");
//            int  getECOTime = Settings.System.getInt(this.getContext().getContentResolver(), SCREEN_OFF_TIMEOUT);
//            DEBUG("[setECOTime] getECOTime [" + getECOTime + "]");
//            return true;
//        } catch (Exception e) {
//            Log.e(TAG, "[Exception] setECOTime(time)");
//            e.printStackTrace();
//            return false;
//        }
//    }
}