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

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.res.ResGoodProperty;
import com.bonait.bnframework.common.db.res.ResMaterilas;
import com.bonait.bnframework.common.message.MessageManager;

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<>();

    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, int position) {
        try {
            if (holder instanceof MyLCViewHolder1) {

                ResGoodProperty goods = (ResGoodProperty) Data.get(position);
                MyLCViewHolder1 myViewHolder = (MyLCViewHolder1) holder;
                myViewHolder.button_select.setText(goods.name);
                if (goods.isSelect)
                {
                    myViewHolder.button_select.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.button3));
                }else
                {
                    myViewHolder.button_select.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.button2));
                }

                //按钮点击
                myViewHolder.button_select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        SetSelect(goods);
                        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=true;
            }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);
        }
    }
}