package com.handset.serialportsensor.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handset.serialportsensor.R;
import com.handset.serialportsensor.account.AccountManager;
import com.handset.serialportsensor.bean.ThresholdBean;
import com.handset.serialportsensor.constant.Constant;
import com.handset.serialportsensor.utils.SpUtil;

public class ThresholdActivity extends AppCompatActivity {
    private EditText etTempLow;
    private EditText etTempHigh;
    private EditText etHumLow;
    private EditText etHumHigh;
    private EditText etPressureLow1;
    private EditText etPressureHigh1;
    private EditText etPressureLow2;
    private EditText etPressureHigh2;


    private EditText etDiffPressureLow;
    private EditText etDiffPressureHigh;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_threshold);
        initView();
        setDefaultValue();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        if(im != null) {
            View view = getCurrentFocus();
            if(view != null) {
                im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }

    private void initView() {
        initTitle();
    }

    private void initTitle() {
        ImageView ivBack = findViewById(R.id.title_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener((View view) -> finish());
        TextView tvTitle = findViewById(R.id.title_text);
        tvTitle.setText(R.string.set_threshold_title);
        etTempLow = findViewById(R.id.et_temp_low);
        etTempHigh = findViewById(R.id.et_temp_high);
        etHumLow = findViewById(R.id.et_humidity_low);
        etHumHigh = findViewById(R.id.et_humidity_high);
        etPressureLow1 = findViewById(R.id.et_pressure1_low);
        etPressureHigh1 = findViewById(R.id.et_pressure1_high);
        etPressureLow2 = findViewById(R.id.et_pressure2_low);
        etPressureHigh2 = findViewById(R.id.et_pressure2_high);
        etDiffPressureHigh = findViewById(R.id.et_pressure_yahcha_high);
        etDiffPressureLow = findViewById(R.id.et_pressure_yacha_low);
        findViewById(R.id.btn_save).setOnClickListener((View view) -> save());
    }

    private void save() {
        if (check()) {
            ThresholdBean bean = new ThresholdBean();
            bean.setTemperatureFloor(etTempLow.getText().toString());
            bean.setTemperatureCeiling(etTempHigh.getText().toString());
            bean.setHumidityFloor(etHumLow.getText().toString());
            bean.setHumidityCeiling(etHumHigh.getText().toString());
            bean.setInnerPressureFloor(etPressureLow1.getText().toString());
            bean.setInnerPressureCeiling(etPressureHigh1.getText().toString());
            bean.setOuterPressureFloor(etPressureLow2.getText().toString());
            bean.setOuterPressureCeiling(etPressureHigh2.getText().toString());
            bean.setDiffPressureFloor(etDiffPressureLow.getText().toString());
            bean.setDiffPressureCeiling(etDiffPressureHigh.getText().toString());
            SpUtil.put(this, Constant.THRESHOLD_KEY, new Gson().toJson(bean));

            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            AccountManager.get().putUserData("ThresholdBean", "ThresholdBean");
            finish();
        }
    }

    private boolean check() {
        if (!checkDataFormat(etTempLow.getText().toString())) {
            Toast.makeText(this, "温度下限格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!checkDataFormat(etTempHigh.getText().toString())) {
            Toast.makeText(this, "温度上限格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!checkDataFormat(etHumLow.getText().toString())) {
            Toast.makeText(this, "湿度下限格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!checkDataFormat(etHumHigh.getText().toString())) {
            Toast.makeText(this, "湿度上限格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!checkDataFormat(etPressureLow1.getText().toString())) {
            Toast.makeText(this, "内压下限格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!checkDataFormat(etPressureHigh1.getText().toString())) {
            Toast.makeText(this, "内压上限格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!checkDataFormat(etPressureLow2.getText().toString())) {
            Toast.makeText(this, "外压下限格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!checkDataFormat(etPressureHigh2.getText().toString())) {
            Toast.makeText(this, "外压上限格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!checkDataFormat(etDiffPressureLow.getText().toString())) {
            Toast.makeText(this, "压差下限格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!checkDataFormat(etDiffPressureHigh.getText().toString())) {
            Toast.makeText(this, "压差上限格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (!between(checkDataFormatF(etTempHigh.getText().toString()), 85f, -45f)
                || !between(checkDataFormatF(etTempLow.getText().toString()), 85f, -45f)) {
            Toast.makeText(this, "温度范围只能输入 -45~85", Toast.LENGTH_SHORT).show();
            return false;
        }


        if (!between(checkDataFormatF(etHumHigh.getText().toString()), 100f, 0f)
                || !between(checkDataFormatF(etHumLow.getText().toString()), 100f, 0f)) {
            Toast.makeText(this, "湿度范围只能输入 0~100", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (!between(checkDataFormatF(etPressureHigh1.getText().toString()), 200f, 0f)
                || !between(checkDataFormatF(etPressureLow1.getText().toString()), 200f, 0f)
                || !between(checkDataFormatF(etPressureHigh2.getText().toString()), 200f, 0f)
                || !between(checkDataFormatF(etPressureLow2.getText().toString()), 200f, 0f)) {
            Toast.makeText(this, "压力范围只能输入 0~200", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (!between(checkDataFormatF(etDiffPressureHigh.getText().toString()), 30f, -20f)
                || !between(checkDataFormatF(etDiffPressureLow.getText().toString()), 30f, -20f)) {
            Toast.makeText(this, "压差范围只能输入 -20~30", Toast.LENGTH_SHORT).show();
            return false;
        }


        if (!compareBS(etTempHigh.getText().toString(), etTempLow.getText().toString())) {
            Toast.makeText(this, "湿度上限不能低于下限", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!compareBS(etHumHigh.getText().toString(), etHumLow.getText().toString())) {
            Toast.makeText(this, "湿度上限不能低于下限", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!compareBS(etPressureHigh1.getText().toString(), etPressureLow1.getText().toString())) {
            Toast.makeText(this, "内压上限不能低于下限", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!compareBS(etPressureHigh2.getText().toString(), etPressureLow2.getText().toString())) {
            Toast.makeText(this, "外压上限不能低于下限", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!compareBS(etDiffPressureHigh.getText().toString(), etDiffPressureLow.getText().toString())) {
            Toast.makeText(this, "压差上限不能低于下限", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    private boolean between(Float f, Float up, Float low) {
        if (f == null || up == null || low == null) {
            return false;
        }
        return low <= f && f <= up;
    }

    private boolean checkDataFormat(String a) {
        return checkDataFormatF(a) != null;
    }

    private Float checkDataFormatF(String a) {
        if (TextUtils.isEmpty(a)) {
            return null;
        }
        try {
            Float f = Float.parseFloat(a);
            return f;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return null;
        }
    }

    private boolean compareBS(String bigger, String smaller) {
        if (TextUtils.isEmpty(bigger) || TextUtils.isEmpty(smaller)) {
            return false;
        }

        try {
            Float bigErf = Float.parseFloat(bigger);
            Float smallErf = Float.parseFloat(smaller);
            if (bigErf >= smallErf) {
                return true;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 配置默认阈值参数
     */

    private void setDefaultValue() {
        ThresholdBean bean = new ThresholdBean();
        bean.setTemperatureFloor("-40");
        bean.setTemperatureCeiling("65");
        bean.setHumidityFloor("0");
        bean.setHumidityCeiling("100");
        bean.setInnerPressureFloor("80");
        bean.setInnerPressureCeiling("150");
        bean.setOuterPressureFloor("80");
        bean.setOuterPressureCeiling("150");
        bean.setDiffPressureFloor("-20");
        bean.setDiffPressureCeiling("30");
        String bean2 = (String) SpUtil.get(this, Constant.THRESHOLD_KEY, "");
        ThresholdBean bean1 = null;
        if (!TextUtils.isEmpty(bean2)) {
            bean1 = new Gson().fromJson(bean2, ThresholdBean.class);
        }

        if (bean1 != null) {
            bean = bean1;
        }
        etTempLow.setText(bean.getTemperatureFloor());
        etTempHigh.setText(bean.getTemperatureCeiling());
        etHumLow.setText(bean.getHumidityFloor());
        etHumHigh.setText(bean.getHumidityCeiling());
        etPressureLow1.setText(bean.getInnerPressureFloor());
        etPressureHigh1.setText(bean.getInnerPressureCeiling());
        etPressureLow2.setText(bean.getOuterPressureFloor());
        etPressureHigh2.setText(bean.getOuterPressureCeiling());
        etDiffPressureHigh.setText(bean.getDiffPressureCeiling());
        etDiffPressureLow.setText(bean.getDiffPressureFloor());
    }
}
