package com.keyborad.theme.trendyborad.trendylistutils;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.keyborad.theme.trendyborad.R;
import com.keyborad.theme.trendyborad.bean.BeanDetails;
import com.keyborad.theme.trendyborad.databinding.AdapterHeCdTrendyBinding;
import com.keyborad.theme.trendyborad.trendyuiactivity.TrendySetKeyboardActivity;
import com.keyborad.theme.trendyborad.trendyutils.TrendyCommon;
import com.bumptech.glide.Glide;

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

public class TrendyHomeChildAdapter extends RecyclerView.Adapter<TrendyHomeChildAdapter.ChildViewHolder> {

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

    public TrendyHomeChildAdapter(Context context, List<BeanDetails> list) {
        mContext = context;
        updateData(list);
    }

    public void updateData(List<BeanDetails> newList) {
        mList.clear();
        if (newList != null && !newList.isEmpty()) {
            mList.addAll(newList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mList.size();  // 直接返回数据的长度
    }

    // 获取实际位置
    private int getActualPosition(int position) {
        return position;  // 不再需要取余处理
    }

    @NonNull
    @Override
    public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        AdapterHeCdTrendyBinding binding = AdapterHeCdTrendyBinding.inflate(
                LayoutInflater.from(parent.getContext()),
                parent,
                false
        );
        return new ChildViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ChildViewHolder holder, int position) {
        if (mList.isEmpty()) {
            holder.binding.imageView.setImageResource(R.drawable.err_load);
            return;
        }

        int actualPosition = getActualPosition(position);
        BeanDetails beanDetails = mList.get(actualPosition);

        String thumbGif = beanDetails.getThumbGif();
        String thumb = beanDetails.getThumbUrl();

        if (thumbGif != null && !thumbGif.isEmpty()) {
            TrendyCommon.INSTANCE.loadWepJif(mContext, thumbGif, holder.binding.imageView);
        } else if (thumb != null && !thumb.isEmpty()) {
            Glide.with(mContext)
                    .load(thumb)
                    .placeholder(R.drawable.err_load)
                    .error(R.drawable.err_load)
                    .into(holder.binding.imageView);
        } else {
            holder.binding.imageView.setImageResource(R.drawable.err_load);
        }

        holder.binding.fragme.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, TrendySetKeyboardActivity.class);
            intent.putExtra(TrendySetKeyboardActivity.SOURCE_KEY, beanDetails);
            intent.putExtra(TrendySetKeyboardActivity.DISPLAY_URL_KEY, beanDetails.getImgPath());
            intent.putExtra(TrendySetKeyboardActivity.ZIP_URL_KEY, beanDetails.getZipPath());
            intent.putExtra(TrendySetKeyboardActivity.NAME_KEY, beanDetails.getTitleName());
            intent.putExtra(TrendySetKeyboardActivity.GIF_KEY, beanDetails.getImgGif());

            String intentThumb = (thumbGif != null && !thumbGif.isEmpty()) ? thumbGif : thumb;
            intent.putExtra(TrendySetKeyboardActivity.THUMB_KEY, intentThumb);

            if (!(mContext instanceof android.app.Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            mContext.startActivity(intent);
        });
    }

    static class ChildViewHolder extends RecyclerView.ViewHolder {
        AdapterHeCdTrendyBinding binding;

        ChildViewHolder(@NonNull AdapterHeCdTrendyBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
