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

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODPROPERTY;
import com.bonait.bnframework.common.db.mode.BPA_GOODSTYPE;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.utils.KeyboardUtil;
import com.bonait.bnframework.common.view.MyLayoutManager;

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

/**
 * @author: liup
 * @description: 基础数据管理 - 属性管理
 * @date: 2024/5/21 16:28.
 */
public class AttributeSetAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    /**
     * 内部点击事件
     */
    private MyClickListener mListener;
    private List<BPA_GOODPROPERTY> datas;

    private ArrayList<BPA_GOODSTYPE> goodstypes;
    private final LayoutInflater mLayoutInflater;
    private Context context;
    public AttributeSetAdapter(@NonNull Context context, @NonNull List<BPA_GOODPROPERTY> objects, ArrayList<BPA_GOODSTYPE> bpa_goodstypes, MyClickListener listener) {
        mListener = listener;
        datas=objects;
        this.context=context;
        goodstypes=bpa_goodstypes;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.sx_item, parent, false);
        return new MyLCViewHolder1(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        MyLCViewHolder1 myLCViewHolder1 = (MyLCViewHolder1) holder;
        BPA_GOODPROPERTY bpa_goodproperty = (BPA_GOODPROPERTY) datas.get(position);//得到当前项选中item实例
        myLCViewHolder1.root.setOnClickListener(KeyboardUtil::hideKeyboard);

        ArrayList<BPA_GOODPROPERTY> zsx= QueryDB.GetGoodsPropertyALL(bpa_goodproperty.id);
        MyLayoutManager layout = new MyLayoutManager();
        layout.setAutoMeasureEnabled(true);
        myLCViewHolder1.recyclerView.setLayoutManager(layout);
        zsx_adapter zsxadapter = new zsx_adapter(position,context,zsx,mListener);
        myLCViewHolder1.recyclerView.setAdapter(zsxadapter);



        // 设置要显示的图片和文字
        myLCViewHolder1.name.setText(bpa_goodproperty.name);
        myLCViewHolder1.sort.setText(bpa_goodproperty.sort+"");

        myLCViewHolder1.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mListener!=null) mListener.clickListenerNew(view,position,bpa_goodproperty);
            }
        });

        myLCViewHolder1.sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mListener!=null) mListener.clickListenerNew(view,position,bpa_goodproperty);
            }
        });

        myLCViewHolder1.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mListener!=null) mListener.clickListenerNew(view,position,bpa_goodproperty);
            }
        });

        myLCViewHolder1.button_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mListener!=null) mListener.clickListenerNew(view,position,bpa_goodproperty);
            }
        });
    }

    public String GetName(String ids)
    {
        String name="";
        if(ids==null || ids.isEmpty())
        {
            return "";
        }
        for (BPA_GOODSTYPE item:goodstypes)
        {
            if(ids.contains(item.id))
            {
                name+=item.name+",";
            }
        }

        if(!name.isEmpty())
        {
            name=name.substring(0, name.length() - 1);
        }

        return name;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class MyLCViewHolder1 extends RecyclerView.ViewHolder{
        LinearLayout root ;
        TextView name ;//属性名称
        TextView sort ;//排序

        ImageView button ;//删除
        ImageView button_add ;//增加 子属性

        RecyclerView recyclerView;
        public MyLCViewHolder1(@NonNull View view) {
            super(view);
            root = view.findViewById(R.id.root);
            name = view.findViewById(R.id.name);//属性名称
            sort = view.findViewById(R.id.sort);//排序

            button = view.findViewById(R.id.button_item);//删除
            button_add = view.findViewById(R.id.button_add);//增加 子属性

            recyclerView=view.findViewById(R.id.recycler_view);
        }
    }
}