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


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
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.utils.DisplayManager;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.databinding.ItemGroupNameBinding;

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

/**
 * @author: liup
 * @description: 配方名称+配方详情
 * @date: 2024/6/6 17:30.
 */
public abstract class GoodGroupNameAdapter extends BaseAdapter<BPA_GOODSRECIPENAME,GoodGroupNameAdapter.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) {

    }

    protected abstract void onDelete(int position,BPA_GOODSRECIPENAME bean);
    protected abstract void onCopy(int position,BPA_GOODSRECIPENAME bean);

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//        if(holder.binding.groupName.getTextSize()>28||holder.binding.groupName.getTextSize()<24){
//            DisplayManager.scaleViewGroup(holder.binding.root);
//            DisplayManager.scaleViewGroup(holder.binding.llControl);
//        }
        holder.binding.groupName.setText(mData.get(position).name+"");
        holder.binding.btnCopy.setOnClickListener(v->{
            onCopy(position,mData.get(position));
        });
        holder.binding.btnDelete.setOnClickListener(v->{
            onDelete(position,mData.get(position));
        });
        if(holder.binding.tvExpand.getText().toString().equals("收起")){
            holder.binding.imgArrow.setBackgroundResource(R.mipmap.ic_arrow_down2);
            holder.binding.llControl.setVisibility(View.VISIBLE);
            holder.binding.recycleDetail.setVisibility(View.VISIBLE);
        }else {
            holder.binding.llControl.setVisibility(View.GONE);
            holder.binding.recycleDetail.setVisibility(View.GONE);
            holder.binding.imgArrow.setBackgroundResource(R.mipmap.ic_arrow_left2);
        }
        GoodGroupDetailAdapter detailAdapter = new GoodGroupDetailAdapter();
        detailAdapter.setMaterilasList(materilasList);
        detailAdapter.setNewData(QueryDB.GetGoodsSreciperecipeList(mData.get(position).id));
        holder.binding.recycleDetail.setAdapter(detailAdapter);
        holder.binding.btnSave.setOnClickListener(v->{
            ArrayList<ResGoodsRecipe> re= QueryDB.GetGoodsSreciperecipeList(mData.get(position).id);
            for(ResGoodsRecipe item:re)
            {
                QueryDB.DeleteGoodsSrecipe(item.id);
            }

            for (ResGoodsRecipe i: detailAdapter.getData())
            {
                BPA_GOODSRECIPE good=new BPA_GOODSRECIPE();
                good.materialID=i.materialID;
                good.recipeID=mData.get(position).id;
                good.goodsID=mData.get(position).goodsID;
                good.value=i.value;
                good.sort=0;
                QueryDB.AddGoodsSrecipe(good);
            }
            ToastUtils.info("保存成功");
        });
        holder.binding.btnAdd.setOnClickListener(v->{
            ResGoodsRecipe bean = new ResGoodsRecipe();
            bean.sort = 0;
            bean.value = "0";
            bean.materialName = "未设置";
            detailAdapter.addData(bean);
            detailAdapter.notifyDataSetChanged();
        });

        holder.binding.llArrow.setOnClickListener(v->{
            if(holder.binding.tvExpand.getText().toString().equals("展开")){
                holder.binding.imgArrow.setBackgroundResource(R.mipmap.ic_arrow_down2);
                holder.binding.tvExpand.setText("收起");
                holder.binding.recycleDetail.setVisibility(View.VISIBLE);
                holder.binding.llControl.setVisibility(View.VISIBLE);

            }else {
                holder.binding.imgArrow.setBackgroundResource(R.mipmap.ic_arrow_left2);
                holder.binding.tvExpand.setText("展开");
                holder.binding.recycleDetail.setVisibility(View.GONE);
                holder.binding.llControl.setVisibility(View.GONE);
            }
        });
        holder.binding.root.setOnClickListener(v->{
            if(holder.binding.tvExpand.getText().toString().equals("展开")){
                holder.binding.tvExpand.setText("收起");
                holder.binding.recycleDetail.setVisibility(View.VISIBLE);
                holder.binding.llControl.setVisibility(View.VISIBLE);
            }else {
                holder.binding.tvExpand.setText("展开");
                holder.binding.recycleDetail.setVisibility(View.GONE);
                holder.binding.llControl.setVisibility(View.GONE);
            }
        });
    }

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

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