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.view.View;
import android.widget.RelativeLayout;

import kr.co.icontrols.ilp300_700.MainActivity;
import kr.co.icontrols.ilp300_700.R;
import kr.co.icontrols.ilp300_700.call.CallManager.CALLTYPE;
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.DATAINPUTSCREEN;
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_Volume extends WpadScreen {

    static final String TAG = "AdminSet_Setting_Volume";

    boolean bLOG = true;   // true, false
    boolean bLOG_DEBUG = false;   // true, false
    boolean bLOG_INFO = false;   // 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;
    WpadTextView Text_Line_05_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 = "음량 조절";
    String strText_Line_01_Item = "01] 시스템 음량";
    String strText_Line_02_Item = "02] 통화 음량";
    String strText_Line_03_Item = "0";
    String strText_Line_04_Item = "1: ↓                3: ↑";
    String strText_Line_05_Item = "- 음량 선택 후 '경비실'버튼을 누르세요. -";
    int MAX_SYSTEM_VOLUME = 10;
    int MIN_SYSTEM_VOLUME = 1;
    int MAX_MOIP_VOLUME = 10;
    int MIN_MOIP_VOLUME = 1;
    int nVolLevel_System = 0;
    int nVolLevel_Talk = 0;
    public AdminSet_Setting_Volume(Context context, RelativeLayout layout) {
        super(context, layout);
        try {
            LOG("------------------ [START - AdminSet_Setting_Volume] -------------------");

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

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

            Text_Line_01_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_01_Item).W, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_01_Item).H, Gravity.LEFT, TextColor_Normal, nFontSize_Menu, bFontNonBold, ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_01_Item);
            ViewRegistration(layout, Text_Line_01_Item, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_01_Item).X, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.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_VOLUME.ELEMENTS.TEXTS.LINE_02_Item).W, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_02_Item).H, Gravity.LEFT, TextColor_Normal, nFontSize_Menu, bFontNonBold, ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_02_Item);
            ViewRegistration(layout, Text_Line_02_Item, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_02_Item).X, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.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_VOLUME.ELEMENTS.TEXTS.LINE_03_Item).W, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_03_Item).H, Gravity.CENTER, TextColor_Highlight, nFontSize_Menu, bFontNonBold, ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_03_Item);
            ViewRegistration(layout, Text_Line_03_Item, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_03_Item).X, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.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_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_04_Item).W, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_04_Item).H, Gravity.CENTER, TextColor_Normal, nFontSize_Menu, bFontNonBold, ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_04_Item);
            ViewRegistration(layout, Text_Line_04_Item, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_04_Item).X, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_04_Item).Y);
            Text_Line_04_Item.setText(strText_Line_04_Item);

            Text_Line_05_Item = new WpadTextView(context, false, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_05_Item).W, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_05_Item).H, Gravity.CENTER, TextColor_Normal, nFontSize_Instruction, bFontNonBold, ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_05_Item);
            ViewRegistration(layout, Text_Line_05_Item, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_05_Item).X, getPosition(ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_05_Item).Y);
            Text_Line_05_Item.setText(strText_Line_05_Item);

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

    // 화면 정의
    // Global.DATAINPUTSCREEN.DETAIL_01 : 음량 조절 메인
    // Global.DATAINPUTSCREEN.DETAIL_02 : 시스템 음량 설정
    // Global.DATAINPUTSCREEN.DETAIL_03 : 통화 음량 설정

    private void LOG_INFO(String log) {
        if (bLOG_INFO) Log.i(TAG, log);
    }

    protected void Initialize() {
        try {
            LOG("[Initialize] ===== START =====");
            Global.DataInputScreen = DATAINPUTSCREEN.DETAIL_01;
            nVolLevel_System = ConfigValue.nVolumeLevel_System;
            nVolLevel_Talk = ConfigValue.nVolumeLevel_Talk;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] Initialize() : " + e);
            e.printStackTrace();
        }
    }

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

    @Override
    protected void onBrReceive(Intent intent) {
        LOG("[onBrReceive] Start!!");
        try {
            Log.d(TAG, "[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 (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_01) {
                // 음량 조절 선택 화면
                if (keyCode == Declare.Button.NUMBER_01) {
                    // 시스템 음량 조절 화면
                    Global.DataInputScreen = DATAINPUTSCREEN.DETAIL_02;
                    nVolLevel_System = ConfigValue.nVolumeLevel_System;
                    strText_Line_03_Item = Integer.toString(nVolLevel_System);
                    UpdateScreen();
                } else if (keyCode == Declare.Button.NUMBER_02) {
                    // 통화 음량 조절 화면
                    Global.DataInputScreen = DATAINPUTSCREEN.DETAIL_03;
                    nVolLevel_Talk = ConfigValue.nVolumeLevel_Talk;
                    strText_Line_03_Item = Integer.toString(nVolLevel_Talk);
                    UpdateScreen();
                } else if (keyCode == Declare.Button.CANCEL || keyCode == Declare.Button.TACT_CANCEL) {
                    // 메뉴화면으로 이동
                    super.setChangeScreen(ID.SCREENS.AdminSet_Setting);
                }
            } else if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_02 || Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_03) {
                // 세대 호출음
                if (keyCode == Declare.Button.NUMBER_01) {
                    // Volume Down
                    if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_02) {
                        if (nVolLevel_System > MIN_SYSTEM_VOLUME) {
                            nVolLevel_System--;
                        } else if (nVolLevel_System == MIN_SYSTEM_VOLUME) {
                            nVolLevel_System = MIN_SYSTEM_VOLUME;
                        }
                    } else if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_03) {
                        if (nVolLevel_Talk > MIN_MOIP_VOLUME) {
                            nVolLevel_Talk--;
                        } else if (nVolLevel_Talk == MIN_MOIP_VOLUME) {
                            nVolLevel_Talk = MIN_MOIP_VOLUME;
                        }
                    }
                    UpdateScreen();
                } else if (keyCode == Declare.Button.NUMBER_03) {
                    // Volume Up
                    if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_02) {
                        if (nVolLevel_System < MAX_SYSTEM_VOLUME) {
                            nVolLevel_System++;
                        } else if (nVolLevel_System == MAX_SYSTEM_VOLUME) {
                            nVolLevel_System = MAX_SYSTEM_VOLUME;
                        }
                    } else if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_03) {
                        if (nVolLevel_Talk < MAX_MOIP_VOLUME) {
                            nVolLevel_Talk++;
                        } else if (nVolLevel_Talk == MAX_MOIP_VOLUME) {
                            nVolLevel_Talk = MAX_MOIP_VOLUME;
                        }
                    }
                    UpdateScreen();
                } else if (keyCode == Declare.Button.GUARD) {
                    if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_02) {
                        // 시스템 음량 설정
                        if (!setSystemVolume(nVolLevel_System))
                            MainActivity.mSound.PlayMent_lobby(R.raw.retryinput);
                        else {
                            Global.changeVolumeMoIPMode(CALLTYPE.NONE);
                            MainActivity.mSound.PlayMent_lobby(R.raw.setting);
                            super.setChangeScreen(ID.SCREENS.AdminSet_Setting);
                        }
                    } else if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_03) {
                        // 통화 음량 설정
                        if (!setMoIPVolume(nVolLevel_Talk))
                            MainActivity.mSound.PlayMent_lobby(R.raw.retryinput);
                        else {
                            MainActivity.mSound.PlayMent_lobby(R.raw.setting);
                            super.setChangeScreen(ID.SCREENS.AdminSet_Setting);
                        }
                        super.setChangeScreen(ID.SCREENS.AdminSet_Setting);
                    }
                } else if (keyCode == Declare.Button.CANCEL || keyCode == Declare.Button.TACT_CANCEL) {
                    // 호출음 선택 메인화면으로 이동
                    Global.DataInputScreen = DATAINPUTSCREEN.DETAIL_01;
                    UpdateScreen();
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onKeyDown(...) : " + e);
            e.printStackTrace();
        }
        super.onKeyDown(keyCode, event);
    }

    protected void UpdateVisibility() {
//        LOG("[UpdateVisibility] Start!!");
        try {
            if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_01) {
                Text_Line_01_Item.setVisibility(View.VISIBLE);
                Text_Line_02_Item.setVisibility(View.VISIBLE);
                Text_Line_03_Item.setVisibility(View.INVISIBLE);
                Text_Line_04_Item.setVisibility(View.INVISIBLE);
                Text_Line_05_Item.setVisibility(View.INVISIBLE);
            } else if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_02 || Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_03) {
                Text_Line_01_Item.setVisibility(View.INVISIBLE);
                Text_Line_02_Item.setVisibility(View.INVISIBLE);
                Text_Line_03_Item.setVisibility(View.VISIBLE);
                Text_Line_04_Item.setVisibility(View.VISIBLE);
                Text_Line_05_Item.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] UpdateVisibility() : " + e);
            e.printStackTrace();
        }
    }

    protected void UpdateScreen() {
//        LOG("[updateScreen] Start!!");
        try {
            if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_01) {
                strText_Title = "음량 조절";
                Text_Title.setText(strText_Title);
                UpdateVisibility();
            } else if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_02) {
                strText_Title = "시스템 음량";
                strText_Line_03_Item = Integer.toString(nVolLevel_System);
                Text_Title.setText(strText_Title);
                Text_Line_03_Item.setText(strText_Line_03_Item);
                UpdateVisibility();
            } else if (Global.DataInputScreen == DATAINPUTSCREEN.DETAIL_03) {
                strText_Title = "통화 음량";
                strText_Line_03_Item = Integer.toString(nVolLevel_Talk);
                Text_Title.setText(strText_Title);
                Text_Line_03_Item.setText(strText_Line_03_Item);
                UpdateVisibility();
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateScreen() : " + e);
            e.printStackTrace();
        }
    }

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

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

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

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

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

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

                case ID.SCREENS.ADMINSET_SETTING_VOLUME.ELEMENTS.TEXTS.LINE_05_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 boolean setSystemVolume(int nVolume) {
        try {
            LOG("[setSystemVolume] nVolume = " + nVolume);
            ConfigValue.nVolumeLevel_System = nVolume;
            if (!MainActivity.ConfigDBManager.setData(Declare.DBInfo.TABLE_NAME_CONFIG, ConfigValKey.VolumeLevel_System, ConfigValue.nVolumeLevel_System))
                return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setSystemVolume(nVol) : " + e);
            e.printStackTrace();
            return false;
        }
    }

    private boolean setMoIPVolume(int nVolume) {
        try {
            LOG("[setMoIPVolume] nVolume = " + nVolume);
            ConfigValue.nVolumeLevel_Talk = nVolume;
            if (!MainActivity.ConfigDBManager.setData(Declare.DBInfo.TABLE_NAME_CONFIG, ConfigValKey.VolumeLevel_Talk, ConfigValue.nVolumeLevel_Talk))
                return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setMoIPVolume(nVol) : " + e);
            e.printStackTrace();
            return false;
        }
    }


}