package com.zztrapp.zztrapp.ui.currency.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.bean.GetTreeListBean;
import com.zztrapp.zztrapp.databinding.ItemRvFaLvFaGuiBinding;

import java.util.List;

/**
 * Describe 法律法规Adapter
 * Created by Bby on 2022/6/23
 * Email bby0856@163.com
 */

public class FaLvFaGuiAdapter extends RecyclerView.Adapter<FaLvFaGuiAdapter.ItemHolder> {

    private Context mContext;
    private OnItemClickListener onItemClickListener;
    private List<GetTreeListBean> list;

    public interface OnItemClickListener {
        void onOpenClick(int position, boolean isOpen);

        void onItemChildClick(String id);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public FaLvFaGuiAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<GetTreeListBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemRvFaLvFaGuiBinding binding = ItemRvFaLvFaGuiBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new ItemHolder(binding);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.bindings.name.setText(list.get(position).getName());
        if (list.get(position).isOpen()) {
            holder.bindings.hint.setText("收起");
            holder.bindings.icon.setImageResource(R.mipmap.jiantou_ic2_select);
            holder.bindings.rv.setVisibility(View.VISIBLE);
        } else {
            holder.bindings.hint.setText("展开");
            holder.bindings.icon.setImageResource(R.mipmap.jiantou_ic2);
            holder.bindings.rv.setVisibility(View.GONE);
        }
        holder.bindings.open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onOpenClick(position, list.get(position).isOpen());
                }
            }
        });
        holder.bindings.rv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        FaLvFaGuiChildAdapter adapter = new FaLvFaGuiChildAdapter(mContext);
        holder.bindings.rv.setAdapter(adapter);
        adapter.setData(list.get(position).getChildren());
        adapter.setOnItemClickListener(new FaLvFaGuiChildAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String id) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemChildClick(id);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        private ItemRvFaLvFaGuiBinding bindings;

        public ItemHolder(ItemRvFaLvFaGuiBinding binding) {
            super(binding.getRoot());
            bindings = binding;
        }
    }
}