package com.wall.turing.wallpapers.adapteres;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.wall.turing.wallpapers.WallpaperApplication;
import com.wall.turing.wallpapers.R;
import com.wall.turing.wallpapers.databinding.MainAdapterBinding;
import com.wall.turing.wallpapers.uutils.AppUtils;
import com.wall.turing.wallpapers.uutils.DatabaseManager;
import com.wall.turing.wallpapers.connect.TuringItemClick;
import com.wall.turing.wallpapers.myselfbean.Wallpaper;

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.HomeParentHolder> {
    private List<String> data = new ArrayList<>();
    private Context mContext;
    private TuringItemClick listener;
    private MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
            new CenterCrop(),
            new RoundedCornersTransformation(AppUtils.dp2Px(12), 0) // 圆角效果
    );

    public void setData(List<String> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setInfoWallpaperListener(TuringItemClick onItemClick) {
        this.listener = onItemClick;
    }

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

    @Override
    public void onBindViewHolder(@NonNull HomeParentHolder holder, int position) {
        String name = data.get(position);
        List<Wallpaper> data1 = DatabaseManager.queryHome(name);
        
        // 显示时去掉"Turing"后缀
        String displayName = name.replace("Turing", "").replace("Tuirng", "");
        holder.itemBing.tvClassName.setText(displayName);
        if(position == data.size()-1){
            holder.itemBing.spaceView.setVisibility(View.VISIBLE);
        }else {
            holder.itemBing.spaceView.setVisibility(View.GONE);
        }

        // 检查数据是否为空
        if (data1 != null && !data1.isEmpty()) {
            String previewUrl400 = data1.get(0).getPreviewUrl1080();
            
            Glide.with(mContext)
                    .asDrawable()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .load(previewUrl400)
//                    .centerCrop()
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                            Log.d(WallpaperApplication.TAG, "------------e="+e.getMessage()+"---previewUrl400="+previewUrl400);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                            return false;
                        }
                    })
                    .placeholder(R.mipmap.im_placeholder)
//                    .transform(multiTransformation)
                    .into(holder.itemBing.preview);
        } else {
            // 如果数据为空，显示占位图
            Log.d(WallpaperApplication.TAG, "------------分类 " + name + " 暂无数据");
            Glide.with(mContext)
                    .load(R.mipmap.im_placeholder)
                    .into(holder.itemBing.preview);
        }

//        initChild(holder,data1);
        holder.itemBing.preview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClickMore(name);
            }
        });

    }



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

    static class HomeParentHolder extends RecyclerView.ViewHolder {

        private MainAdapterBinding  itemBing;

        public HomeParentHolder(@NonNull MainAdapterBinding itemView) {
            super(itemView.getRoot());
            itemBing = itemView;
        }
    }
}
