package com.zztrapp.zztrapp.ui.index.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.RecyclerView;

import com.google.android.flexbox.FlexboxLayoutManager;
import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.bean.IndexBean;
import com.zztrapp.zztrapp.databinding.ItemRvIndexChildBinding;

import java.util.List;

/**
 * Describe 首页列表
 * Created by Bby on 2022/6/22
 * Email bby0856@163.com
 */

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

    private Context mContext;
    private OnItemClickListener onItemClickListener;
    private List<IndexBean.ValueListBean> list;
    private int type;

    public interface OnItemClickListener {
        void onLookDetailClick(int type, String name);
    }

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


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

    public void setData(List<IndexBean.ValueListBean> list, int type) {
        this.list = list;
        this.type = type;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemRvIndexChildBinding binding = ItemRvIndexChildBinding.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 (position == list.size() - 1) {
            holder.bindings.line.setVisibility(View.GONE);
        } else {
            holder.bindings.line.setVisibility(View.VISIBLE);
        }
        holder.bindings.lookDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onLookDetailClick(type, list.get(position).getName());
                }
            }
        });
        holder.bindings.detailHint.setVisibility(View.VISIBLE);
        holder.bindings.detailIcon.setVisibility(View.VISIBLE);
        if (list.get(position).getName().equals("重点监管单位")) {
            holder.bindings.icon.setBackgroundResource(R.mipmap.zdjg_ic);
        } else if (list.get(position).getName().equals("土壤污染地块")) {
            holder.bindings.icon.setBackgroundResource(R.mipmap.trwr_ic);
        } else if (list.get(position).getName().equals("监测井")) {
            holder.bindings.icon.setBackgroundResource(R.mipmap.jcj_ic);
        } else if (list.get(position).getName().equals("报废井")) {
            holder.bindings.icon.setBackgroundResource(R.mipmap.bfj_ic);
        } else if (list.get(position).getName().equals("双源调查")) {
            holder.bindings.detailHint.setVisibility(View.GONE);
            holder.bindings.detailIcon.setVisibility(View.GONE);
            holder.bindings.icon.setBackgroundResource(R.mipmap.sydc_ic);
        } else if (list.get(position).getName().equals("黑臭水体")) {
            holder.bindings.icon.setBackgroundResource(R.mipmap.hcst_ic);
        } else if (list.get(position).getName().equals("污水处理设施")) {
            holder.bindings.icon.setBackgroundResource(R.mipmap.wsclss_ic);
        } else {
            holder.bindings.icon.setBackgroundResource(R.mipmap.zdjg_ic);
        }

        holder.bindings.rv.setLayoutManager(new FlexboxLayoutManager(mContext));
        IndexChildChildAdapter adapter = new IndexChildChildAdapter(mContext);
        holder.bindings.rv.setAdapter(adapter);
        adapter.setData(list.get(position).getValue());
    }

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

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

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