package com.key.vibekeyboard.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.key.vibekeyboard.Activity.DownloadActivity;
import com.key.vibekeyboard.R;
import com.key.vibekeyboard.Room.WallpaperInfo;

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

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


    private Context context;
    private List<WallpaperInfo> list;

    public FavoriteRecyclerViewAdapter(Context context, List<WallpaperInfo> list) {

        this.context = context;
        this.list = new ArrayList<>(list);

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_recycler_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        WallpaperInfo wallpaperInfo = list.get(position);

        List<WallpaperInfo> limitedList = list.subList(0, Math.min(20, list.size()));

        if (wallpaperInfo != null) {
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, DownloadActivity.class);
                    intent.putExtra("wallpaperInfo", wallpaperInfo); // wallpaperInfo 已经实现了 Parcelable
                    intent.putParcelableArrayListExtra("list", new ArrayList<>(limitedList)); // list 中的 WallpaperInfo 对象实现了 Parcelable
                    context.startActivity(intent);
                }
            });
        }

        // 应用圆角变换
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.splash)
                .error(R.mipmap.splash)
                .transform(new CenterCrop())
                .transform(new RoundedCorners(16)); // 设置圆角度数
        if (wallpaperInfo != null) {
            Glide.with(context)
                    .load(wallpaperInfo.getPreview())
                    .apply(options)
                    .into(holder.imageView);

        }
    }

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


    public static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.home_recycler_image);
        }
    }

    // 使用 DiffUtil 更新列表
    public void updateList(List<WallpaperInfo> newList) {
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffUtil.Callback() {
            @Override
            public int getOldListSize() {
                return list.size();
            }

            @Override
            public int getNewListSize() {
                return newList.size();
            }

            @Override
            public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
                // 通过唯一标识符比较两个项目是否相同
                return list.get(oldItemPosition).getId() == newList.get(newItemPosition).getId();
            }

            @Override
            public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
                // 比较内容是否相同
                return list.get(oldItemPosition).equals(newList.get(newItemPosition));
            }
        });

        list.clear();
        list.addAll(newList);
        diffResult.dispatchUpdatesTo(this); // 通知 RecyclerView 进行局部更新
    }

}
