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

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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ntels.kneet.v3.R;
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.Dlog;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.common.widget.CustomActivity;
import com.ntels.kneet.v3.view.dialog.CustomDialog;

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

public class Rules_Multisensor_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                             sMultisensorNAME                = "";
    private String                             sMultisensor                    = "";
    private String                             sMultisensorCondition_type_code = "";
    private String                             sMultisensorUnit                = "";
    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 btnUpMultisensor, btnDownMultisensor;
    private TextView tvUpMultisensor;
    private TextView tvDownMultisensor;
    private EditText edtMultisensor;
    private int iEdtMaxLength     = 0;
    private int iMultisensorStep  = 1;
    private int iMultisensorStart = 0;
    private int iMultisensorEnd   = 99;

    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

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

        } else {
            Intent intent = getIntent();
            sMultisensorNAME = util.null2string(intent.getStringExtra(Constants.EXTRA_NAME));
            sMultisensorUnit = util.null2string(intent.getStringExtra(Constants.EXTRA_UNIT));
            iMultisensorStart = intent.getIntExtra(Constants.EXTRA_MIN, iMultisensorStart);
            iMultisensorEnd = intent.getIntExtra(Constants.EXTRA_MAX, iMultisensorEnd);
            iMultisensorStep = intent.getIntExtra(Constants.EXTRA_STEP, iMultisensorStep);
            sMultisensorCondition_type_code = util.null2string(intent.getStringExtra(Constants.EXTRA_CONDITION_TYPE_CODE));
            sMultisensor = util.null2string(intent.getStringExtra(Constants.EXTRA_MULTISENSOR));
        }

        displayListView();
    }

    private void displayListView() {

        util.getView(act, R.id.llRowCheck1).setOnClickListener(this);
        util.getView(act, R.id.llRowCheck2).setOnClickListener(this);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);
        util.getView(act, R.id.llBtn2).setOnClickListener(this);
        btnUpMultisensor = (Button) findViewById(R.id.btnUpMultisensor);
        btnDownMultisensor = (Button) findViewById(R.id.btnDownMultisensor);
        tvUpMultisensor = (TextView) findViewById(R.id.tvUpMultisensor);
        edtMultisensor = (EditText) findViewById(R.id.edtMultisensor);
        tvDownMultisensor = (TextView) findViewById(R.id.tvDownMultisensor);
        btnUpMultisensor.setOnClickListener(this);
        tvUpMultisensor.setOnClickListener(this);
        tvDownMultisensor.setOnClickListener(this);
        btnDownMultisensor.setOnClickListener(this);

        util.setKeyboardHide(act, util.getLinearLayout(act, R.id.llPageRoot));
        util.setKeyboardHide(act, util.getLinearLayout(act, R.id.llSvbody));

        util.getTextView(act, R.id.tvTitle).setText(sMultisensorNAME);

        setKeyDisp();

        iEdtMaxLength = util.null2string(iMultisensorStart + "").length() > util.null2string(iMultisensorEnd + "").length()
                        ? util.null2string(iMultisensorStart + "").length()
                        : util.null2string(iMultisensorEnd + "").length();
        edtMultisensor.setFilters(new InputFilter[]{new InputFilter.LengthFilter(iEdtMaxLength)});

        util.getTextView(act, R.id.tvUnit).setText(sMultisensorUnit);
        if ( sMultisensor.equals("") ) {
            edtMultisensor.setText(iMultisensorStart + "");
        } else {
            edtMultisensor.setText(sMultisensor);
        }
        Dlog.v("sMultisensorCondition_type_code = ", sMultisensorCondition_type_code);
        if ( sMultisensorCondition_type_code.equals("03") ) {
            util.getView(act, R.id.llRowCheck1).setSelected(false);
            util.getView(act, R.id.llRowCheck2).setSelected(true);
        } else if ( sMultisensorCondition_type_code.equals("05") ) {
            util.getView(act, R.id.llRowCheck1).setSelected(true);
            util.getView(act, R.id.llRowCheck2).setSelected(false);
        }
    }

    void setKeyDisp() {

        edtMultisensor.setImeOptions(EditorInfo.IME_ACTION_DONE); // 키보드 확인 버튼 클릭시
        edtMultisensor.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ( actionId == EditorInfo.IME_ACTION_DONE ) {
                    edtMultisensor.clearFocus();
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
                return false;
            }
        });
        edtMultisensor.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {

                if ( !hasFocus ) {
                    if ( util.null2string(Math.abs(util.null2int(edtMultisensor.getText()))).length() == 1 ) {
                        edtMultisensor.setText(edtMultisensor.getText());
                    }
                }
            }
        });
        edtMultisensor.addTextChangedListener(new TextWatcher() {

            @Override public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override public void afterTextChanged(Editable s) {

                try {
                    if ( util.null2int(s.toString()) > iMultisensorEnd ) {
                        edtMultisensor.setText(iMultisensorEnd + "");
                        edtMultisensor.selectAll();
                    } else if ( util.null2string(s.toString()).equals("") ) {
                        edtMultisensor.setText(iMultisensorStart + "");
                        edtMultisensor.selectAll();
                    } else {
                        int iCurentMultisensor = util.null2int(s.toString());
                        if ( iCurentMultisensor > iMultisensorStart ) {
                            tvUpMultisensor.setText((iCurentMultisensor - iMultisensorStep) + "");
                            if ( iCurentMultisensor < iMultisensorEnd ) {
                                tvDownMultisensor.setText((iCurentMultisensor + iMultisensorStep) + "");
                            } else {
                                tvDownMultisensor.setText(iMultisensorStart + "");
                            }
                        } else {
                            tvUpMultisensor.setText(iMultisensorEnd + "");
                            tvDownMultisensor.setText((iMultisensorStart + iMultisensorStep) + "");
                        }

                        if ( util.null2string(Math.abs(util.null2int(edtMultisensor.getText()))).length() == iEdtMaxLength ) {
                            edtMultisensor.selectAll();
                        }
                    }
                } catch (Exception e) {
                    //                    e.printStackTrace();
                }
            }
        });
    }

    @Override public void onClick(View v) {

        int iCurentMultisensor = util.null2int(edtMultisensor.getText());
        switch (v.getId()) {
            case R.id.llBtn1:
                intent = new Intent();
                intent.putExtra(Constants.EXTRA_MULTISENSOR, iCurentMultisensor + "");
                String sCondition_type_code = "";
                if ( util.getView(act, R.id.llRowCheck1).isSelected() ) {
                    sCondition_type_code = "05";
                } else if ( util.getView(act, R.id.llRowCheck2).isSelected() ) {
                    sCondition_type_code = "03";
                } else {
                    util.setShowToast(act, util.getString(act, R.string.MSG850), Toast.LENGTH_SHORT);
                    return;
                }
                intent.putExtra(Constants.EXTRA_CONDITION_TYPE_CODE, sCondition_type_code);
                intent.putExtra(Constants.EXTRA_UNIT, sMultisensorUnit);
                act.setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;
            case R.id.llRowCheck1:
                util.getView(act, R.id.llRowCheck1).setSelected(true);
                util.getView(act, R.id.llRowCheck2).setSelected(false);
                break;
            case R.id.llRowCheck2:
                util.getView(act, R.id.llRowCheck1).setSelected(false);
                util.getView(act, R.id.llRowCheck2).setSelected(true);
                break;
            case R.id.btnUpMultisensor:
            case R.id.tvUpMultisensor:
                if ( iCurentMultisensor > iMultisensorStart ) {
                    iCurentMultisensor -= iMultisensorStep;
                    edtMultisensor.setText(iCurentMultisensor + "");
                    edtMultisensor.selectAll();
                } else {
                    iCurentMultisensor = iMultisensorEnd;
                    edtMultisensor.setText(iCurentMultisensor + "");
                    edtMultisensor.selectAll();
                }
                break;
            case R.id.tvDownMultisensor:
            case R.id.btnDownMultisensor:
                if ( iCurentMultisensor < iMultisensorEnd ) {
                    iCurentMultisensor += iMultisensorStep;
                    edtMultisensor.setText(iCurentMultisensor + "");
                    edtMultisensor.selectAll();
                } else {
                    iCurentMultisensor = iMultisensorStart;
                    edtMultisensor.setText(iCurentMultisensor + "");
                    edtMultisensor.selectAll();
                }
                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();
    }
}
