package com.example.bpa.view.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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

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

import com.example.bpa.R;
import com.example.bpa.db.mode.BPA_MATERIAL;
import com.example.bpa.helper.T;
import com.example.bpa.view.inteface.MyClickListener;
import com.example.bpa.view.mode.ResGoodsRecipe;

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

/**
 * 配料数据表格
 */
public class pf_adapter extends ArrayAdapter<ResGoodsRecipe> {
    /**
     * 内部点击事件
     */
    private MyClickListener mListener;
    private ArrayList<ResGoodsRecipe> datas= new ArrayList<>();

    ArrayList<String> wuliao=new ArrayList<>();
    ArrayList<BPA_MATERIAL> wllist=new ArrayList<>();
    public pf_adapter(@NonNull Context context, int resource, @NonNull ArrayList<ResGoodsRecipe> objects, ArrayList<BPA_MATERIAL> wl, MyClickListener listener) {
        super(context, resource, objects);
        mListener = listener;
        datas=objects;
        if(wuliao.size()<=0)
        {
            wuliao=new ArrayList<>();
            for (BPA_MATERIAL me:wl)
            {
                wuliao.add(me.name);
            }
            wllist=wl;
        }
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ResGoodsRecipe resGoodsRecipe = (ResGoodsRecipe) getItem(position);//得到当前项选中item实例
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(R.layout.pf_item, parent, false);
        //分别获取 image view 和 textview 的实例
        TextView name = view.findViewById(R.id.materialname);
        EditText sort = view.findViewById(R.id.sort);
        EditText value = view.findViewById(R.id.value);
        Button button = view.findViewById(R.id.button_item);
        // 设置要显示的图片和文字
//        ArrayAdapter mAdapter =  new ArrayAdapter<String>(view.getContext(),
//                R.layout.my_text_view,wuliao);
//        //mAdapter.addAll(wuliao);
//        name.setAdapter(mAdapter);

//        if(resGoodsRecipe.materialName!=null && !resGoodsRecipe.materialName.isEmpty())
//        {
//            SpinnerAdapter apsAdapter= name.getAdapter();
//            int k= apsAdapter.getCount();
//            for(int i=0;i<k;i++){
//                if(resGoodsRecipe.materialName.equals(apsAdapter.getItem(i).toString())){
//                    name.setSelection(i,true);
//                    break;
//                }
//            }
//        }
        name.setText(resGoodsRecipe.materialName);
        value.setText(resGoodsRecipe.value+"");
        sort.setText(resGoodsRecipe.sort+"");
        sort.setTag(resGoodsRecipe.id);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try
                {
                    mListener.clickListener(view,resGoodsRecipe);
                } catch (Exception e)
                {
                    T.show(view.getContext(),"删除配方出错:"+e.getMessage());
                }
            }
        });

        sort.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=sort.getText().toString().trim().toString();
                if(so.isEmpty())
                {
                    T.show(view.getContext(), "不能为空!");
                    return;
                }
                resGoodsRecipe.sort=Integer.parseInt(so);
                datas.set(position,resGoodsRecipe);
            }
        });

        value.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=value.getText().toString().trim().toString();
                if(so.isEmpty())
                {
                    T.show(view.getContext(), "不能为空!");
                    return;
                }
                resGoodsRecipe.value=Integer.parseInt(so);
                datas.set(position,resGoodsRecipe);
            }
        });

//        name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                int index=name.getSelectedItemPosition();
//                if(index>=0)
//                {
//                    BPA_MATERIAL wljh= wllist.get(name.getSelectedItemPosition());
//                    resGoodsRecipe.materialName=wljh.name;
//                    resGoodsRecipe.materialID=wljh.id;
//                    datas.set(position,resGoodsRecipe);
//                }else
//                {
//                    T.show(view.getContext(), "不能为空!");
//                    return;
//                }
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> adapterView) {
//                int index=name.getSelectedItemPosition();
//                if(index>=0)
//                {
//                    BPA_MATERIAL wljh= wllist.get(name.getSelectedItemPosition());
//                    resGoodsRecipe.materialName=wljh.name;
//                    resGoodsRecipe.materialID=wljh.id;
//                    datas.set(position,resGoodsRecipe);
//                }else
//                {
//                    resGoodsRecipe.materialName="";
//                    resGoodsRecipe.materialID="";
//                    datas.set(position,resGoodsRecipe);
//                    T.show(view.getContext(), "不能为空!");
//                    return;
//                }
//            }
//        });
        return view;
    }

    /**
     * 获取数据
     * @return
     */
    public ArrayList<ResGoodsRecipe> GetDatas()
    {
        return datas;
    }
}