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

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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

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

    /** Called when the activity is first created. */

    private Toolbar   toolbar;
    private AppConfig appConfig;
    Main_Interface mCallback;
    private Util util = new Util();
    private Intent   intent;
    private Activity act;
    private DecimalFormat decimalFormat = new DecimalFormat("00");

    private CustomDialog                       customDialog      = new CustomDialog();
    private String                             sCallType         = "";
    private String                             sDATE             = "";
    private String                             sHour             = "";
    private String                             sMinute           = "";
    private String                             sAMPM             = "";
    private String                             sRepeatble        = "";
    private String                             sDayOfWeek        = "";
    private String                             sDayOfWeek1       = "";
    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 HashMap<String, Object>            hmItem_Sub        = new HashMap<String, Object>();
    private Button btnUpHH, btnDownHH;
    private Button btnUpMM, btnDownMM;
    private Button btnUpAMPM, btnDownAMPM;
    private TextView tvUpHH, tvUpMM, tvUpAMPM, tvAMPM;
    private TextView tvDownHH, tvDownMM, tvDownAMPM;
    private EditText edtHH, edtMM;
    private int iHHStart = 1;
    private int iHHEnd   = 12;
    private int iMMStart = 0;
    private int iMMEnd   = 59;

    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

        this.setContentView(viewToLoad);
        String sTempHour = "";
        if ( savedInstanceState != null ) {

        } else {
            Intent intent = getIntent();
            sDATE = util.null2string(intent.getStringExtra(Constants.EXTRA_DATE));
        }

        displayListView();
    }

    private void displayListView() {

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

        //        setKeyDisp();

        String sDate1 = "";
        String sDate2 = "";

        if ( sDATE.equals("") ) {
            sDate1 = util.getRealDate1();
            sDate2 = util.getRealDate1();
        } else {
            String[] strArray = sDATE.split("\\~", 100);
            for ( int iRow1 = 0; iRow1 < strArray.length; iRow1++ ) {
                if ( strArray[iRow1].equals("") ) {
                    break;
                }

                if ( iRow1 == 0 ) {
                    sDate1 = strArray[iRow1];
                } else {
                    sDate2 = strArray[iRow1];
                }
            }
        }
        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(sDate1, "yyyy-MM-dd", "yyyy"));
        util.getEditText(act, R.id.edtMonth1).setText(util.formattedDate(sDate1, "yyyy-MM-dd", "MM"));
        util.getEditText(act, R.id.edtDay1).setText(util.formattedDate(sDate1, "yyyy-MM-dd", "dd"));
        util.getEditText(act, R.id.edtYear2).setText(util.formattedDate(sDate2, "yyyy-MM-dd", "yyyy"));
        util.getEditText(act, R.id.edtMonth2).setText(util.formattedDate(sDate2, "yyyy-MM-dd", "MM"));
        util.getEditText(act, R.id.edtDay2).setText(util.formattedDate(sDate2, "yyyy-MM-dd", "dd"));
    }

    @Override public void onClick(View v) {

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

    @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.MSG805:
                if ( iBtnResult == 1 ) {
                    // ComHandler.sendMessage(ComHandler.obtainMessage(MSG_COM_DOLOGOUT));
                }
                break;
            default:
                break;
        }
    }

    @Override protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
    }

    @Override public void onBackPressed() {

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