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 act;
  private Util util = 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);

    act = RulesTriggersSelect.this;

    View viewToLoad = LayoutInflater.from(act).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() {

    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 (triggers.size() > 0) {

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

      if (itemSubTypeCode.equals("05")) {
        //time
        util.getView(act, R.id.llIcon1).performClick();
      } else if (itemSubTypeCode.equals("32")) {
        //sun
        util.getView(act, R.id.llIcon2).performClick();
      } else if (itemSubTypeCode.equals("31")) {
        //더울 때 /추울때
        util.getView(act, R.id.llIcon3).performClick();
      } else if (itemSubTypeCode.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);
  }

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

    Intent intent;
    switch (view.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, RulesTriggersTime.class);
        intent.putExtra(Constants.EXTRA_TRIGGERS, triggers);
        intent.putExtra(Constants.EXTRA_CONDITIONS, 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, RulesTriggersSun.class);
        intent.putExtra(Constants.EXTRA_TRIGGERS, triggers);
        intent.putExtra(Constants.EXTRA_CONDITIONS, 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, RulesTriggersTemp.class);
        intent.putExtra(Constants.EXTRA_TRIGGERS, triggers);
        intent.putExtra(Constants.EXTRA_CONDITIONS, conditions);
        util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_TRIGGERS_TEMP);
        break;
      case R.id.llIcon4:
        util.callBtnGoogleAnalytics(act, "Set_Trigger", "Trigger_Things");
        int row = 0;
        int row1 = 0;
        String sourceId = "";
        String sourceSubId = "";
        if (triggers.size() > 0) {
          ArrayList<HashMap<String, Object>> itemSubList;
          if (!util.null2string(triggers.get(row).get(Constants.KEY_ITEM_SUB)).equals("")) {
            itemSubList = (ArrayList<HashMap<String, Object>>)
                triggers.get(row).get(Constants.KEY_ITEM_SUB);
            sourceId = util.null2string(itemSubList.get(row1).get(Constants.KEY_SOURCE_ID));
            sourceSubId = util.null2string(itemSubList.get(row1).get(Constants.KEY_SOURCE_SUB_ID));
            String availableYn = util.null2string(
                itemSubList.get(row1).get(Constants.KEY_AVAILABLE_YN));

            if (availableYn.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, 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);
        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);
    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;
        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) {
            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, false);
    this.setResult(RESULT_CANCELED, intent);
    finish();
  }

  @Override
  public void setAnimFinish(String val) {

    super.setAnimFinish(val);
  }

}
