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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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.view.MyLayoutManager;

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

public class sx_adapter extends ArrayAdapter<BPA_GOODPROPERTY> {
    /**
     * 内部点击事件
     */
    private MyClickListener mListener;
    private List<BPA_GOODPROPERTY> datas;

    private ArrayList<BPA_GOODSTYPE> goodstypes;
    int resource1;
    public Context contextA;
    public sx_adapter(@NonNull Context context, int resource, @NonNull List<BPA_GOODPROPERTY> objects, ArrayList<BPA_GOODSTYPE> bpa_goodstypes, MyClickListener listener) {
        super(context, resource, objects);
        mListener = listener;
        contextA=context;
        datas=objects;
        goodstypes=bpa_goodstypes;
        this.resource1=resource;
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        BPA_GOODPROPERTY bpa_goodproperty = (BPA_GOODPROPERTY) getItem(position);//得到当前项选中item实例
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(resource1, parent, false);
        //分别获取 image view 和 textview 的实例
        TextView name = view.findViewById(R.id.name);//属性名称
        TextView sort = view.findViewById(R.id.sort);//排序
        TextView typeS = view.findViewById(R.id.typeS);//排序

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

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

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



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

        typeS.setText(GetName(bpa_goodproperty.GoodsTypeId));
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mListener!=null) mListener.clickListener(view,bpa_goodproperty);
            }
        });

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

        typeS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mListener!=null) mListener.clickListener(view,bpa_goodproperty);
            }
        });

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

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

    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;
    }
}
