package com.datarecovery.master.module.homepage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.datarecovery.master.databinding.ItemHomePageOtherFunctionBinding;
import com.datarecovery.master.utils.GridLayoutItemDecoration;

import java.util.List;

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


    @NonNull
    private final List<FunctionBean> beanList;

    private final LifecycleOwner lifecycleOwner;

    private onItemClick onItemClick;
    private GridLayoutItemDecoration itemDecoration;

    public OtherFunctionAdapter(LifecycleOwner lifecycleOwner, @NonNull List<FunctionBean> beanList) {
        this.lifecycleOwner = lifecycleOwner;
        this.beanList = beanList;
    }

    public void setOnItemClick(OtherFunctionAdapter.onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        if (itemDecoration == null) {
            itemDecoration = new GridLayoutItemDecoration(3, 0.0267857142857143F, 0.0267857142857143F);
        }
        recyclerView.addItemDecoration(itemDecoration);
    }

    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        if (itemDecoration != null) {
            recyclerView.removeItemDecoration(itemDecoration);
        }
    }

    @NonNull
    @Override
    public OtherFunctionAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        ItemHomePageOtherFunctionBinding binding = ItemHomePageOtherFunctionBinding.inflate(layoutInflater, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull OtherFunctionAdapter.ViewHolder holder, int position) {
        holder.bind(beanList.get(position));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        ItemHomePageOtherFunctionBinding binding;

        public ViewHolder(@NonNull ItemHomePageOtherFunctionBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.setLifecycleOwner(lifecycleOwner);
            binding.getRoot().setOnClickListener(v -> {
                if (onItemClick != null) onItemClick.onClick(binding.getBean());
            });
        }

        public void bind(FunctionBean bean) {
            binding.setBean(bean);
        }
    }

    public interface onItemClick {
        void onClick(FunctionBean bean);
    }
}
