package com.sjkj.appthreefloor_tsgz.adapter;

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

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.bean.RightModelBean;

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

/**
 * CREATE BY LiYang
 * ON 2021-03-25
 * SUPPLY : Thanks for watching
 */
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.ViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<RightModelBean> list = new ArrayList<>();
    private ChangeStateListener listener;

    public RightAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<RightModelBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void clear() {
        this.list.clear();
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_right_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.checkBox.setText(list.get(position).getName());
        if (list.get(position).isState()) {
            holder.checkBox.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_screen_blue_solid_5));
            holder.checkBox.setTextColor(ContextCompat.getColor(context, R.color.color_white));
        } else {
            holder.checkBox.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_details_gray_solid_5));
            holder.checkBox.setTextColor(ContextCompat.getColor(context, R.color.color_black));
        }
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView checkBox;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.checkBox);
            checkBox.setOnClickListener(v -> {
                if (list.get(getAdapterPosition()).isState()) {
                    list.get(getAdapterPosition()).setState(false);
                    listener.change("");
                    notifyItemChanged(getAdapterPosition());

                } else {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setState(false);
                    }
                    list.get(getAdapterPosition()).setState(true);
                    listener.change(list.get(getAdapterPosition()).getId());
                    notifyDataSetChanged();
                }
            });
        }
    }

    public interface ChangeStateListener {
        void change(String item);
    }

    public void setListener(ChangeStateListener listener) {
        this.listener = listener;
    }
}
