package com.wd.workoffice.widget;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.wd.workoffice.R;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.WorkUtils;

import java.math.BigDecimal;

import flexible.xd.android_base.utils.ToastUtil;

public class AddAndReduceView extends LinearLayout implements View.OnClickListener {

    private final Button add;
    private final Button remove;
    private final EditText tvNum;
    private String num = "0";
    private double maxNum = Double.MAX_VALUE;

    public AddAndReduceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_add_remove, this);
        add = findViewById(R.id.btn_add);
        remove = findViewById(R.id.btn_reduce);
        tvNum = findViewById(R.id.tv_num);
//        tvNum.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_VARIATION_NORMAL);
        add.setOnClickListener(this);
        remove.setOnClickListener(this);
        tvNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                WorkUtils.keepDecimals(tvNum);
            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    Double.valueOf(s.toString());
                    if (new BigDecimal(Double.valueOf(s.toString())).compareTo(new BigDecimal(maxNum)) == -1) {
                        num = MathUtils.converData(s.toString(), 3);
                        if (mOnNumberChangedListener != null)
                            mOnNumberChangedListener.OnNumberChanged(num);
                        return;
                    }
                    if (new BigDecimal(Double.valueOf(s.toString())).compareTo(new BigDecimal(maxNum)) == 1) {
                        ToastUtil.showShort("目前已选择最大数量");
                        num = MathUtils.converData(maxNum, 3);
                        tvNum.setText(MathUtils.converData(maxNum, 3));
                        if (mOnNumberChangedListener != null)
                            mOnNumberChangedListener.OnNumberChanged(num);
                        return;
                    }
                    if (new BigDecimal(Double.valueOf(s.toString())).compareTo(new BigDecimal(maxNum)) == 0) {
                        ToastUtil.showShort("目前已选择最大数量");
                        num = MathUtils.converData(maxNum, 3);
                        if (mOnNumberChangedListener != null)
                            mOnNumberChangedListener.OnNumberChanged(num);
                        return;
                    }
                    tvNum.setSelection(num.length());
//                    if (new BigDecimal(Double.valueOf(num)).compareTo(new BigDecimal(maxNum)) == 0) {
//                        ToastUtil.showShort("目前已选择最大数量");
//                        return;
//                    }
//                    BigDecimal result = new BigDecimal(maxNum).subtract(new BigDecimal(Double.valueOf(num)));
//                    if (result.compareTo(new BigDecimal("1")) <= 0) {
//                        num = MathUtils.converData(maxNum, 3);
//                        tvNum.setText(MathUtils.converData(maxNum, 3));
//                        if (mOnNumberChangedListener != null)
//                            mOnNumberChangedListener.OnNumberChanged(num);
//                        ToastUtil.showShort("目前已选择最大数量");
//                        return;
//                    }
                } catch (Exception e) {

                }
            }
        });
    }

    public void setNumber(String number) {
        num = number;
        tvNum.setText(number);
    }

    public String getNumber() {
        return num;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add:
                if (new BigDecimal(Double.valueOf(num)).compareTo(new BigDecimal(maxNum)) == 0) {
                    ToastUtil.showShort("目前已选择最大数量");
                    return;
                }
                BigDecimal result = new BigDecimal(maxNum).subtract(new BigDecimal(Double.valueOf(num)));
                if (result.compareTo(new BigDecimal("1")) == -1) {
//                    num = String.valueOf(maxNum);
                    tvNum.setText(MathUtils.converData(maxNum, 3));
                    ToastUtil.showShort("目前已选择最大数量");
                    return;
                }
                String add = MathUtils.add(num, "1", 3);
                tvNum.setText(add);
                break;
            case R.id.btn_reduce:
                if (Double.valueOf(num) <= 1d) {
                    Toast.makeText(getContext(), "不能小于0", Toast.LENGTH_SHORT).show();
                } else {
                    num = MathUtils.subtract(num, "1", 3);
                    tvNum.setText(num);
                    if (mOnNumberChangedListener != null)
                        mOnNumberChangedListener.OnNumberChanged(String.valueOf(num));
                }
                break;
        }
    }

    public Double getMaxNum() {
        return maxNum;
    }

    public void setMaxNum(Double maxNum) {
        this.maxNum = maxNum;
    }

    public interface OnNumberChangedListener {
        void OnNumberChanged(String vs);
    }

    private OnNumberChangedListener mOnNumberChangedListener;

    public void setOnNumberChangedListener(OnNumberChangedListener onNumberChangedListener) {
        mOnNumberChangedListener = onNumberChangedListener;
    }
    public  void setNumFocusListener(OnFocusChangeListener listener){
        tvNum.setOnFocusChangeListener(listener);
    }

    public void  setNumFouce(){
        tvNum.setFocusable(true);
        tvNum.setSelection(tvNum.length());

    }
}
