package com.apps.chillscreen.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.apps.chillscreen.R;
import com.apps.chillscreen.bean.Picture;
import com.apps.chillscreen.databinding.ItemDiscoverBinding;
import com.apps.chillscreen.tool.PictureManager;
import com.apps.chillscreen.listener.CardClickListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;

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

public class DiscoverAdapter extends RecyclerView.Adapter<DiscoverAdapter.HomeParentHolder> {
    private Context context;
    private CardClickListener listener;
    private List<String> pictureNames = new ArrayList<>();

    @Override
    public void onBindViewHolder(@NonNull HomeParentHolder holder, int position) {
        String name = pictureNames.get(position);
        Log.d("YYYYYY", "pictureNames.size is" + getItemCount());
        holder.itemBing.tvClassName.setText(name);
        List<Picture> pictures = PictureManager.queryCover(name);
        String cover = pictures.get(0).getPreviewUrl1080();
        String thumb = pictures.get(0).getPreviewUrl200();
        Glide.with(context).load(cover).placeholder(R.mipmap.img_placeholder).error(R.mipmap.img_placeholder).thumbnail(
                Glide.with(context)
                        .asDrawable()
                        .load(thumb)
                        .apply(new RequestOptions()
                                .format(DecodeFormat.PREFER_RGB_565))
                        .placeholder(R.mipmap.img_placeholder)
                        .centerCrop()
        ).into(holder.itemBing.preview);
        if (position >= getItemCount() - 2) {
            holder.itemBing.bottomView.setVisibility(View.VISIBLE);
        } else if (position <= 1) {
            holder.itemBing.topView.setVisibility(View.VISIBLE);
        } else {
            holder.itemBing.bottomView.setVisibility(View.GONE);
            holder.itemBing.topView.setVisibility(View.GONE);
        }

        holder.itemBing.preview.setOnClickListener(v -> listener.onHomeClick(name));
    }

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

    @NonNull
    @Override
    public HomeParentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        ItemDiscoverBinding inflate = ItemDiscoverBinding.inflate(LayoutInflater.from(context), parent, false);
        return new HomeParentHolder(inflate);
    }

    static class HomeParentHolder extends RecyclerView.ViewHolder {

        private ItemDiscoverBinding itemBing;

        public HomeParentHolder(@NonNull ItemDiscoverBinding itemView) {
            super(itemView.getRoot());
            itemBing = itemView;
        }
    }

    public void setPictures(List<String> pictureList) {
        this.pictureNames = pictureList;
        notifyDataSetChanged();
    }

    public void setClickListener(CardClickListener listener) {
        this.listener = listener;
    }
}
