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

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

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.db.SQLManager;
import com.ntels.kneet.v3.common.c_interface.Main_Interface;
import com.ntels.kneet.v3.common.util.Dlog;
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.util.ArrayList;
import java.util.HashMap;

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

    /*툴바 관련*/
    private Toolbar toolbar;

    /*통신 관련*/
    //    private              ArrayList<HashMap<String, Object>> alTotalList                                 = new ArrayList<HashMap<String, Object>>();
    //    private              int                                iType                                       = 0;
    //    private static final int                                MSG_COM_G_RULES_ITEM_DEVICES_ITEM_TYPE_CODE = 1;

    //    private static final int                                MSG_COM_P_MEMBER_PROFILE_IMAGE = 1;
    //    private static final int                                MSG_COM_G_MEMBER               = 2;
    //    private static final int                                MSG_COM_P_MEMBER_PUSH_NOTI     = 3;

    /*공통*/
    private Activity       act;
    private AppConfig      appConfig;
    private Main_Interface mCallback;
    private Intent         intent;
    private Util         util              = new Util();
    private CustomDialog customDialog      = new CustomDialog();
    private String       sCallType         = "";
    private SQLManager   sqlManager        = new SQLManager();
    private boolean      bReloadFinishFlag = false;
    ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
    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 int                                iDevicesPosition   = -1;
    private String                             sDevices           = "";
    private String                             sDevices_Code      = "";
    private ArrayList<String>                  alDevicesTitle     = new ArrayList<String>();
    private ArrayList<String>                  alDevicesId        = new ArrayList<String>();
    private ArrayList<String>                  alDevicesImage     = new ArrayList<String>();
    private ArrayList<String>                  alDevicesViewImage = new ArrayList<String>();

    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

        if ( savedInstanceState != null ) {
            // sTitle =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_TITLE);
            // aList1 =
            // savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST1);
            // aList2 =
            // savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST2);
            // // 0: 세로 한개 선택, 1: 가로 한개선택
            // iType = savedInstanceState.getInt(Constants.EXTRA_SPINNER_TYPE,
            // 0);
            // sBtn1 =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN1);
            // sBtn2 =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN2);
            // iPosition =
            // savedInstanceState.getInt(Constants.EXTRA_SPINNER_POSITION, 0);
        } else {
            Intent intent = getIntent();
            //EXTRA_CALL_TYPE =
            //            sCallType = intent.getStringExtra(Constants.EXTRA_CALL_TYPE);
            alList_Triggers = (ArrayList<HashMap<String, Object>>) intent.getSerializableExtra(Constants.EXTRA_TRIGGERS);
            alList_Conditions = (ArrayList<HashMap<String, Object>>) intent.getSerializableExtra(Constants.EXTRA_CONDITIONS);
        }

        Dlog.v("alList_Triggers2 = ", alList_Triggers.toString());
        Dlog.v("alList_Actions2 = ", alList_Conditions.toString());
        setDisp();
    }

    private void setDisp() {

        util.getView(act, R.id.llIcon1).setOnClickListener(this);
        util.getView(act, R.id.llIcon2).setOnClickListener(this);
        util.getView(act, R.id.llIcon3).setOnClickListener(this);
        util.getView(act, R.id.llIcon4).setOnClickListener(this);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);
        if ( alList_Triggers.size() > 0 ) {

            String sHTTP_item_sub_type_code = util.null2string(alList_Triggers.get(0).get(Constants.HTTP_item_sub_type_code));

            if ( sHTTP_item_sub_type_code.equals("05") ) {
                //time
                util.getView(act, R.id.llIcon1).performClick();
            } else if ( sHTTP_item_sub_type_code.equals("32") ) {
                //sun
                util.getView(act, R.id.llIcon2).performClick();
            } else if ( sHTTP_item_sub_type_code.equals("31") ) {
                //더울 때 /추울때
                util.getView(act, R.id.llIcon3).performClick();
            } else if ( sHTTP_item_sub_type_code.equals("01") ) {
                //상태가 변할때
                util.getView(act, R.id.llIcon4).performClick();
            }
        } else {
            util.getView(act, R.id.llPageRoot).setVisibility(View.VISIBLE);
        }
    }
    @Override public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override public void onClick(View v) {

        Intent intent;
        switch (v.getId()) {
            case R.id.llBtn1:
                onBackPressed();
                break;
            case R.id.llIcon1:
                util.callBtnGoogleAnalytics(act, "Set_Trigger", "Trigger_Time");
                util.getView(act, R.id.llPageRoot).setVisibility(View.INVISIBLE);
                intent = new Intent(act, Rules_Triggers_Time.class);
                //                intent.putExtra(Constants.EXTRA_CALL_TYPE, "1");
                intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_TRIGGERS_TIME);
                break;
            case R.id.llIcon2:
                util.callBtnGoogleAnalytics(act, "Set_Trigger", "Trigger_Sun");
                util.getView(act, R.id.llPageRoot).setVisibility(View.INVISIBLE);
                intent = new Intent(act, Rules_Triggers_Sun.class);
                //                intent.putExtra(Constants.EXTRA_CALL_TYPE, "1");
                intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_TRIGGERS_SUN);
                break;
            case R.id.llIcon3:
                util.callBtnGoogleAnalytics(act, "Set_Trigger", "Trigger_Temp");
                util.getView(act, R.id.llPageRoot).setVisibility(View.INVISIBLE);
                intent = new Intent(act, Rules_Triggers_Temp.class);
                //                intent.putExtra(Constants.EXTRA_CALL_TYPE, "1");
                intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_TRIGGERS_TEMP);
                break;
            case R.id.llIcon4:
                util.callBtnGoogleAnalytics(act, "Set_Trigger", "Trigger_Things");
                int iRow = 0;
                int iRow1 = 0;
                String sHTTP_source_id = "";
                String sHTTP_source_sub_id = "";
                if ( alList_Triggers.size() > 0 ) {
                    ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                    if ( !util.null2string(alList_Triggers.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                        alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Triggers.get(iRow).get(Constants.HTTP_item_sub);
                        sHTTP_source_id = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_source_id));
                        sHTTP_source_sub_id = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_source_sub_id));
                        String sHTTP_available_yn = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_available_yn));

                        if ( sHTTP_available_yn.equals("N") ) {
                            util.setShowToast(act, util.getString(act, R.string.MSG900), Toast.LENGTH_SHORT);
                            onBackPressed();
                            return;
                        }
                    }
                }
                util.getView(act, R.id.llPageRoot).setVisibility(View.INVISIBLE);
                intent = new Intent(act, Rules_Things_Select.class);
                intent.putExtra(Constants.EXTRA_CALL_TYPE, "1");
                intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                intent.putExtra(Constants.EXTRA_DEVICE_ID, sHTTP_source_id);
                intent.putExtra(Constants.EXTRA_NODE_ID, sHTTP_source_sub_id);
                intent.putExtra(Constants.EXTRA_POSITION, iRow1);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_THINGS_SELECT);
                break;
            default:
                break;
        }
    }

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

        super.onActivityResult(requestCode, resultCode, data);
        Boolean bReload_Flag = false;
        if ( resultCode == RESULT_OK ) {
            switch (requestCode) {
                case Constants.ACT_RULES_TRIGGERS_TIME:
                case Constants.ACT_RULES_TRIGGERS_SUN:
                case Constants.ACT_RULES_TRIGGERS_TEMP:
                case Constants.ACT_RULES_THINGS_SELECT:
                    act.setResult(RESULT_OK, data);
                    finish();
                    break;
            }
        } else {
            switch (requestCode) {
                case Constants.ACT_RULES_TRIGGERS_TIME:
                case Constants.ACT_RULES_TRIGGERS_SUN:
                case Constants.ACT_RULES_TRIGGERS_TEMP:
                case Constants.ACT_RULES_THINGS_SELECT:
                    if ( alList_Triggers.size() > 0 ) {
                        act.setResult(RESULT_CANCELED, data);
                        finish();
                    } else {
                        util.getView(act, R.id.llPageRoot).setVisibility(View.VISIBLE);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override public void onBackPressed() {

        Intent intent = new Intent();
        intent.putExtra(Constants.EXTRA_RELOAD_FLAG, bReloadFinishFlag);
        this.setResult(RESULT_CANCELED, intent);
        finish();
    }

    Boolean mFlag = false;

    @Override protected void onDestroy() {

        super.onDestroy();
    }

    @Override public void setAnimFinish(String sVal) {

        super.setAnimFinish(sVal);
    }

    Handler mHandler = new Handler(new FinishHandlerCallBack());

    class FinishHandlerCallBack implements Handler.Callback {

        @Override public boolean handleMessage(Message msg) {

            if ( msg.what == 0 ) {
                mFlag = false;
            }
            return true;
        }
    }

    @Override public void onResume() {

        super.onResume();

        // if (!GreenBilllab.getIntro()) {
        // if (Main.reLoginCheck()) {
        // onBackPressed();
        // }
        // }
    }

    @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.MSG807:
//                if ( iBtnResult == 1 ) {
//                    //                    ComHandler.sendMessageDelayed(ComHandler.obtainMessage(iType), 10);
//                } else {
//                    act.finish();
//                }
//                break;
//            case Constants.MSG840:
//                act.finish();
//                break;
            default:
                break;
        }
    }
}
