package com.app.input.personalised.board.listhelpers;

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

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

import com.app.input.personalised.board.R;
import com.app.input.personalised.board.databean.KeyboardTheme;
import com.app.input.personalised.board.helpers.AppUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

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

public class BannerListAdapter extends RecyclerView.Adapter<BannerListAdapter.BannerViewHolder> {

    private Context mContext;
    private List<KeyboardTheme> mList = new ArrayList<>();
    private OnBannerClickListener mListener;
    private boolean showTitle = false; // 是否显示标题

    public interface OnBannerClickListener {
        void onBannerClick(KeyboardTheme beanDetails);
    }

    public BannerListAdapter(Context context, List<KeyboardTheme> list) {
        mContext = context;
        this.mList = list != null ? list : new ArrayList<>();
    }

    public void setOnBannerClickListener(OnBannerClickListener listener) {
        mListener = listener;
    }

    public void setShowTitle(boolean show) {
        showTitle = show;
    }

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

    @Override
    public void onBindViewHolder(@NonNull BannerViewHolder holder, int position) {
        KeyboardTheme beanDetails = mList.get(position);
        String thumbGif = beanDetails.getThumbGif();
        String thumb = beanDetails.getThumbUrl();
        String title = beanDetails.getTitleName();
        
        // 设置标题
        if (showTitle && title != null && !title.isEmpty()) {
            holder.tvTitle.setText(title);
            holder.tvTitle.setVisibility(View.VISIBLE);
            holder.viewGradient.setVisibility(View.VISIBLE);
        } else {
            holder.tvTitle.setVisibility(View.GONE);
            holder.viewGradient.setVisibility(View.GONE);
        }
        
        // 加载图片，使用圆角
        int cornerRadius = (int) (16 * mContext.getResources().getDisplayMetrics().density);
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.placeholder)
                .transform(new RoundedCorners(cornerRadius));
        
        if (thumbGif != null && !thumbGif.isEmpty()) {
            // WebP GIF 使用Common加载，但需要应用圆角
            AppUtils.INSTANCE.loadWepJif(mContext, thumbGif, holder.imageView);
        } else if (thumb != null && !thumb.isEmpty()) {
            Glide.with(mContext)
                    .load(thumb)
                    .apply(options)
                    .into(holder.imageView);
        } else {
            holder.imageView.setImageResource(R.drawable.placeholder);
        }
        
        holder.itemView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onBannerClick(beanDetails);
            }
        });
    }

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

    public static class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tvTitle;
        View viewGradient;

        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_banner);
            tvTitle = itemView.findViewById(R.id.tv_banner_title);
            viewGradient = itemView.findViewById(R.id.view_gradient);
        }
    }
}
