package com.key.vibekeyboard.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
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.ui.activity.DownloadActivity;
import com.key.vibekeyboard.R;
import com.key.vibekeyboard.data.database.entity.WallpaperInfo;

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

public class WallpaperAdapter extends RecyclerView.Adapter<WallpaperAdapter.CategoryRecyclerViewHolder> {

    private final Context context;
    private final List<WallpaperInfo> list;

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

        this.context = context;
        this.list = list;
    }

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

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

        WallpaperInfo wallpaperInfo = list.get(position);
        Log.d("WallpaperInfo", "WallpaperInfo: " + wallpaperInfo);

        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);
                    intent.putParcelableArrayListExtra("list", new ArrayList<>(limitedList));
                    context.startActivity(intent);
                }
            });

        } else {
            Log.e("WallpaperInfo", "WallpaperInfo is null at position: " + position);
        }

        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.splash)
                .error(R.mipmap.splash)
                .transform(new CenterCrop())
                .transform(new RoundedCorners(16));

        Glide.with(context)
                .load(wallpaperInfo.getPreview())
                .apply(options)
                .into(holder.imageView);
    }

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

    public static class CategoryRecyclerViewHolder extends RecyclerView.ViewHolder {

        private final ImageView imageView;
        public CategoryRecyclerViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.home_recycler_image);
        }
    }

    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);
    }

}
