package com.ntels.onecable.view.rules;

import com.ntels.onecable.R;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.util.Dlog;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

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

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

  /*공통*/
  private Activity mActivity;
  private Util mUtil = new Util();
  private ArrayList<HashMap<String, Object>> triggers = new ArrayList<HashMap<String, Object>>();
  private ArrayList<HashMap<String, Object>> conditions = new ArrayList<HashMap<String, Object>>();

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

    super.onCreate(savedInstanceState);

    mActivity = RulesTriggersSelect.this;

    View viewToLoad = LayoutInflater.from(mActivity).inflate(R.layout.rules_triggers_select, null);
    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);
    }

    Dlog.v("alList_Triggers2 = ", triggers.toString());
    Dlog.v("alList_Actions2 = ", conditions.toString());
    setDisp();
  }

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

    mUtil.getView(mActivity, R.id.llIcon1).setOnClickListener(this);
    mUtil.getView(mActivity, R.id.llIcon2).setOnClickListener(this);
    mUtil.getView(mActivity, R.id.llIcon3).setOnClickListener(this);
    mUtil.getView(mActivity, R.id.llIcon4).setOnClickListener(this);
    mUtil.getView(mActivity, R.id.llBtn1).setOnClickListener(this);
    if (triggers.size() > 0) {

      String itemSubTypeCode = mUtil.null2string(
          triggers.get(0).get(Constants.KEY_ITEM_SUB_TYPE_CODE));

      if (itemSubTypeCode.equals("05")) {
        //time
        mUtil.getView(mActivity, R.id.llIcon1).performClick();
      } else if (itemSubTypeCode.equals("32")) {
        //sun
        mUtil.getView(mActivity, R.id.llIcon2).performClick();
      } else if (itemSubTypeCode.equals("31")) {
        //더울 때 /추울때
        mUtil.getView(mActivity, R.id.llIcon3).performClick();
      } else if (itemSubTypeCode.equals("01")) {
        //상태가 변할때
        mUtil.getView(mActivity, R.id.llIcon4).performClick();
      }
    } else {
      mUtil.getView(mActivity, R.id.llPageRoot).setVisibility(View.VISIBLE);
    }
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {

    super.onConfigurationChanged(newConfig);
  }

  @SuppressWarnings("unchecked")
  @Override
  public void onClick(View view) {

    Intent intent;
    switch (view.getId()) {
      case R.id.llBtn1:
        onBackPressed();
        break;
      case R.id.llIcon1:
        mUtil.callBtnGoogleAnalytics(mActivity, "Set_Trigger", "Trigger_Time");
        mUtil.getView(mActivity, R.id.llPageRoot).setVisibility(View.INVISIBLE);
        intent = new Intent(mActivity, RulesTriggersTime.class);
        intent.putExtra(Constants.EXTRA_TRIGGERS, triggers);
        intent.putExtra(Constants.EXTRA_CONDITIONS, conditions);
        mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_RULES_TRIGGERS_TIME);
        break;
      case R.id.llIcon2:
        mUtil.callBtnGoogleAnalytics(mActivity, "Set_Trigger", "Trigger_Sun");
        mUtil.getView(mActivity, R.id.llPageRoot).setVisibility(View.INVISIBLE);
        intent = new Intent(mActivity, RulesTriggersSun.class);
        intent.putExtra(Constants.EXTRA_TRIGGERS, triggers);
        intent.putExtra(Constants.EXTRA_CONDITIONS, conditions);
        mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_RULES_TRIGGERS_SUN);
        break;
      case R.id.llIcon3:
        mUtil.callBtnGoogleAnalytics(mActivity, "Set_Trigger", "Trigger_Temp");
        mUtil.getView(mActivity, R.id.llPageRoot).setVisibility(View.INVISIBLE);
        intent = new Intent(mActivity, RulesTriggersTemp.class);
        intent.putExtra(Constants.EXTRA_TRIGGERS, triggers);
        intent.putExtra(Constants.EXTRA_CONDITIONS, conditions);
        mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_RULES_TRIGGERS_TEMP);
        break;
      case R.id.llIcon4:
        mUtil.callBtnGoogleAnalytics(mActivity, "Set_Trigger", "Trigger_Things");
        int row = 0;
        int row1 = 0;
        String sourceId = "";
        String sourceSubId = "";
        if (triggers.size() > 0) {
          ArrayList<HashMap<String, Object>> itemSubList;
          if (!mUtil.null2string(triggers.get(row).get(Constants.KEY_ITEM_SUB)).equals("")) {
            itemSubList = (ArrayList<HashMap<String, Object>>)
                triggers.get(row).get(Constants.KEY_ITEM_SUB);
            sourceId = mUtil.null2string(itemSubList.get(row1).get(Constants.KEY_SOURCE_ID));
            sourceSubId = mUtil.null2string(itemSubList.get(row1).get(Constants.KEY_SOURCE_SUB_ID));
            String availableYn = mUtil.null2string(
                itemSubList.get(row1).get(Constants.KEY_AVAILABLE_YN));

            if (availableYn.equals("N")) {
              mUtil.setShowToast(mActivity, mUtil.getString(mActivity, R.string.MSG900), Toast.LENGTH_SHORT);
              onBackPressed();
              return;
            }
          }
        }
        mUtil.getView(mActivity, R.id.llPageRoot).setVisibility(View.INVISIBLE);
        intent = new Intent(mActivity, RulesThingsSelect.class);
        intent.putExtra(Constants.EXTRA_CALL_TYPE, "1");
        intent.putExtra(Constants.EXTRA_TRIGGERS, triggers);
        intent.putExtra(Constants.EXTRA_CONDITIONS, conditions);
        intent.putExtra(Constants.EXTRA_DEVICE_ID, sourceId);
        intent.putExtra(Constants.EXTRA_NODE_ID, sourceSubId);
        intent.putExtra(Constants.EXTRA_POSITION, row1);
        mUtil.callActivityForResultNoAnim(mActivity, intent, Constants.ACT_RULES_THINGS_SELECT);
        break;
      default:
        break;
    }
  }

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

    super.onActivityResult(requestCode, resultCode, data);
    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:
          mActivity.setResult(RESULT_OK, data);
          finish();
          break;
        default:
          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 (triggers.size() > 0) {
            mActivity.setResult(RESULT_CANCELED, data);
            finish();
          } else {
            mUtil.getView(mActivity, R.id.llPageRoot).setVisibility(View.VISIBLE);
          }
          break;
        default:
          break;
      }
    }
  }

  @Override
  public void onBackPressed() {

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

  @Override
  public void setAnimFinish(String val) {

    super.setAnimFinish(val);
  }

}
