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

package com.ntels.onecable.view.rules;

import com.ntels.onecable.R;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActivity;
import com.ntels.onecable.view.dialog.CustomDialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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 java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * <pre>
 * 1. ClassName :   RulesTriggersTime
 * 2. FileName  :   RulesTriggersTime.java
 * 3. Package   :   com.ntels.onecable.view.rules
 * 4. Date      :   2016-11-30
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class RulesTriggersTime extends CustomActivity implements View.OnClickListener,
    CustomDialog.OnMsgAlterToResult {

  private Util util = new Util();
  private Intent intent;
  private Activity act;
  private DecimalFormat decimalFormat = new DecimalFormat("00");

  private CustomDialog customDialog = new CustomDialog();
  private String hour = "";
  private String minute = "";
  private String ampm = "";
  private String dayOfWeek = "";
  private ArrayList<HashMap<String, Object>> triggers = new ArrayList<HashMap<String, Object>>();
  private ArrayList<HashMap<String, Object>> conditions = new ArrayList<HashMap<String, Object>>();
  private HashMap<String, Object> itemSubMap = new HashMap<String, Object>();
  private TextView tvUpHour;
  private TextView tvUpMinute;
  private TextView tvUpAmpm;
  private TextView tvAmpm;
  private TextView tvDownHour;
  private TextView tvDownMinute;
  private TextView tvDownAmpm;
  private EditText edtHour;
  private EditText edtMinute;
  private int hourStart = 1;
  private int hourEnd = 12;
  private int minuteStart = 0;
  private int minuteEnd = 59;

  @SuppressWarnings("unchecked")
  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    act = RulesTriggersTime.this;
    customDialog.setOnMsgAlterToResultEvent(this);

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

    this.setContentView(viewToLoad);
    if (null != getIntent()) {
      Intent intent = getIntent();
      triggers = (ArrayList<HashMap<String, Object>>)
          intent.getSerializableExtra(Constants.EXTRA_TRIGGERS);
      conditions = (ArrayList<HashMap<String, Object>>)
          intent.getSerializableExtra(Constants.EXTRA_CONDITIONS);
    }

    displayListView();
  }

  /**
   * UI display.
   */
  private void displayListView() {

    tvUpHour = (TextView) findViewById(R.id.tvUpHH);
    tvUpMinute = (TextView) findViewById(R.id.tvUpMM);
    tvUpAmpm = (TextView) findViewById(R.id.tvUpAMPM);
    edtHour = (EditText) findViewById(R.id.edtHH);
    edtMinute = (EditText) findViewById(R.id.edtMM);
    tvAmpm = (TextView) findViewById(R.id.tvAMPM);
    tvDownHour = (TextView) findViewById(R.id.tvDownHH);
    tvDownMinute = (TextView) findViewById(R.id.tvDownMM);
    tvDownAmpm = (TextView) findViewById(R.id.tvDownAMPM);
    Button btnUpHour = (Button) findViewById(R.id.btnUpHH);
    btnUpHour.setOnClickListener(this);
    tvUpHour.setOnClickListener(this);
    tvDownHour.setOnClickListener(this);
    Button btnDownHour = (Button) findViewById(R.id.btnDownHH);
    btnDownHour.setOnClickListener(this);
    Button btnUpMinute = (Button) findViewById(R.id.btnUpMM);
    btnUpMinute.setOnClickListener(this);
    tvUpMinute.setOnClickListener(this);
    tvDownMinute.setOnClickListener(this);
    Button btnDownMinute = (Button) findViewById(R.id.btnDownMM);
    btnDownMinute.setOnClickListener(this);
    Button btnUpAmpm = (Button) findViewById(R.id.btnUpAMPM);
    btnUpAmpm.setOnClickListener(this);
    tvUpAmpm.setOnClickListener(this);
    tvDownAmpm.setOnClickListener(this);
    Button btnDownAmpm = (Button) findViewById(R.id.btnDownAMPM);
    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);

    getTriggersTime();

    getDayOfWeek();

    setKeyDisp();
    if (hour.equals("")) {
      edtHour.setText(util.getTimeHOUR());
    } else {
      edtHour.setText(hour);
    }

    if (minute.equals("")) {
      edtMinute.setText(util.getTimeMINUTE());
    } else {
      edtMinute.setText(minute);
    }

    if (ampm.equals("")) {
      tvAmpm.setText(util.getTimeAM_PM());
    } else {
      tvAmpm.setText(ampm);
    }
    btnUpAmpm.performClick();
    btnDownAmpm.performClick();

  }

  /**
   * trigger time 가져오기.
   */
  @SuppressWarnings("unchecked")
  void getTriggersTime() {

    if (triggers.size() > 0) {
      String itemSubTypeCode = util.null2string(triggers.get(0)
          .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
      ArrayList<HashMap<String, Object>> itemSubList;
      if (!util.null2string(triggers.get(0).get(Constants.KEY_ITEM_SUB)).equals("")) {
        itemSubList = (ArrayList<HashMap<String, Object>>) triggers.get(0)
            .get(Constants.KEY_ITEM_SUB);
        if (itemSubTypeCode.equals("05")) {
          itemSubMap = itemSubList.get(0);

          String tempHour = "";
          dayOfWeek = util.null2string(itemSubMap.get(Constants.KEY_DAYOFWEEK));
          tempHour = util.null2string(itemSubMap.get(Constants.KEY_HOUR));
          minute = util.null2string(itemSubMap.get(Constants.KEY_MINUTE));
          String[] saTime = util.UTCtoLocal6(tempHour + minute);
          ampm = (util.str2int(saTime[0]) >= 12
              ? util.getString(act, R.string.layout_pm) : util.getString(act, R.string.layout_am));
          hour = (util.str2int(saTime[0]) >= 12
              ? decimalFormat.format((util.str2int(saTime[0]) - 12)) : saTime[0]);
        }
      }
    }
  }

  /**
   * 요일 가져오기.
   */
  @SuppressWarnings("unchecked")
  void getDayOfWeek() {

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

        String itemSubTypeCode = util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB_TYPE_CODE));
        if (itemSubTypeCode.equals("22")) {
          //요일
          ArrayList<HashMap<String, Object>> itemSubList;
          if (!util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB)).equals("")) {
            itemSubList = (ArrayList<HashMap<String, Object>>) hmTemp.get(Constants.KEY_ITEM_SUB);
            if (itemSubList.size() > 0) {
              HashMap<String, Object> tempItemSubMap;
              tempItemSubMap = itemSubList.get(0);

              String cmdclsValue = util.null2string(tempItemSubMap.get(
                  Constants.KEY_CMDCLS_VALUE));

              dayOfWeek = cmdclsValue;
            }
          }
          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 (!dayOfWeek.equals("")) {
            String[] strArray = dayOfWeek.split("\\,", 100);
            for (String s : strArray) {
              if (s.equals("MON")) {
                util.getView(act, R.id.llMon).setSelected(true);
              } else if (s.equals("TUE")) {
                util.getView(act, R.id.llTue).setSelected(true);
              } else if (s.equals("WED")) {
                util.getView(act, R.id.llWed).setSelected(true);
              } else if (s.equals("THU")) {
                util.getView(act, R.id.llThu).setSelected(true);
              } else if (s.equals("FRI")) {
                util.getView(act, R.id.llFri).setSelected(true);
              } else if (s.equals("SAT")) {
                util.getView(act, R.id.llSat).setSelected(true);
              } else if (s.equals("SUN")) {
                util.getView(act, R.id.llSun).setSelected(true);
              }
            }
          }
          break;
        }
      }
    }
  }

  /**
   * 요일 셋팅.
   *
   * @return 셋팅할 요일
   */
  private Boolean setsDayOfWeek() {



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

    String[] alItem = util.getStringArrayValues(act, "DAYOFWEEK_ITEM");
    String[] alText = util.getStringArrayValues(act, "DAYOFWEEK_TEXT");
    String tempDayOfWeek1 = "";
    tempDayOfWeek1 = tempDayOfWeek;
    String[] strArray = tempDayOfWeek.split("\\,", 100);
    for (String s : strArray) {
      int idx = util.getArraryFind(alItem, s);
      tempDayOfWeek1 = tempDayOfWeek1.replaceAll(s, alText[idx]);
    }

    HashMap<String, Object> tempItemSubMap = new HashMap<String, Object>();
    String tempSourceId = "";
    String tempSourceSubId = "";
    String tempConditionTypeCode = "09";
    String tempCmdclsValue = tempDayOfWeek;
    String tempCmdclsValueMsg = tempDayOfWeek1;

    tempItemSubMap.put(Constants.KEY_SOURCE_ID, tempSourceId);
    tempItemSubMap.put(Constants.KEY_SOURCE_SUB_ID, tempSourceSubId);
    tempItemSubMap.put(Constants.KEY_CONDITION_TYPE_CODE, tempConditionTypeCode);
    tempItemSubMap.put(Constants.KEY_CMDCLS_VALUE, tempCmdclsValue);
    tempItemSubMap.put(Constants.KEY_CMDCLS_VALUE_MSG, tempCmdclsValueMsg);

    ArrayList<HashMap<String, Object>> tempItemSubList = new ArrayList<>();
    tempItemSubList.add(tempItemSubMap);

    HashMap<String, Object> hmTempCondition = new HashMap<String, Object>();
    hmTempCondition.put(Constants.KEY_ITEM_NAME, "");
    hmTempCondition.put(Constants.KEY_ITEM_SUB_TYPE_CODE, "22");
    hmTempCondition.put(Constants.KEY_ITEM_SUB, tempItemSubList);

    boolean isSet = false;
    if (conditions.size() > 0) {
      for (int row = 0; row < conditions.size(); row++) {
        HashMap<String, Object> hmTemp;
        hmTemp = conditions.get(row);

        String itemSubTypeCode = util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB_TYPE_CODE));
        if (itemSubTypeCode.equals("22")) {
          conditions.set(row, hmTempCondition);
          isSet = true;
          break;
        }
      }

      if (!isSet) {
        conditions.add(hmTempCondition);
      }
    } else {
      conditions.add(hmTempCondition);
    }

    return true;
  }

  /**
   * keyboard display.
   */
  void setKeyDisp() {

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

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

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

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

      }

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

      }

      @Override
      public void afterTextChanged(Editable editable) {

        if (util.null2int(editable.toString()) > 12) {
          edtHour.setText("12");
        } else if (util.null2string(editable.toString()).equals("")) {
          edtHour.setText("12");
        } else if (editable.toString().length() > 1) {
          if (util.null2int(editable.toString()) == 0) {
            edtHour.setText("12");
          } else {
            int currentHour = util.null2int(editable.toString());
            if (currentHour <= hourEnd) {
              if (currentHour == hourStart) {
                tvUpHour.setText(decimalFormat.format(hourEnd));
              } else {
                tvUpHour.setText(decimalFormat.format(currentHour - 1));
              }
              if (currentHour < hourEnd) {
                tvDownHour.setText(decimalFormat.format(currentHour + 1));
              } else {
                tvDownHour.setText(decimalFormat.format(hourStart));
              }
            } else {
              currentHour = hourStart;
              tvUpHour.setText(decimalFormat.format(hourEnd));
              tvDownHour.setText(decimalFormat.format(currentHour + 1));
            }
            edtHour.selectAll();
          }
        } else {
          if (util.null2int(editable.toString()) > 1) {
            edtHour.setText("0" + editable.toString());
          } else if (util.null2int(editable.toString()) > 0) {
            int currentHour = util.null2int(editable.toString());
            if (currentHour <= hourEnd) {
              if (currentHour == hourStart) {
                tvUpHour.setText(decimalFormat.format(hourEnd));
              } else {
                tvUpHour.setText(decimalFormat.format(currentHour - 1));
              }
              if (currentHour < hourEnd) {
                tvDownHour.setText(decimalFormat.format(currentHour + 1));
              } else {
                tvDownHour.setText(decimalFormat.format(hourStart));
              }
            } else {
              currentHour = hourStart;
              tvUpHour.setText(decimalFormat.format(hourEnd));
              tvDownHour.setText(decimalFormat.format(currentHour + 1));
            }
          }
        }
      }
    });
    edtMinute.setImeOptions(EditorInfo.IME_ACTION_DONE); // 키보드 확인 버튼 클릭시
    edtMinute.setOnEditorActionListener(new TextView.OnEditorActionListener() {
      @Override
      public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {

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

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

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

      }

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

      }

      @Override
      public void afterTextChanged(Editable editable) {

        if (util.null2int(editable.toString()) > 59) {
          edtMinute.setText("59");
        } else if (util.null2string(editable.toString()).equals("")) {
          edtMinute.setText("00");
        } else if (editable.toString().length() > 1) {
          int currentMinute = util.null2int(editable.toString());
          if (currentMinute > minuteStart) {
            tvUpMinute.setText(decimalFormat.format(currentMinute - 1));
            if (currentMinute < minuteEnd) {
              tvDownMinute.setText(decimalFormat.format(currentMinute + 1));
            } else {
              tvDownMinute.setText(decimalFormat.format(minuteStart));
            }
          } else {
            currentMinute = minuteStart;
            tvUpMinute.setText(decimalFormat.format(minuteEnd));
            tvDownMinute.setText(decimalFormat.format(currentMinute + 1));
          }
          edtMinute.selectAll();
        } else {
          if (util.null2int(editable.toString()) > 5) {
            edtMinute.setText("0" + editable.toString());
          } else if (util.null2int(editable.toString()) > 0) {
            int currentMinute = util.null2int(editable.toString());
            if (currentMinute <= minuteEnd) {
              if (currentMinute == hourStart) {
                tvUpMinute.setText(decimalFormat.format(minuteEnd));
              } else {
                tvUpMinute.setText(decimalFormat.format(currentMinute - 1));
              }
              if (currentMinute < minuteEnd) {
                tvDownMinute.setText(decimalFormat.format(currentMinute + 1));
              } else {
                tvDownMinute.setText(decimalFormat.format(minuteStart));
              }
            } else {
              currentMinute = minuteStart;
              tvUpMinute.setText(decimalFormat.format(minuteEnd));
              tvDownMinute.setText(decimalFormat.format(currentMinute + 1));
            }
          }
        }
      }
    });
  }

  @Override
  public void onClick(View view) {

    int currentHour = util.null2int(edtHour.getText());
    int currentMinute = util.null2int(edtMinute.getText());
    String ampm = util.null2string(tvAmpm.getText());
    switch (view.getId()) {
      case R.id.llBtn1:
        if (setsDayOfWeek()) {

          String hour = "";
          hour = decimalFormat.format(currentHour + (ampm.equals(
              util.getString(act, R.string.layout_pm)) ? 12 : 0));
          String minute = "";
          minute = decimalFormat.format(currentMinute);
          String[] saTime = util.LocaltoUTC3(hour + minute);
          String sourceId = "";
          itemSubMap.put(Constants.KEY_SOURCE_ID, sourceId);
          String sourceSubId = "";
          itemSubMap.put(Constants.KEY_SOURCE_SUB_ID, sourceSubId);
          String conditionTypeCode = "";
          itemSubMap.put(Constants.KEY_CONDITION_TYPE_CODE, conditionTypeCode);
          String cmdclsValue = "";
          itemSubMap.put(Constants.KEY_CMDCLS_VALUE, cmdclsValue);
          String dataTypeCode = "";
          itemSubMap.put(Constants.KEY_DATA_TYPE_CODE, dataTypeCode);
          String dayOfWeek = "";
          itemSubMap.put(Constants.KEY_DAYOFWEEK, dayOfWeek);
          String repeatable = "";
          itemSubMap.put(Constants.KEY_REPEATABLE, repeatable);
          itemSubMap.put(Constants.KEY_HOUR, saTime[0]);
          itemSubMap.put(Constants.KEY_MINUTE, saTime[1]);

          ArrayList<HashMap<String, Object>> itemSubList = new ArrayList<>();
          itemSubList.clear();
          itemSubList.add(itemSubMap);

          triggers.clear();

          HashMap<String, Object> hmTriggers = new HashMap<>();
          hmTriggers.put(Constants.KEY_ITEM_SUB, itemSubList);
          hmTriggers.put(Constants.KEY_ITEM_NAME,
              util.getString(act, R.string.layout_rules_triggers_select_02));
          hmTriggers.put(Constants.KEY_ITEM_SUB_TYPE_CODE, "05");

          triggers.add(hmTriggers);

          intent = new Intent();
          intent.putExtra(Constants.EXTRA_TRIGGERS, triggers);
          intent.putExtra(Constants.EXTRA_CONDITIONS, conditions);
          act.setResult(RESULT_OK, intent);
          finish();
        }
        break;
      case R.id.llBtn2:
        onBackPressed();
        break;
      case R.id.btnUpHH:
      case R.id.tvUpHH:
        if (currentHour > hourStart) {
          currentHour--;
          edtHour.setText(decimalFormat.format(currentHour));
        } else {
          currentHour = hourEnd;
          edtHour.setText(decimalFormat.format(currentHour));
        }

        break;
      case R.id.tvDownHH:
      case R.id.btnDownHH:
        if (currentHour < hourEnd) {
          currentHour++;
          edtHour.setText(decimalFormat.format(currentHour));
        } else {
          currentHour = hourStart;
          edtHour.setText(decimalFormat.format(currentHour));
        }
        break;
      case R.id.btnUpMM:
      case R.id.tvUpMM:
        if (currentMinute > minuteStart) {
          currentMinute--;
          edtMinute.setText(decimalFormat.format(currentMinute));
        } else {
          currentMinute = minuteEnd;
          edtMinute.setText(decimalFormat.format(currentMinute));
        }
        break;
      case R.id.tvDownMM:
      case R.id.btnDownMM:
        if (currentMinute < minuteEnd) {
          currentMinute++;
          edtMinute.setText(decimalFormat.format(currentMinute));
        } else {
          currentMinute = minuteStart;
          edtMinute.setText(decimalFormat.format(currentMinute));
        }
        break;
      case R.id.btnUpAMPM:
      case R.id.tvUpAMPM:
      case R.id.tvDownAMPM:
      case R.id.btnDownAMPM:
        if (ampm.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 (view.isSelected()) {
          view.setSelected(false);
        } else {
          view.setSelected(true);
        }
        break;
      default:
        break;
    }
  }

  @Override
  public void resultCustomDialogResponse(int msgId, int btnResult) {

    switch (msgId) {

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

  @Override
  public void onBackPressed() {

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