package com.app.input.inputaura.onjfsgvfsf;

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

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

import com.app.input.inputaura.R;
import com.app.input.inputaura.dasda.Shikamaru;
import com.app.input.inputaura.databinding.TibsgdgBinding;
import com.app.input.inputaura.lmyfvfs.ViyVyg;
import com.app.input.inputaura.kfsdfbsd.IutFhfg;
import com.bumptech.glide.Glide;

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

public class DubTbf extends RecyclerView.Adapter<DubTbf.CategoryViewHolder> {

    private Context mContext;
    private List<Shikamaru> mList = new ArrayList<>();
    private int screenWidth;
    private int gridSpacing;
    private int recyclerPadding; // 存储RecyclerView的左右padding

    public DubTbf(Context context, List<Shikamaru> list) {
        mContext = context;
        this.mList = list;

        // 获取屏幕宽度、间距和padding
        screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        gridSpacing = context.getResources().getDimensionPixelSize(R.dimen.grid_spacing);
        // 从dimens.xml获取RecyclerView的padding，如果没有定义则默认为10dp
        recyclerPadding = context.getResources().getDimensionPixelSize(R.dimen.recycler_padding);
    }

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

    @Override
    public void onBindViewHolder(@NonNull CategoryViewHolder holder, int position) {
        // 动态设置卡片高度为4:3比例
        int columnCount = 2; // 网格列数

        // 修正：可用宽度 = 屏幕宽度 - (左右padding × 2) - (列间距 × (列数 + 1))
        // 10dp(左padding) + 10dp(右padding) + 间距
        int availableWidth = screenWidth - (recyclerPadding * 2) - (gridSpacing * (columnCount + 1));
        int itemWidth = availableWidth / columnCount;
        int itemHeight = (int) (itemWidth * 0.75); // 4:3比例，高度 = 宽度 × 3/4

        // 设置卡片布局参数
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        layoutParams.width = itemWidth;
        layoutParams.height = itemHeight;
        holder.itemView.setLayoutParams(layoutParams);

        // 加载数据
        Shikamaru shikamaru = mList.get(position);

        // 加载图片
        String thumbGif = shikamaru.getThumbGif();
        String thumb = shikamaru.getThumbUrl();

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

        // 点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, ViyVyg.class);
                intentApply.putExtra(ViyVyg.SOURCE_KEY, shikamaru);
                intentApply.putExtra(ViyVyg.DISPLAY_URL_KEY, shikamaru.getImgPath());
                intentApply.putExtra(ViyVyg.ZIP_URL_KEY, shikamaru.getZipPath());
                intentApply.putExtra(ViyVyg.NAME_KEY, shikamaru.getTitleName());
                intentApply.putExtra(ViyVyg.GIF_KEY, shikamaru.getImgGif());

                String intent_thumb;
                if (thumbGif != null && !thumbGif.isEmpty()) {
                    intent_thumb = thumbGif;
                } else {
                    intent_thumb = thumb;
                }
                intentApply.putExtra(ViyVyg.THUMB_KEY, intent_thumb);

                mContext.startActivity(intentApply);
            }
        });
    }

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

    public static class CategoryViewHolder extends RecyclerView.ViewHolder {
        private TibsgdgBinding binding;

        public CategoryViewHolder(@NonNull TibsgdgBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }
}