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


import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPENAME;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.res.ResGoodsRecipe;
import com.bonait.bnframework.common.db.res.ResMaterilas;
import com.bonait.bnframework.common.utils.DisplayManager;
import com.bonait.bnframework.databinding.ItemGroupDetailBinding;

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

/**
 * @author: liup
 * @description:  配方详情
 * @date: 2024/6/6 17:30.
 */
public class GoodGroupDetailAdapter extends BaseAdapter<ResGoodsRecipe, GoodGroupDetailAdapter.ViewHolder>  {
    /**
     * 当前物料数据
     */
    public List<BPA_MATERIAL> materilasList=new ArrayList<>();

    public void setMaterilasList(List<BPA_MATERIAL> materilasList) {
        this.materilasList = materilasList;
    }

    @Override
    protected void onItemClick(View v, int position) {

    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(ItemGroupDetailBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
//        if(holder.binding.btnMaterialDelete.getTextSize()>28||holder.binding.btnMaterialDelete.getTextSize()<24){
//            DisplayManager.scaleViewGroup(holder.binding.root);
//        }
        if(mData.get(position).value!=null && !mData.get(position).value.isEmpty()){
            holder.binding.edittextWeight.setText(mData.get(position).value);
        }
        List<String> names = new ArrayList<>();
        for(BPA_MATERIAL material : materilasList){
            names.add(material.name);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<>(holder.binding.spinnerName.getContext(), R.layout.spinner_text24_item, names);
        adapter.setDropDownViewResource(R.layout.spinner_dropdown24_item);
        holder.binding.spinnerName.setAdapter(adapter);

        if(mData.get(position).materialID!=null && !mData.get(position).materialID.isEmpty()){
            for(int i=0;i<materilasList.size();i++){
                if(materilasList.get(i).id.equals(mData.get(position).materialID)){
                    holder.binding.spinnerName.setSelection(i);
                    break;
                }
            }
        }
        holder.binding.spinnerName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int p, long l) {
                for(int i=0;i<materilasList.size();i++){
                    if(materilasList.get(i).name.equals(holder.binding.spinnerName.getSelectedItem().toString())){
                        mData.get(position).materialID = materilasList.get(i).id;
                        mData.get(position).materialName = materilasList.get(i).name;
                        break;
                    }
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        holder.binding.edittextWeight.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) {
                mData.get(position).value = holder.binding.edittextWeight.getText().toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        holder.binding.btnMaterialDelete.setOnClickListener(v->{
            mData.remove(position);
            notifyDataSetChanged();
        });

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ItemGroupDetailBinding binding;

        public ViewHolder(ItemGroupDetailBinding view) {
            super(view.getRoot());
            binding = view;
        }
    }
}
