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

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 android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
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.boartactivity.ThemeDetailActivity;
import com.app.input.personalised.board.helpers.AppUtils;
import com.bumptech.glide.Glide;

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

public class ThemeStaggeredAdapter extends RecyclerView.Adapter<ThemeStaggeredAdapter.StaggeredViewHolder> {

    private Context mContext;
    private List<KeyboardTheme> mList = new ArrayList<>();

    public ThemeStaggeredAdapter(Context context, List<KeyboardTheme> list) {
        mContext = context;
        this.mList = list;
    }

    public void setData(List<KeyboardTheme> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull StaggeredViewHolder holder, int position) {
        KeyboardTheme beanDetails = mList.get(position);
        String thumbGif = beanDetails.getThumbGif();
        String thumb = beanDetails.getThumbUrl();
        String title = beanDetails.getTitleName();

        holder.tvName.setText(title != null ? title : "");

        // 加载图片，参考HomeChildAdapter的实现
        if (thumbGif != null && !thumbGif.isEmpty()) {
            AppUtils.INSTANCE.loadWepJif(mContext, thumbGif, holder.imageView);
        } else if (thumb != null && !thumb.isEmpty()) {
            Glide.with(mContext)
                    .load(thumb)
                    .placeholder(R.drawable.placeholder)
                    .error(R.drawable.placeholder)
                    .centerCrop()
                    .into(holder.imageView);
        } else {
            // 如果没有图片URL，显示占位符
            holder.imageView.setImageResource(R.drawable.placeholder);
        }

        holder.cardView.setOnClickListener(v -> {
            Intent intentApply = new Intent(mContext, ThemeDetailActivity.class);
            intentApply.putExtra(ThemeDetailActivity.SOURCE_KEY, beanDetails);
            intentApply.putExtra(ThemeDetailActivity.DISPLAY_URL_KEY, beanDetails.getImgPath());
            intentApply.putExtra(ThemeDetailActivity.ZIP_URL_KEY, beanDetails.getZipPath());
            intentApply.putExtra(ThemeDetailActivity.NAME_KEY, beanDetails.getTitleName());
            intentApply.putExtra(ThemeDetailActivity.GIF_KEY, beanDetails.getImgGif());
            String intent_thumb;
            if (thumbGif != null && !thumbGif.isEmpty()) {
                intent_thumb = thumbGif;
            } else {
                intent_thumb = thumb;
            }
            intentApply.putExtra(ThemeDetailActivity.THUMB_KEY, intent_thumb);
            mContext.startActivity(intentApply);
        });
    }

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

    public static class StaggeredViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        ImageView imageView;
        TextView tvName;

        public StaggeredViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.card_view);
            imageView = itemView.findViewById(R.id.iv_image);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }
}

