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

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

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

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPENAME;
import com.bonait.bnframework.common.db.res.ResGoodProperty;
import com.bonait.bnframework.common.db.res.ResMaterilas;
import com.bonait.bnframework.common.db.util.GoodsRecipeUtil;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.utils.ToastUtils;

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

public class newpropertyselect_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;

    private Context context;

    ArrayList<ResGoodProperty> Data = new ArrayList<>();
//    private List<String> idList = new ArrayList<>();
//    private String goodId="";
//    private int parentPosition = 0;
//
//    public void setParentPosition(int parentPosition) {
//        this.parentPosition = parentPosition;
//    }
//
//    public void setGoodId(String goodId) {
//        this.goodId = goodId;
//    }
//
//    public void setIdList(List<String> idList) {
//        this.idList = idList;
//    }

    public newpropertyselect_adapter(Context context, ArrayList<ResGoodProperty> data) {
        this.context = context;

        mLayoutInflater = LayoutInflater.from(context);
        Data = data;
    }

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

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        try {
//            List<BPA_GOODSRECIPENAME> goodsrecipenames = new ArrayList<>();
//            if(goodId!=null && !goodId.isEmpty()&&idList!=null && !idList.isEmpty()){
//                List<String> list = new ArrayList<>();
//                for (int i=0;i<idList.size();i++){
//                    if(i!=parentPosition){
//                        list.add(idList.get(i));
//                    }
//                }
//                goodsrecipenames = GoodsRecipeUtil.GetGoodsRecipeNameLikes(goodId,list);
//            }
            if (holder instanceof MyLCViewHolder1) {
                ResGoodProperty goods = (ResGoodProperty) Data.get(position);
                MyLCViewHolder1 myViewHolder = (MyLCViewHolder1) holder;
                myViewHolder.button_select.setText(goods.name);
                if (goods.isSelect)
                {
//                    boolean isExit = false;
//                    for(BPA_GOODSRECIPENAME goodsrecipename :goodsrecipenames){
//                        if(goodsrecipename.design.contains(goods.id)){
//                            isExit = true;
//                        }
//                    }
//                    if(isExit){
                        myViewHolder.button_select.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.button3));
//                    }else {
//                        goods.isSelect = false;
//                        myViewHolder.button_select.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.button4));
//                    }
                }else
                {
//                    boolean isExit = false;
//                    for(BPA_GOODSRECIPENAME goodsrecipename :goodsrecipenames){
//                        if(goodsrecipename.design.contains(goods.id)){
//                            isExit = true;
//                        }
//                    }
//                    if(isExit){
                        myViewHolder.button_select.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.button2));
//                    }else {
//                        myViewHolder.button_select.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.button4));
//                    }
                }

                //按钮点击
                myViewHolder.button_select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        SetSelect(goods);
                        clickCallBack.onClickItem(view,position,goods.id);
                        MessageManager.getInstance().sendMessage(MessageName.ClickProperty,"ClickProperty");
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void SetSelect(ResGoodProperty pro)
    {
        for (ResGoodProperty item:Data)
        {
            if(item.id.equals(pro.id))
            {
                item.isSelect = !item.isSelect;
            }else
            {
                item.isSelect=false;
            }
        }
        notifyDataSetChanged();
    }

    private Activity findActivity(@NonNull Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return findActivity(((ContextWrapper) context).getBaseContext());
        } else {
            return null;
        }
    }

    /**
     * 刷新
     *
     * @param
     */
    public void refresh() {
        Activity activity = findActivity(context);
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        notifyDataSetChanged();
                    } catch (Exception e) {

                    }
                }
            });
        }
    }

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

    public static class MyLCViewHolder1 extends RecyclerView.ViewHolder {
        Button button_select;

        public MyLCViewHolder1(View view) {
            super(view);
            button_select = view.findViewById(R.id.button_select);
        }
    }

    private ItemClickCallBack clickCallBack;

    public void setClickCallBack(ItemClickCallBack clickCallBack){
        this.clickCallBack=clickCallBack;
    }

    public interface ItemClickCallBack{
        /**
         *
         * @param view
         * @param position 子属性位置
         * @param attributeId 子属性id
         */
        void onClickItem(View view,int position,String attributeId);
    }


}