package com.bonait.bnframework.modules.home.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.res.ResGoodsRecipe;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;


public class xxpf_adapter extends ArrayAdapter<ResGoodsRecipe> {
    /**
     * 内部点击事件
     */
    private MyClickListener mListener;
    int resource1;
    public xxpf_adapter(@NonNull Context context, int resource, @NonNull ArrayList<ResGoodsRecipe> objects, MyClickListener listener) {
        super(context, resource, objects);
        mListener = listener;
        this.resource1=resource;
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ResGoodsRecipe goodsrecipe = (ResGoodsRecipe) getItem(position);//得到当前项选中item实例
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(resource1, parent, false);
        //分别获取 image view 和 textview 的实例
        TextView name = view.findViewById(R.id.name);
        Button button = view.findViewById(R.id.button_delete);

        //序号 重量
        EditText edittext_bz = view.findViewById(R.id.edittext_bz);
        EditText edittext_zl = view.findViewById(R.id.edittext_zl);


        //设置要显示的图片和文字
        name.setText(goodsrecipe.materialName);
        edittext_zl.setText(goodsrecipe.value+"");
        edittext_bz.setText(goodsrecipe.sort+"");

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DataBus.getInstance().Recipe.remove(goodsrecipe);
                notifyDataSetChanged();
            }
        });

        edittext_bz.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void afterTextChanged(Editable editable) {
                String so=edittext_bz.getText().toString().trim().toString();
                if(so.isEmpty())
                {
                    goodsrecipe.sort=0;
                    DataBus.getInstance().Recipe.set(position,goodsrecipe);
                    //notifyDataSetChanged();
                }else
                {
                    try
                    {
                        goodsrecipe.sort=Integer.parseInt(so);
                    }catch (Exception ex)
                    {
                        goodsrecipe.sort=0;
                    }
                    DataBus.getInstance().Recipe.set(position,goodsrecipe);
                    //notifyDataSetChanged();
                }

            }
        });

        edittext_zl.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void afterTextChanged(Editable editable) {
                String so=edittext_zl.getText().toString().trim().toString();
                double K=0;
                if(so.isEmpty())
                {
                    K=0;
                }else
                {
                    try
                    {
                        K=Double.parseDouble(so);
                    }catch (Exception ex)
                    {
                        ToastUtils.error("重量数据不合法！");
                        K=0;
                        edittext_zl.setText("0.0");
                    }
                }

                if (K==0)
                {
                    goodsrecipe.value="0.0";
                    DataBus.getInstance().Recipe.set(position,goodsrecipe);
                    //notifyDataSetChanged();
                }else
                {
                    goodsrecipe.value=String.format ("%.1f", K);
                    DataBus.getInstance().Recipe.set(position,goodsrecipe);
                   // notifyDataSetChanged();
                }
            }
        });
        return view;
    }
}