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

package com.ntels.onecable.view.rules;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.ntels.onecable.R;
import com.ntels.onecable.common.AppConfig;
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 java.util.Locale;

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

  private Util util = new Util();
  private Intent intent;
  private Activity act;

  private CustomDialog customDialog = new CustomDialog();
  private String date = "";

  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

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

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

    this.setContentView(viewToLoad);
    if (null != getIntent()) {
      Intent intent = getIntent();
      date = util.null2string(intent.getStringExtra(Constants.EXTRA_DATE));
    }

    displayListView();
  }

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

    util.getView(act, R.id.llBtn1).setOnClickListener(this);
    util.getView(act, R.id.llBtn2).setOnClickListener(this);

    String date1 = "";
    String date2 = "";

    if (date.equals("")) {
      date1 = util.getRealDate1();
      date2 = util.getRealDate1();
    } else {
      String[] strArray = date.split("\\~", 100);
      for (int row = 0; row < strArray.length; row++) {
        if (strArray[row].equals("")) {
          break;
        }

        if (row == 0) {
          date1 = strArray[row];
        } else {
          date2 = strArray[row];
        }
      }
    }
    if (AppConfig.getLanguageType().equalsIgnoreCase(Locale.ENGLISH.toString())) {
      util.getTextView(act, R.id.tvTitle)
          .setText(getString(R.string.layout_rules_edit_08).substring(4));
    }
    util.getEditText(act, R.id.edtYear1).setText(util.formattedDate(date1, "yyyy-MM-dd", "yyyy"));
    util.getEditText(act, R.id.edtMonth1).setText(util.formattedDate(date1, "yyyy-MM-dd", "MM"));
    util.getEditText(act, R.id.edtDay1).setText(util.formattedDate(date1, "yyyy-MM-dd", "dd"));
    util.getEditText(act, R.id.edtYear2).setText(util.formattedDate(date2, "yyyy-MM-dd", "yyyy"));
    util.getEditText(act, R.id.edtMonth2).setText(util.formattedDate(date2, "yyyy-MM-dd", "MM"));
    util.getEditText(act, R.id.edtDay2).setText(util.formattedDate(date2, "yyyy-MM-dd", "dd"));
  }

  @Override
  public void onClick(View view) {

    switch (view.getId()) {
      case R.id.llBtn1:
        String tempDate1 = util.getEditTextVal(act, R.id.edtYear1) + "-"
            + util.getEditTextVal(act, R.id.edtMonth1) + "-"
            + util.getEditTextVal(act, R.id.edtDay1);
        String tempDate2 = util.getEditTextVal(act, R.id.edtYear2) + "-"
            + util.getEditTextVal(act, R.id.edtMonth2)
            + "-" + util.getEditTextVal(act, R.id.edtDay2);
        if (!util.isDateValid(tempDate1)) {
          customDialog.msgAlert(act, Constants.MSG841);
          return;
        }
        if (!util.isDateValid(tempDate2)) {
          customDialog.msgAlert(act, Constants.MSG842);
          return;
        }
        intent = new Intent();
        intent.putExtra(Constants.EXTRA_DATE, tempDate1 + "~" + tempDate2);
        act.setResult(RESULT_OK, intent);
        finish();
        break;
      case R.id.llBtn2:
        onBackPressed();
        break;
      default:
        break;
    }
  }

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

    switch (msgId) {

      case Constants.MSG841:
        break;
      case Constants.MSG842:
        break;
      default:
        break;
    }
  }

  @Override
  public void onBackPressed() {

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