/*
 * 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.c_interface.OnDataSendToReslut;
import com.ntels.onecable.common.http.HttpSend;
import com.ntels.onecable.common.http.Response;
import com.ntels.onecable.common.util.Dlog;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActivity;
import com.ntels.onecable.view.dialog.CustomDialog;
import com.ntels.onecable.view.dialog.DialogSpinner;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.HashMap;

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

    private static final int MSG_COM_G_COMMON_WEATHER_LOCATION_CODES = 1;

    Handler mComHandler = new Handler(new IncomingHandlerCallBack());
    private Util mUtil = new Util();
    private Intent mIntent;
    private Activity mActivity;
    private String mDayOfWeek = "";
    private ArrayList<HashMap<String, Object>> mTriggers = new ArrayList<>();
    private ArrayList<HashMap<String, Object>> mConditions = new ArrayList<>();
    private HashMap<String, Object> mItemSubMap = new HashMap<>();
    /*통신 관련*/
    private int mType = 0;
    private CustomDialog mCustomDialog = new CustomDialog();
    private int mAreaPosition = -1;
    private String mArea = "";
    private String mAreaCode = "";
    private ArrayList<String> mAlAreaTitle = new ArrayList<>();
    private ArrayList<String> mAlAreaId = new ArrayList<>();

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

        super.onCreate(savedInstanceState);

        mActivity = RulesTriggersTemp.this;
        mCustomDialog.setOnMsgAlterToResultEvent(this);

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

        this.setContentView(viewToLoad);
        if (null != getIntent()) {
            Intent intent = getIntent();

            mTriggers = (ArrayList<HashMap<String, Object>>)
                    intent.getSerializableExtra(Constants.EXTRA_TRIGGERS);
            mConditions = (ArrayList<HashMap<String, Object>>)
                    intent.getSerializableExtra(Constants.EXTRA_CONDITIONS);
        }

        displayListView();
    }

    /**
     * 온도 trigger 가져오기.
     */
    @SuppressWarnings("unchecked")
    void getTriggersHot() {

        if (mTriggers.size() > 0) {
            int row = 0;
            int row1 = 0;
            String itemSubTypeCode = mUtil.null2string(
                    mTriggers.get(row).get(Constants.KEY_ITEM_SUB_TYPE_CODE));
            ArrayList<HashMap<String, Object>> itemSubMap;
            if (!mUtil.null2string(mTriggers.get(row).get(Constants.KEY_ITEM_SUB)).equals("")) {
                itemSubMap = (ArrayList<HashMap<String, Object>>) mTriggers.get(row)
                        .get(Constants.KEY_ITEM_SUB);
                if (itemSubTypeCode.equals("31")) {
                    this.mItemSubMap = itemSubMap.get(row1);

                    String sourceId = mUtil.null2string(this.mItemSubMap.get(Constants.KEY_SOURCE_ID));
                    String sourceName = mUtil.null2string(this.mItemSubMap.get(Constants.KEY_SOURCE_NAME));
                    String conditionTypeCode = mUtil.null2string(
                            this.mItemSubMap.get(Constants.KEY_CONDITION_TYPE_CODE));

                    if (conditionTypeCode.equals("04")) {
                        mUtil.getView(mActivity, R.id.btnHot1).setSelected(true);
                        mUtil.getView(mActivity, R.id.btnHot2).setSelected(false);
                    } else if (conditionTypeCode.equals("02")) {
                        mUtil.getView(mActivity, R.id.btnHot1).setSelected(false);
                        mUtil.getView(mActivity, R.id.btnHot2).setSelected(true);
                    }
                    mAreaCode = sourceId;
                    mArea = sourceName;
                    mUtil.getTextView(mActivity, R.id.tvArea).setText(mArea);
//                    mUtil.getViewUnderline(mActivity, R.id.tvArea);
                    String cmdclsValue = mUtil.null2string(this.mItemSubMap.get(Constants.KEY_CMDCLS_VALUE));
                    String plusMinus = cmdclsValue.substring(0, 1);
                    String temperature = "";
                    if ("-".equals(plusMinus)) {
                        mUtil.getImageView(mActivity, R.id.ivTempIcon).setImageResource(R.drawable.d_img_btn_temper_below);
                        mUtil.getImageView(mActivity, R.id.ivTempIcon).setTag("1");
                        temperature = cmdclsValue.substring(1);
                    } else {
                        mUtil.getImageView(mActivity, R.id.ivTempIcon).setImageResource(R.drawable.d_img_btn_temper_above);
                        mUtil.getImageView(mActivity, R.id.ivTempIcon).setTag("0");
                        temperature = cmdclsValue;
                    }
                    mUtil.getEditText(mActivity, R.id.edtTemperature).setText(temperature);
//                    mUtil.getViewUnderline(mActivity, R.id.tvTemperature);
                }
            }
        }
    }

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

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

                String itemSubTypeCode = mUtil.null2string(hmTemp.get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                if (itemSubTypeCode.equals("22")) {
                    //요일
                    ArrayList<HashMap<String, Object>> itemSubList;
                    if (!mUtil.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 = mUtil.null2string(tempItemSubMap.get(
                                    Constants.KEY_CMDCLS_VALUE));

                            mDayOfWeek = cmdclsValue;
                        }
                    }
                    mUtil.getView(mActivity, R.id.llMon).setSelected(false);
                    mUtil.getView(mActivity, R.id.llTue).setSelected(false);
                    mUtil.getView(mActivity, R.id.llWed).setSelected(false);
                    mUtil.getView(mActivity, R.id.llThu).setSelected(false);
                    mUtil.getView(mActivity, R.id.llFri).setSelected(false);
                    mUtil.getView(mActivity, R.id.llSat).setSelected(false);
                    mUtil.getView(mActivity, R.id.llSun).setSelected(false);
                    if (!mDayOfWeek.equals("")) {
                        String[] strArray = mDayOfWeek.split("\\,", 100);
                        for (String s : strArray) {
                            if (s.equals("MON")) {
                                mUtil.getView(mActivity, R.id.llMon).setSelected(true);
                            } else if (s.equals("TUE")) {
                                mUtil.getView(mActivity, R.id.llTue).setSelected(true);
                            } else if (s.equals("WED")) {
                                mUtil.getView(mActivity, R.id.llWed).setSelected(true);
                            } else if (s.equals("THU")) {
                                mUtil.getView(mActivity, R.id.llThu).setSelected(true);
                            } else if (s.equals("FRI")) {
                                mUtil.getView(mActivity, R.id.llFri).setSelected(true);
                            } else if (s.equals("SAT")) {
                                mUtil.getView(mActivity, R.id.llSat).setSelected(true);
                            } else if (s.equals("SUN")) {
                                mUtil.getView(mActivity, R.id.llSun).setSelected(true);
                            }
                        }
                    }
                    break;
                }
            }
        }
    }

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

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

        String[] items = mUtil.getStringArrayValues(mActivity, "DAYOFWEEK_ITEM");
        String[] texts = mUtil.getStringArrayValues(mActivity, "DAYOFWEEK_TEXT");
        String tempDayOfWeek1 = "";
        tempDayOfWeek1 = tempDayOfWeek;
        String[] strArray = tempDayOfWeek.split("\\,", 100);
        for (String s : strArray) {
            int idx = mUtil.getArraryFind(items, s);
            tempDayOfWeek1 = tempDayOfWeek1.replaceAll(s, texts[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 (mConditions.size() > 0) {
            for (int row = 0; row < mConditions.size(); row++) {
                HashMap<String, Object> hmTemp;
                hmTemp = mConditions.get(row);

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

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

        return true;
    }

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

//        mUtil.getTextView(mActivity, R.id.tvArea).setText(mUtil.getString(mActivity, R.string.layout_selected));
//        mUtil.getViewUnderline(mActivity, R.id.tvArea);
//        mUtil.getTextView(mActivity, R.id.tvTemperature).setText(mUtil.getString(mActivity, R.string.layout_selected));
//        mUtil.getViewUnderline(mActivity, R.id.tvTemperature);
        mUtil.getView(mActivity, R.id.btnHot1).setSelected(true);
        mUtil.getView(mActivity, R.id.btnHot2).setSelected(false);
        mUtil.getView(mActivity, R.id.tvArea).setOnClickListener(this);
//        mUtil.getView(mActivity, R.id.tvTemperature).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.btnHot1).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.btnHot2).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llMon).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llTue).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llWed).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llThu).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llFri).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llSat).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llSun).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llBtn1).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llBtn2).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.ivTempIcon).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.ivTempIcon).setTag("0");
        mUtil.getView(mActivity, R.id.llMon).setSelected(true);
        mUtil.getView(mActivity, R.id.llTue).setSelected(true);
        mUtil.getView(mActivity, R.id.llWed).setSelected(true);
        mUtil.getView(mActivity, R.id.llThu).setSelected(true);
        mUtil.getView(mActivity, R.id.llFri).setSelected(true);
        mUtil.getView(mActivity, R.id.llSat).setSelected(true);
        mUtil.getView(mActivity, R.id.llSun).setSelected(true);

        getTriggersHot();

        getDayOfWeek();


        mComHandler.sendMessageDelayed(mComHandler.obtainMessage(MSG_COM_G_COMMON_WEATHER_LOCATION_CODES), Constants.HANDLER_DELAY_MILLIS);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.ivTempIcon:
                if ("0".equals(view.getTag())) {
                    ((ImageView) view).setImageResource(R.drawable.d_img_btn_temper_below);
                    view.setTag("1");
                } else {
                    ((ImageView) view).setImageResource(R.drawable.d_img_btn_temper_above);
                    view.setTag("0");
                }
                break;
            case R.id.llBtn1:
                if (mAreaPosition == -1) {
                    mCustomDialog.msgAlert(mActivity, Constants.MSG860);
                    return;
                }
                if (mUtil.getEditTextVal(mActivity, R.id.edtTemperature).equals(
                        mUtil.getString(mActivity, R.string.layout_selected))) {
                    mCustomDialog.msgAlert(mActivity, Constants.MSG831);
                    return;
                }
                if (setsDayOfWeek()) {

                    String sourceId = "";
                    String sourceName = "";
                    if (mAreaPosition > -1) {
                        sourceId = mUtil.null2string(mAlAreaId.get(mAreaPosition));
                        sourceName = mUtil.null2string(mAlAreaTitle.get(mAreaPosition));
                    } else {
                        mCustomDialog.msgAlert(mActivity, Constants.MSG860);
                        return;
                    }
                    String conditionTypeCode = "";
                    if (mUtil.getView(mActivity, R.id.btnHot1).isSelected()) {
                        conditionTypeCode = "04";
                    } else {
                        conditionTypeCode = "02";
                    }
                    String cmdclsValue = "";
                    String plusMinus = "0".equals(mUtil.getView(mActivity, R.id.ivTempIcon).getTag()) ? "" : "-";
                    cmdclsValue = plusMinus + mUtil.getEditTextVal(mActivity, R.id.edtTemperature);

                    mItemSubMap.put(Constants.KEY_SOURCE_ID, sourceId);
                    mItemSubMap.put(Constants.KEY_SOURCE_NAME, sourceName);
                    String sourceSubId = "";
                    mItemSubMap.put(Constants.KEY_SOURCE_SUB_ID, sourceSubId);
                    String sourceSubName = "";
                    mItemSubMap.put(Constants.KEY_SOURCE_SUB_NAME, sourceSubName);
                    mItemSubMap.put(Constants.KEY_CONDITION_TYPE_CODE, conditionTypeCode);
                    mItemSubMap.put(Constants.KEY_CMDCLS_VALUE, cmdclsValue);
                    String dataTypeCode = "";
                    mItemSubMap.put(Constants.KEY_DATA_TYPE_CODE, dataTypeCode);
                    String dayOfWeek = "";
                    mItemSubMap.put(Constants.KEY_DAYOFWEEK, dayOfWeek);
                    String repeatable = "";
                    mItemSubMap.put(Constants.KEY_REPEATABLE, repeatable);
                    String hour = "";
                    mItemSubMap.put(Constants.KEY_HOUR, hour);
                    String minute = "";
                    mItemSubMap.put(Constants.KEY_MINUTE, minute);

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

                    mTriggers.clear();

                    HashMap<String, Object> hmTriggers = new HashMap<>();
                    hmTriggers.put(Constants.KEY_ITEM_SUB, itemSubList);
                    hmTriggers.put(Constants.KEY_ITEM_NAME,
                            mUtil.getString(mActivity, R.string.layout_rules_triggers_select_04));
                    hmTriggers.put(Constants.KEY_ITEM_SUB_TYPE_CODE, "31");

                    mTriggers.add(hmTriggers);

                    mIntent = new Intent();
                    mIntent.putExtra(Constants.EXTRA_TRIGGERS, mTriggers);
                    mIntent.putExtra(Constants.EXTRA_CONDITIONS, mConditions);
                    mActivity.setResult(RESULT_OK, mIntent);
                    finish();
                }
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;
            case R.id.btnHot1:
                mUtil.getView(mActivity, R.id.btnHot1).setSelected(true);
                mUtil.getView(mActivity, R.id.btnHot2).setSelected(false);
                break;
            case R.id.btnHot2:
                mUtil.getView(mActivity, R.id.btnHot1).setSelected(false);
                mUtil.getView(mActivity, R.id.btnHot2).setSelected(true);
                break;
            case R.id.tvArea:
                mIntent = new Intent();
                mIntent.setClass(mActivity, DialogSpinner.class);
                mIntent.putExtra(Constants.EXTRA_SPINNER_LIST1, mAlAreaTitle);
                mIntent.putExtra(Constants.EXTRA_SPINNER_TITLE, mUtil.getString(mActivity, R.string.layout_rules_location_select_01));
                mIntent.putExtra(Constants.EXTRA_SPINNER_TYPE, 0);
                mIntent.putExtra(Constants.EXTRA_SPINNER_SHOW_TYPE, 1);
                mIntent.putExtra(Constants.EXTRA_SPINNER_POSITION, mAreaPosition);
                mUtil.callActivityForResult(mActivity, mIntent, Constants.ACT_SPINNER, Constants.CHANGE_EVENT_BOTTOM_BOTTOM);
                break;
            /*case R.id.tvTemperature:
                mIntent = new Intent();
                mIntent.setClass(mActivity, RulesTemperatureSelect.class);
                String temp = mUtil.getTextViewVal(mActivity, R.id.tvTemperature);
                temp = temp.replaceAll(mUtil.getString(mActivity, R.string.layout_temperature1), "")
                        .replaceAll(mUtil.getString(mActivity, R.string.layout_selected), "");
                mIntent.putExtra(Constants.EXTRA_TEMPERATURE, temp);
                mUtil.callActivityForResultNoAnim(mActivity, mIntent, Constants.ACT_RULES_TEMPERATURE);
                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;
        }
    }

    /**
     * 야후날씨 지역코드 목록 조회(야후 날씨에 사용할 지역코드목록을 조회한다) 요청.
     */
    private void sendGetCommonWeatherLocationCodes() {

        try {
            HttpSend httpSend;

            httpSend = new HttpSend(mActivity);

            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override
                public void resultResponse(Response result) {

                    resultGetCommonWeatherLocationCodes(result);
                }
            });
            httpSend.getCommonWeatherLocationCode();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * {@link RulesTriggersTemp#sendGetCommonWeatherLocationCodes() 지역코드}목록 조회 요청 결과.
     *
     * @param response response data
     */
    private void resultGetCommonWeatherLocationCodes(Response response) {

        try {
            if (response.getStatusCode() == 200) {
                ArrayList<HashMap<String, Object>> alList = mUtil.jsonToArrayListMap(response.getContent());
                if (alList.size() > 0) {
                    for (int row = 0; row < alList.size(); row++) {
                        HashMap<String, Object> tempMap;
                        tempMap = alList.get(row);
                        String commonCode = mUtil.null2string(tempMap.get(Constants.KEY_COMMON_CODE));
                        String commonCodeName = mUtil.null2string(tempMap.get(Constants.KEY_COMMON_CODE_NAME));
                        mAlAreaId.add(commonCode);
                        mAlAreaTitle.add(commonCodeName);
                        if (commonCode.equals(mAreaCode)) {
                            mAreaPosition = row;
                            String area = mUtil.null2string(mAlAreaTitle.get(mAreaPosition));
                            mUtil.getTextView(mActivity, R.id.tvArea).setText(area);
//                            mUtil.getViewUnderline(mActivity, R.id.tvArea);
                        }
                    }
                }
            } else {
                JSONObject joResult = new JSONObject(response.getContent());
                Dlog.v(joResult.toString());
//                String errorCode = mUtil.null2string(joResult.get(Constants.KEY_ERR_CODE));
                String resultCode = mUtil.null2string(joResult.get(Constants.KEY_RESULT_CODE));
//                String errorMsg = mUtil.null2string(joResult.get(Constants.KEY_ERR_MSG));
                String resultMsg = mUtil.null2string(joResult.get(Constants.KEY_RESULT_MSG));

                switch (resultCode) {
                    case "401":
                        mCustomDialog.msgAlert(mActivity, Constants.MSG998, resultMsg);
                        break;
                    case "401-106":
                        mCustomDialog.msgAlert(mActivity, Constants.MSG997, resultMsg);
                        break;
                    default:
                        mCustomDialog.msgAlert(mActivity, Constants.MSG000, resultMsg);
                        break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            mCustomDialog.msgAlert(mActivity, Constants.MSG807);
        }
    }

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

        switch (msgId) {

            case Constants.MSG998:
                mUtil.callApp_Start(mActivity, "LOGOUT");
                break;
            case Constants.MSG997:
                mUtil.callApp_Start(mActivity, "HOME_RELOAD");
                break;
            case Constants.MSG805:
                if (btnResult == 1) {
                    // mComHandler.sendMessage(mComHandler.obtainMessage(MSG_COM_DOLOGOUT));
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {

        mIntent = new Intent();
        mActivity.setResult(RESULT_CANCELED, mIntent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                /*case Constants.ACT_RULES_TEMPERATURE:
                    String temp = data.getStringExtra(Constants.EXTRA_TEMPERATURE);
                    mUtil.getTextView(mActivity, R.id.tvTemperature)
                            .setText(temp + mUtil.getString(mActivity, R.string.layout_temperature1));
                    mUtil.getViewUnderline(mActivity, R.id.tvTemperature);
                    break;*/
                case Constants.ACT_SPINNER:
                    if (mAreaPosition != data.getIntExtra(Constants.EXTRA_SPINNER_RESULT, 0)) {
                        mAreaPosition = data.getIntExtra(Constants.EXTRA_SPINNER_RESULT, 0);
                        mArea = mUtil.null2string(mAlAreaTitle.get(mAreaPosition));
                        mUtil.getTextView(mActivity, R.id.tvArea).setText(mArea);
//                        mUtil.getViewUnderline(mActivity, R.id.tvArea);
                    }
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                default:
                    break;
            }
        }
    }

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {

            mType = msg.what;
            switch (msg.what) {
                case MSG_COM_G_COMMON_WEATHER_LOCATION_CODES:
                    sendGetCommonWeatherLocationCodes();
                    break;

                default:
                    break;
            }
            return true;
        }
    }
}
