package com.photo.wallpaper.category.itemAdapter;

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.photo.wallpaper.category.MyApp;
import com.photo.wallpaper.category.R;
import com.photo.wallpaper.category.bean.Data;
import com.photo.wallpaper.category.api.ApiItemClick;
import com.photo.wallpaper.category.bean.DataBaseHelper;
import com.photo.wallpaper.category.databinding.ItemHomeParentBinding;
import com.photo.wallpaper.category.utils.CommonHelper;

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

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

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

    public void setInfoWallpaperListener(ApiItemClick apiItemClick) {
        this.listener = apiItemClick;
    }

    @NonNull
    @Override
    public HomeParentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        ItemHomeParentBinding inflate = ItemHomeParentBinding.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<Data> data1 = DataBaseHelper.queryHome(name);
        String previewUrl400 = data1.get(0).getPreviewUrl1080();

        holder.itemBing.tvClassName.setText(name);

        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(MyApp.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);

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

    }

    private void initChild(HomeParentHolder holder, List<Data> data) {
//        int height = CommonUtils.dp2Px(120);
//        AdapterThumb adapterThumb = new AdapterThumb(mContext,height);
//        adapterThumb.setDataInfo(data);
//        adapterThumb.setInfoWallpaperListener(listener);
//        RecyclerSpace recyclerSpace = new RecyclerSpace(10, 10, 0);
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(MyWallpaper.mAppContext, 2);
//        holder.itemBing.childRecycler.addItemDecoration(recyclerSpace);
//        holder.itemBing.childRecycler.setLayoutManager(gridLayoutManager);
//
//        holder.itemBing.childRecycler.setAdapter(adapterThumb);
    }

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

    static class HomeParentHolder extends RecyclerView.ViewHolder {

        private ItemHomeParentBinding itemBing;

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