package com.lock.arcanvasart.adapter;

import android.app.Activity;
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.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.lock.arcanvasart.R;
import com.lock.arcanvasart.activity.CameraActivity;
import com.lock.arcanvasart.room.ImageEntry;

import java.util.List;

public class ProfileAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_HEADER = 0;
    private static final int VIEW_TYPE_ITEM = 1;

    private List<ImageEntry> imageEntries;
    private final Activity context;
    private final OnHeaderClickListener onHeaderClickListener;

    public ProfileAdapter(List<ImageEntry> imageEntries, Activity context, OnHeaderClickListener onHeaderClickListener) {
        this.imageEntries = imageEntries;
        this.context = context;
        this.onHeaderClickListener = onHeaderClickListener;
    }

    public interface OnHeaderClickListener {
        void onHeaderClick();
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? VIEW_TYPE_HEADER : VIEW_TYPE_ITEM;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_HEADER) {
            // 加载特殊控件的布局
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_special_view, parent, false);
            return new HeaderViewHolder(view);
        } else {
            // 加载普通控件的布局
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_image_work, parent, false);
            return new ItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_HEADER) {
            HeaderViewHolder headerHolder = (HeaderViewHolder) holder;

            headerHolder.itemView.setOnClickListener(v -> {
                if (onHeaderClickListener != null) {
                    onHeaderClickListener.onHeaderClick();
                }
            });
        } else {
            ItemViewHolder itemHolder = (ItemViewHolder) holder;
            ImageEntry imageEntry = imageEntries.get(position - 1);

            RequestOptions requestOptions = new RequestOptions()
                    .transform(new CenterCrop(), new RoundedCorners(20));

            Glide.with(context)
                    .load(imageEntry.getImagePath())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .apply(requestOptions)
                    .into(itemHolder.imageView);

            itemHolder.imageView.setOnClickListener(v -> {
                Intent intent = new Intent(context, CameraActivity.class);
                intent.putExtra("imagePath", imageEntry.getImagePath()); // 传递图片路径
                context.startActivity(intent);
            });

        }
    }

    @Override
    public int getItemCount() {
        return imageEntries.size() + 1; // 加上 header 的项数
    }

    public void updateData(List<ImageEntry> newImageEntries) {
        this.imageEntries = newImageEntries;
        notifyDataSetChanged();
    }

    // Header ViewHolder 类（特殊控件）
    static class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(View view) {
            super(view);
            // 在这里可以初始化 Header 控件
        }
    }

    // Item ViewHolder 类（普通控件）
    static class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public ItemViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.image_view);
        }
    }
}
