package com.kaixed.kchat.view.adapter;

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

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

import com.kaixed.kchat.databinding.SearchRecycleItemContactsBinding;
import com.kaixed.kchat.databinding.SearchRecycleItemDetailsBinding;
import com.kaixed.kchat.model.search.SearchItem;

import java.util.List;

/**
 * @Author: kaixed
 * @Date: 2024/8/12 20:25
 * @Description: 搜索结果的Adapter
 */
public class SearchResultAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<Object> displayedItems;

    public SearchResultAdapter(List<Object> objects) {
        this.displayedItems = objects;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            SearchRecycleItemContactsBinding binding = SearchRecycleItemContactsBinding.inflate(
                    LayoutInflater.from(parent.getContext()), parent, false);
            return new GroupViewHolder(binding);
        } else {
            SearchRecycleItemDetailsBinding binding = SearchRecycleItemDetailsBinding.inflate(
                    LayoutInflater.from(parent.getContext()), parent, false);

            return new DetailsViewHolder(binding);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder.getItemViewType() == 0) {
            GroupViewHolder groupViewHolder = (GroupViewHolder) holder;
            groupViewHolder.binding.tvName.setText(String.valueOf(displayedItems.get(position)));
        } else {
            DetailsViewHolder detailsViewHolder = (DetailsViewHolder) holder;
            if (displayedItems.get(position) instanceof SearchItem item) {
                detailsViewHolder.binding.tvName.setText((item.getName()));
                if ((item.getHasMore())) {
                    detailsViewHolder.binding.tvHasMore.setText("更多" + item.getType());
                    detailsViewHolder.binding.rlHasMore.setVisibility(View.VISIBLE);
                    detailsViewHolder.binding.view.setVisibility(View.INVISIBLE);

                } else {
                    detailsViewHolder.binding.rlHasMore.setVisibility(View.GONE);
                    detailsViewHolder.binding.view.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (displayedItems.get(position) instanceof String) {
            return 0;
        } else {
            return 1;
        }
    }

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

    // ViewHolder for Group Items
    static class GroupViewHolder extends RecyclerView.ViewHolder {
        private final SearchRecycleItemContactsBinding binding;

        public GroupViewHolder(SearchRecycleItemContactsBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

        }
    }

    // ViewHolder for Detail Items
    static class DetailsViewHolder extends RecyclerView.ViewHolder {
        private final SearchRecycleItemDetailsBinding binding;

        public DetailsViewHolder(SearchRecycleItemDetailsBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
