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.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.Category;
import com.key.vibekeyboard.Room.WallpaperInfo;

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

public class HomeViewPager2Adapter extends RecyclerView.Adapter<HomeViewPager2Adapter.HomeViewPager2ViewHolder> {

    private List<Category> categories;
    private Context context;

    public HomeViewPager2Adapter(Context context, List<Category> categories) {
        this.context = context;
        this.categories = categories;
    }

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

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

        int actualPosition = position % categories.size();  // 确保最后一张的视图是第一张
        Category category = categories.get(actualPosition);
        List<WallpaperInfo> list = category.getList();
        WallpaperInfo categoryWallpaperInfo = category.getList().get(position);
        Random random = new Random();
        int randomIndex = random.nextInt(20);
        WallpaperInfo wallpaperInfo = category.getList().get(randomIndex);

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

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

        // 应用圆角变换
        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 categories.size()+1;
    }

    public static class HomeViewPager2ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView imageView;

        public HomeViewPager2ViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.home_viewpager_image);
        }
    }

}
