package com.snap.wall;

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

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.List;

public class RecentWallpaperAdapter extends BaseAdapter {
    private Context context;
    private List<Wallpaper> wallpaperList;
    private final int cornerRadius = 12; // 圆角大小（dp）
    private OnItemClickListener onItemClickListener;
    private RequestManager glideRequestManager;

    // 点击回调接口
    public interface OnItemClickListener {
        void onItemClick(Wallpaper wallpaper);
    }

    // 构造方法
    public RecentWallpaperAdapter(Context context, List<Wallpaper> wallpaperList, RequestManager glideManager, OnItemClickListener listener) {
        this.context = context;
        this.wallpaperList = wallpaperList;
        this.glideRequestManager = glideManager;
        this.onItemClickListener = listener;
    }

    @Override
    public int getCount() {
        return wallpaperList.size();
    }

    @Override
    public Object getItem(int position) {
        return wallpaperList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_wallpaper, parent, false);
            holder = new ViewHolder();
            holder.ivWallpaper = convertView.findViewById(R.id.iv_wallpaper);
            // 强制设置ScaleType，确保圆角生效
            holder.ivWallpaper.setScaleType(ImageView.ScaleType.CENTER_CROP);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Wallpaper wallpaper = wallpaperList.get(position);

        // 核心修复：Glide组合变换（CenterCrop + 圆角），强制生效
        if (glideRequestManager != null) {
            RequestOptions options = new RequestOptions()
                    .transform(new CenterCrop(), new RoundedCorners(dp2px(cornerRadius)))
                    .dontAnimate();

            glideRequestManager
                    .load(wallpaper.getImageUrl())
                    .apply(options)
                    .into(holder.ivWallpaper);
        }

        // Item点击事件
        convertView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(wallpaper);
            }
        });

        return convertView;
    }

    // dp转px（精确计算，避免圆角大小偏差）
    private int dp2px(int dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return Math.round(dp * density);
    }

    static class ViewHolder {
        ImageView ivWallpaper;
    }
}