/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.kneet.v3.view.rules;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.c_interface.Main_Interface;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.common.widget.CustomActivity;
import com.ntels.kneet.v3.R;
import com.ntels.kneet.v3.view.dialog.CustomDialog;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

public class Rules_Triggers_Time extends CustomActivity implements View.OnClickListener,
                                                                   CustomDialog.OnMsgAlterToResult {

    /** Called when the activity is first created. */

    private Toolbar   toolbar;
    private AppConfig appConfig;
    Main_Interface mCallback;
    private Util util = new Util();
    private Intent   intent;
    private Activity act;
    private DecimalFormat decimalFormat = new DecimalFormat("00");

    private              CustomDialog customDialog                = new CustomDialog();
    private String                             sCallType         = "";
    private String                             sTime             = "";
    private String                             sHour             = "";
    private String                             sMinute           = "";
    private String                             sAMPM             = "";
    private String                             sRepeatble        = "";
    private String                             sDayOfWeek        = "";
    private String                             sDayOfWeek1       = "";
    private ArrayList<HashMap<String, Object>> alList_Triggers   = new ArrayList<HashMap<String, Object>>();
    private ArrayList<HashMap<String, Object>> alList_Conditions = new ArrayList<HashMap<String, Object>>();
    private HashMap<String, Object>            hmItem_Sub        = new HashMap<String, Object>();
    private Button btnUpHH, btnDownHH;
    private Button btnUpMM, btnDownMM;
    private Button btnUpAMPM, btnDownAMPM;
    private TextView tvUpHH, tvUpMM, tvUpAMPM, tvAMPM;
    private TextView tvDownHH, tvDownMM, tvDownAMPM;
    private EditText edtHH, edtMM;
    private int iHHStart = 1;
    private int iHHEnd   = 12;
    private int iMMStart = 0;
    private int iMMEnd   = 59;

    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        act = Rules_Triggers_Time.this;
        appConfig = (AppConfig) act.getApplicationContext();
        customDialog.setOnMsgAlterToResultEvent(this);

        View viewToLoad = LayoutInflater.from(act).inflate(R.layout.rules_triggers_time, null);
        this.setContentView(viewToLoad);

        this.setContentView(viewToLoad);
        String sTempHour = "";
        if ( savedInstanceState != null ) {

        } else {
            Intent intent = getIntent();
            alList_Triggers = (ArrayList<HashMap<String, Object>>) intent.getSerializableExtra(Constants.EXTRA_TRIGGERS);
            alList_Conditions = (ArrayList<HashMap<String, Object>>) intent.getSerializableExtra(Constants.EXTRA_CONDITIONS);
        }

        displayListView();
    }

    private void displayListView() {


        btnUpHH = (Button) findViewById(R.id.btnUpHH);
        btnUpMM = (Button) findViewById(R.id.btnUpMM);
        btnUpAMPM = (Button) findViewById(R.id.btnUpAMPM);
        btnDownHH = (Button) findViewById(R.id.btnDownHH);
        btnDownMM = (Button) findViewById(R.id.btnDownMM);
        btnDownAMPM = (Button) findViewById(R.id.btnDownAMPM);

        tvUpHH = (TextView) findViewById(R.id.tvUpHH);
        tvUpMM = (TextView) findViewById(R.id.tvUpMM);
        tvUpAMPM = (TextView) findViewById(R.id.tvUpAMPM);
        edtHH = (EditText) findViewById(R.id.edtHH);
        edtMM = (EditText) findViewById(R.id.edtMM);
        tvAMPM = (TextView) findViewById(R.id.tvAMPM);
        tvDownHH = (TextView) findViewById(R.id.tvDownHH);
        tvDownMM = (TextView) findViewById(R.id.tvDownMM);
        tvDownAMPM = (TextView) findViewById(R.id.tvDownAMPM);

        btnUpHH.setOnClickListener(this);
        tvUpHH.setOnClickListener(this);
        tvDownHH.setOnClickListener(this);
        btnDownHH.setOnClickListener(this);

        btnUpMM.setOnClickListener(this);
        tvUpMM.setOnClickListener(this);
        tvDownMM.setOnClickListener(this);
        btnDownMM.setOnClickListener(this);

        btnUpAMPM.setOnClickListener(this);
        tvUpAMPM.setOnClickListener(this);
        tvDownAMPM.setOnClickListener(this);
        btnDownAMPM.setOnClickListener(this);

        util.getView(act, R.id.llMon).setOnClickListener(this);
        util.getView(act, R.id.llTue).setOnClickListener(this);
        util.getView(act, R.id.llWed).setOnClickListener(this);
        util.getView(act, R.id.llThu).setOnClickListener(this);
        util.getView(act, R.id.llFri).setOnClickListener(this);
        util.getView(act, R.id.llSat).setOnClickListener(this);
        util.getView(act, R.id.llSun).setOnClickListener(this);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);
        util.getView(act, R.id.llBtn2).setOnClickListener(this);
        util.getView(act, R.id.llMon).setSelected(true);
        util.getView(act, R.id.llTue).setSelected(true);
        util.getView(act, R.id.llWed).setSelected(true);
        util.getView(act, R.id.llThu).setSelected(true);
        util.getView(act, R.id.llFri).setSelected(true);
        util.getView(act, R.id.llSat).setSelected(true);
        util.getView(act, R.id.llSun).setSelected(true);

        getTriggers_Time();

        getDayOfWeek();

        setKeyDisp();
        if ( sHour.equals("") ) {
            edtHH.setText(util.getTimeHOUR());
        } else {
            edtHH.setText(sHour);
        }
        //		btnUpHH.performClick();
        //		btnDownHH.performClick();

        if ( sMinute.equals("") ) {
            edtMM.setText(util.getTimeMINUTE());
        } else {
            edtMM.setText(sMinute);
        }
        //		btnUpMM.performClick();
        //		btnDownMM.performClick();

        if ( sAMPM.equals("") ) {
            tvAMPM.setText(util.getTimeAM_PM());
        } else {
            tvAMPM.setText(sAMPM);
        }
        btnUpAMPM.performClick();
        btnDownAMPM.performClick();

        //        util.setKeyboardHide(act, util.getLinearLayout(act, R.id.llSvbody));

    }

    void getTriggers_Time() {

        if ( alList_Triggers.size() > 0 ) {
            String sHTTP_item_sub_type_code = util.null2string(alList_Triggers.get(0).get(Constants.HTTP_item_sub_type_code));
            ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
            if ( !util.null2string(alList_Triggers.get(0).get(Constants.HTTP_item_sub)).equals("") ) {
                alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Triggers.get(0).get(Constants.HTTP_item_sub);
                if ( sHTTP_item_sub_type_code.equals("05") ) {
                    hmItem_Sub = alList_Item_Sub.get(0);

                    String sTempHour = "";
                    sDayOfWeek = util.null2string(hmItem_Sub.get(Constants.HTTP_dayofweek));
                    sRepeatble = util.null2string(hmItem_Sub.get(Constants.HTTP_repeatable));
                    sTempHour = util.null2string(hmItem_Sub.get(Constants.HTTP_hour));
                    sMinute = util.null2string(hmItem_Sub.get(Constants.HTTP_minute));
                    sAMPM = (util.str2int(sTempHour) >= 12 ? util.getString(act, R.string.layout_pm) : util.getString(act, R.string.layout_am));
                    sHour = (util.str2int(sTempHour) >= 12 ? decimalFormat.format((util.str2int(sTempHour) - 12)) : sTempHour);
                }
            }
        }
    }

    void getDayOfWeek() {

        if ( alList_Conditions.size() > 0 ) {
            for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                HashMap<String, Object> hmTemp = new HashMap<String, Object>();
                hmTemp = alList_Conditions.get(iRow);

                String sHTTP_item_sub_type_code = util.null2string(hmTemp.get(Constants.HTTP_item_sub_type_code));
                if ( sHTTP_item_sub_type_code.equals("22") ) {
                    //요일
                    ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                    if ( !util.null2string(hmTemp.get(Constants.HTTP_item_sub)).equals("") ) {
                        alList_Item_Sub = (ArrayList<HashMap<String, Object>>) hmTemp.get(Constants.HTTP_item_sub);
                        if ( alList_Item_Sub.size() > 0 ) {
                            HashMap<String, Object> hmTempItem_sub = new HashMap<String, Object>();
                            hmTempItem_sub = alList_Item_Sub.get(0);

                            String sHTTP_cmdcls_value = util.null2string(hmTempItem_sub.get(Constants.HTTP_cmdcls_value));
                            String sHTTP_cmdcls_value_msg = util.null2string(hmTempItem_sub.get(Constants.HTTP_cmdcls_value_msg));

                            sDayOfWeek = sHTTP_cmdcls_value;
                            sDayOfWeek1 = sHTTP_cmdcls_value_msg;
                        }
                    }
                    util.getView(act, R.id.llMon).setSelected(false);
                    util.getView(act, R.id.llTue).setSelected(false);
                    util.getView(act, R.id.llWed).setSelected(false);
                    util.getView(act, R.id.llThu).setSelected(false);
                    util.getView(act, R.id.llFri).setSelected(false);
                    util.getView(act, R.id.llSat).setSelected(false);
                    util.getView(act, R.id.llSun).setSelected(false);
                    if ( !sDayOfWeek.equals("") ) {
                        String[] strArray = sDayOfWeek.split("\\,", 100);
                        for ( int iRow1 = 0; iRow1 < strArray.length; iRow1++ ) {
                            if ( strArray[iRow1].equals("MON") ) {
                                util.getView(act, R.id.llMon).setSelected(true);
                            } else if ( strArray[iRow1].equals("TUE") ) {
                                util.getView(act, R.id.llTue).setSelected(true);
                            } else if ( strArray[iRow1].equals("WED") ) {
                                util.getView(act, R.id.llWed).setSelected(true);
                            } else if ( strArray[iRow1].equals("THU") ) {
                                util.getView(act, R.id.llThu).setSelected(true);
                            } else if ( strArray[iRow1].equals("FRI") ) {
                                util.getView(act, R.id.llFri).setSelected(true);
                            } else if ( strArray[iRow1].equals("SAT") ) {
                                util.getView(act, R.id.llSat).setSelected(true);
                            } else if ( strArray[iRow1].equals("SUN") ) {
                                util.getView(act, R.id.llSun).setSelected(true);
                            }
                        }
                    }
                    break;
                }
            }
        }
    }

    private Boolean setsDayOfWeek() {

        Boolean bResult = true;
        String sTempDayOfWeek = "";
        String sTempDayOfWeek1 = "";

        if ( util.getView(act, R.id.llMon).isSelected() ) {
            if ( !sTempDayOfWeek.equals("") ) {
                sTempDayOfWeek = sTempDayOfWeek + ",";
            }
            sTempDayOfWeek = sTempDayOfWeek + "MON";
        }
        if ( util.getView(act, R.id.llTue).isSelected() ) {
            if ( !sTempDayOfWeek.equals("") ) {
                sTempDayOfWeek = sTempDayOfWeek + ",";
            }
            sTempDayOfWeek = sTempDayOfWeek + "TUE";
        }
        if ( util.getView(act, R.id.llWed).isSelected() ) {
            if ( !sTempDayOfWeek.equals("") ) {
                sTempDayOfWeek = sTempDayOfWeek + ",";
            }
            sTempDayOfWeek = sTempDayOfWeek + "WED";
        }
        if ( util.getView(act, R.id.llThu).isSelected() ) {
            if ( !sTempDayOfWeek.equals("") ) {
                sTempDayOfWeek = sTempDayOfWeek + ",";
            }
            sTempDayOfWeek = sTempDayOfWeek + "THU";
        }
        if ( util.getView(act, R.id.llFri).isSelected() ) {
            if ( !sTempDayOfWeek.equals("") ) {
                sTempDayOfWeek = sTempDayOfWeek + ",";
            }
            sTempDayOfWeek = sTempDayOfWeek + "FRI";
        }
        if ( util.getView(act, R.id.llSat).isSelected() ) {
            if ( !sTempDayOfWeek.equals("") ) {
                sTempDayOfWeek = sTempDayOfWeek + ",";
            }
            sTempDayOfWeek = sTempDayOfWeek + "SAT";
        }
        if ( util.getView(act, R.id.llSun).isSelected() ) {
            if ( !sTempDayOfWeek.equals("") ) {
                sTempDayOfWeek = sTempDayOfWeek + ",";
            }
            sTempDayOfWeek = sTempDayOfWeek + "SUN";
        }
        if ( sTempDayOfWeek.equals("") ) {
            customDialog.MsgAlert(act, Constants.MSG855);
            bResult = false;
            return bResult;
        }

        String[] alItem = util.getStringArrayValues(act, "DAYOFWEEK_ITEM");
        String[] alText = util.getStringArrayValues(act, "DAYOFWEEK_TEXT");
        sTempDayOfWeek1 = sTempDayOfWeek;
        String[] strArray = sTempDayOfWeek.split("\\,", 100);
        for ( int iRow1 = 0; iRow1 < strArray.length; iRow1++ ) {
            String sTemp = strArray[iRow1];
            int iIdx = util.getArraryFind(alItem, sTemp);
            sTempDayOfWeek1 = sTempDayOfWeek1.replaceAll(sTemp, alText[iIdx]);
        }

        ArrayList<HashMap<String, Object>> alTempItem_sub = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> hmTempItem_sub = new HashMap<String, Object>();
        String sTempSOURCE_ID = "";
        String sTempSOURCE_SUB_ID = "";
        String sTempCONDITION_TYPE_CODE = "09";
        String sTempCMDCLS_VALUE = sTempDayOfWeek;
        String sTempCMDCLS_VALUE_MSG = sTempDayOfWeek1;

        hmTempItem_sub.put(Constants.HTTP_source_id, sTempSOURCE_ID);
        hmTempItem_sub.put(Constants.HTTP_source_sub_id, sTempSOURCE_SUB_ID);
        hmTempItem_sub.put(Constants.HTTP_condition_type_code, sTempCONDITION_TYPE_CODE);
        hmTempItem_sub.put(Constants.HTTP_cmdcls_value, sTempCMDCLS_VALUE);
        hmTempItem_sub.put(Constants.HTTP_cmdcls_value_msg, sTempCMDCLS_VALUE_MSG);
        alTempItem_sub.add(hmTempItem_sub);

        HashMap<String, Object> hmTempCondition = new HashMap<String, Object>();
        //        hmTempCondition.put(Constants.HTTP_pred_rule_id, "");
        //        hmTempCondition.put(Constants.HTTP_pred_item_sequence, "");
        hmTempCondition.put(Constants.HTTP_item_name, "");
        hmTempCondition.put(Constants.HTTP_item_sub_type_code, "22");
        hmTempCondition.put(Constants.HTTP_item_sub, alTempItem_sub);

        boolean bSet = false;
        if ( alList_Conditions.size() > 0 ) {
            for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                HashMap<String, Object> hmTemp = new HashMap<String, Object>();
                hmTemp = alList_Conditions.get(iRow);

                String sHTTP_item_sub_type_code = util.null2string(hmTemp.get(Constants.HTTP_item_sub_type_code));
                if ( sHTTP_item_sub_type_code.equals("22") ) {
                    alList_Conditions.set(iRow, hmTempCondition);
                    bSet = true;
                    break;
                }
            }

            if ( !bSet ) {
                alList_Conditions.add(hmTempCondition);
            }
        } else {
            alList_Conditions.add(hmTempCondition);
        }

        return bResult;
    }

    void setKeyDisp() {

        edtHH.setImeOptions(EditorInfo.IME_ACTION_DONE); // 키보드 확인 버튼 클릭시
        edtHH.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ( actionId == EditorInfo.IME_ACTION_DONE ) {
                    edtHH.clearFocus();
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
                return false;
            }
        });
        edtHH.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {

                if ( !hasFocus ) {
                    if ( edtHH.getText().toString().length() == 1 ) {
                        edtHH.setText("0" + edtHH.getText());
                    }
                }
            }
        });
        edtHH.addTextChangedListener(new TextWatcher() {

            @Override public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override public void afterTextChanged(Editable s) {

                if ( util.null2int(s.toString()) > 12 ) {
                    edtHH.setText("12");
                } else if ( util.null2string(s.toString()).equals("") ) {
                    edtHH.setText("12");
                } else if ( s.toString().length() > 1 ) {
                    if ( util.null2int(s.toString()) == 0 ) {
                        edtHH.setText("12");
                    } else {
                        int iCurentHH = util.null2int(s.toString());
                        if ( iCurentHH <= iHHEnd ) {
                            if ( iCurentHH == iHHStart ) {
                                tvUpHH.setText(decimalFormat.format(iHHEnd));
                            } else {
                                tvUpHH.setText(decimalFormat.format(iCurentHH - 1));
                            }
                            if ( iCurentHH < iHHEnd ) {
                                tvDownHH.setText(decimalFormat.format(iCurentHH + 1));
                            } else {
                                tvDownHH.setText(decimalFormat.format(iHHStart));
                            }
                        } else {
                            iCurentHH = iHHStart;
                            tvUpHH.setText(decimalFormat.format(iHHEnd));
                            tvDownHH.setText(decimalFormat.format(iCurentHH + 1));
                        }
                        edtHH.selectAll();
                    }
                } else {
                    if ( util.null2int(s.toString()) > 1 ) {
                        edtHH.setText("0" + s.toString());
                    } else if ( util.null2int(s.toString()) > 0 ) {
                        int iCurentHH = util.null2int(s.toString());
                        if ( iCurentHH <= iHHEnd ) {
                            if ( iCurentHH == iHHStart ) {
                                tvUpHH.setText(decimalFormat.format(iHHEnd));
                            } else {
                                tvUpHH.setText(decimalFormat.format(iCurentHH - 1));
                            }
                            if ( iCurentHH < iHHEnd ) {
                                tvDownHH.setText(decimalFormat.format(iCurentHH + 1));
                            } else {
                                tvDownHH.setText(decimalFormat.format(iHHStart));
                            }
                        } else {
                            iCurentHH = iHHStart;
                            tvUpHH.setText(decimalFormat.format(iHHEnd));
                            tvDownHH.setText(decimalFormat.format(iCurentHH + 1));
                        }
                    }
                }
            }
        });
        edtMM.setImeOptions(EditorInfo.IME_ACTION_DONE); // 키보드 확인 버튼 클릭시
        edtMM.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ( actionId == EditorInfo.IME_ACTION_DONE ) {
                    edtMM.clearFocus();
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
                return false;
            }
        });
        edtMM.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {

                if ( !hasFocus ) {
                    if ( edtMM.getText().toString().length() == 1 ) {
                        edtMM.setText("0" + edtMM.getText());
                    }
                }
            }
        });
        edtMM.addTextChangedListener(new TextWatcher() {

            @Override public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override public void afterTextChanged(Editable s) {

                if ( util.null2int(s.toString()) > 59 ) {
                    edtMM.setText("59");
                } else if ( util.null2string(s.toString()).equals("") ) {
                    edtMM.setText("00");
                } else if ( s.toString().length() > 1 ) {
                    int iCurentMM = util.null2int(s.toString());
                    if ( iCurentMM > iMMStart ) {
                        tvUpMM.setText(decimalFormat.format(iCurentMM - 1));
                        if ( iCurentMM < iMMEnd ) {
                            tvDownMM.setText(decimalFormat.format(iCurentMM + 1));
                        } else {
                            tvDownMM.setText(decimalFormat.format(iMMStart));
                        }
                    } else {
                        iCurentMM = iMMStart;
                        tvUpMM.setText(decimalFormat.format(iMMEnd));
                        tvDownMM.setText(decimalFormat.format(iCurentMM + 1));
                    }
                    edtMM.selectAll();
                } else {
                    if ( util.null2int(s.toString()) > 5 ) {
                        edtMM.setText("0" + s.toString());
                    } else if ( util.null2int(s.toString()) > 0 ) {
                        int iCurentMM = util.null2int(s.toString());
                        if ( iCurentMM <= iMMEnd ) {
                            if ( iCurentMM == iHHStart ) {
                                tvUpMM.setText(decimalFormat.format(iMMEnd));
                            } else {
                                tvUpMM.setText(decimalFormat.format(iCurentMM - 1));
                            }
                            if ( iCurentMM < iMMEnd ) {
                                tvDownMM.setText(decimalFormat.format(iCurentMM + 1));
                            } else {
                                tvDownMM.setText(decimalFormat.format(iMMStart));
                            }
                        } else {
                            iCurentMM = iMMStart;
                            tvUpMM.setText(decimalFormat.format(iMMEnd));
                            tvDownMM.setText(decimalFormat.format(iCurentMM + 1));
                        }
                    }
                }
            }
        });
    }

    @Override public void onClick(View v) {

        int iCurentHH = util.null2int(edtHH.getText());
        int iCurentMM = util.null2int(edtMM.getText());
        String sAMPM = util.null2string(tvAMPM.getText());
        switch (v.getId()) {
            case R.id.llBtn1:
                if ( setsDayOfWeek() ) {
                    ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                    String sSOURCE_ID = "";
                    String sSOURCE_SUB_ID = "";
                    String sCONDITION_TYPE_CODE = "";
                    String sCMDCLS_VALUE = "";
                    String sDATA_TYPE_CODE = "";
                    String sDAYOFWEEK = "";
                    String sREPEATABLE = "";
                    String sHOUR = "";
                    String sMINUTE = "";

                    sHOUR = decimalFormat.format(iCurentHH + (sAMPM.equals(util.getString(act, R.string.layout_pm)) ? 12 : 0));
                    sMINUTE = decimalFormat.format(iCurentMM);
                    hmItem_Sub.put(Constants.HTTP_source_id, sSOURCE_ID);
                    hmItem_Sub.put(Constants.HTTP_source_sub_id, sSOURCE_SUB_ID);
                    hmItem_Sub.put(Constants.HTTP_condition_type_code, sCONDITION_TYPE_CODE);
                    hmItem_Sub.put(Constants.HTTP_cmdcls_value, sCMDCLS_VALUE);
                    hmItem_Sub.put(Constants.HTTP_data_type_code, sDATA_TYPE_CODE);
                    hmItem_Sub.put(Constants.HTTP_dayofweek, sDAYOFWEEK);
                    hmItem_Sub.put(Constants.HTTP_repeatable, sREPEATABLE);
                    hmItem_Sub.put(Constants.HTTP_hour, sHOUR);
                    hmItem_Sub.put(Constants.HTTP_minute, sMINUTE);
                    alList_Item_Sub.clear();
                    alList_Item_Sub.add(hmItem_Sub);

                    alList_Triggers.clear();

                    HashMap<String, Object> hmTriggers = new HashMap<String, Object>();
                    hmTriggers.put(Constants.HTTP_item_sub, alList_Item_Sub);
                    hmTriggers.put(Constants.HTTP_item_name, util.getString(act, R.string.layout_rules_triggers_select_02));
                    hmTriggers.put(Constants.HTTP_item_sub_type_code, "05");

                    alList_Triggers.add(hmTriggers);

                    intent = new Intent();
                    intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                    intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                    act.setResult(RESULT_OK, intent);
                    finish();
                }
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;
            case R.id.btnUpHH:
            case R.id.tvUpHH:
                if ( iCurentHH > iHHStart ) {
                    iCurentHH--;
                    edtHH.setText(decimalFormat.format(iCurentHH));
                } else {
                    iCurentHH = iHHEnd;
                    edtHH.setText(decimalFormat.format(iCurentHH));
                }

                break;
            case R.id.tvDownHH:
            case R.id.btnDownHH:
                if ( iCurentHH < iHHEnd ) {
                    iCurentHH++;
                    edtHH.setText(decimalFormat.format(iCurentHH));
                } else {
                    iCurentHH = iHHStart;
                    edtHH.setText(decimalFormat.format(iCurentHH));
                }
                break;
            case R.id.btnUpMM:
            case R.id.tvUpMM:
                if ( iCurentMM > iMMStart ) {
                    iCurentMM--;
                    edtMM.setText(decimalFormat.format(iCurentMM));
                } else {
                    iCurentMM = iMMEnd;
                    edtMM.setText(decimalFormat.format(iCurentMM));
                }
                break;
            case R.id.tvDownMM:
            case R.id.btnDownMM:
                if ( iCurentMM < iMMEnd ) {
                    iCurentMM++;
                    edtMM.setText(decimalFormat.format(iCurentMM));
                } else {
                    iCurentMM = iMMStart;
                    edtMM.setText(decimalFormat.format(iCurentMM));
                }
                break;
            case R.id.btnUpAMPM:
            case R.id.tvUpAMPM:
            case R.id.tvDownAMPM:
            case R.id.btnDownAMPM:
                if ( sAMPM.equals(util.getString(act, R.string.layout_am)) ) {
                    tvUpAMPM.setText(util.getString(act, R.string.layout_am));
                    tvAMPM.setText(util.getString(act, R.string.layout_pm));
                    tvDownAMPM.setText(util.getString(act, R.string.layout_am));
                } else {
                    tvUpAMPM.setText(util.getString(act, R.string.layout_pm));
                    tvAMPM.setText(util.getString(act, R.string.layout_am));
                    tvDownAMPM.setText(util.getString(act, R.string.layout_pm));
                }
                break;
            case R.id.llMon:
            case R.id.llTue:
            case R.id.llWed:
            case R.id.llThu:
            case R.id.llFri:
            case R.id.llSat:
            case R.id.llSun:
                if ( v.isSelected() ) {
                    v.setSelected(false);
                } else {
                    v.setSelected(true);
                }
                break;
            default:
                break;
        }
    }

    @Override public void resultCustomDialogResponse(int iMsgID, int iBtnResult) {

        switch (iMsgID) {

            case Constants.MSG998:
                util.callApp_Start(act, "LOGOUT");
                break;
            case Constants.MSG997:
                util.callApp_Start(act, "HOME_RELOAD");
                break;
            case Constants.MSG805:
                if ( iBtnResult == 1 ) {
                    // ComHandler.sendMessage(ComHandler.obtainMessage(MSG_COM_DOLOGOUT));
                }
                break;
            default:
                break;
        }
    }

    @Override protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
    }

    @Override public void onBackPressed() {

        intent = new Intent();
        act.setResult(RESULT_CANCELED, intent);
        finish();
    }
}
