package com.wall.snap.wallpaper.itemsnap;

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.snap.wallpaper.MysnapApp;
import com.wall.snap.wallpaper.R;
import com.wall.snap.wallpaper.databinding.ItemMainsnapBinding;
import com.wall.snap.wallpaper.toolsnap.ComUtilssnap;
import com.wall.snap.wallpaper.toolsnap.ObectBoxManagersnap;
import com.wall.snap.wallpaper.mysnapbean.MysnapData;

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class ItemsnapMain extends RecyclerView.Adapter<ItemsnapMain.HomeParentHolder> {
    private List<String> data = new ArrayList<>();
    private Context mContext;
    private com.wall.snap.wallpaper.connectorsnap.OnItemsnapClick listener;
    private MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
            new CenterCrop(),
            new RoundedCornersTransformation(ComUtilssnap.dp2Px(12), 0) // 圆角效果
    );

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

    public void setInfoWallpaperListener(com.wall.snap.wallpaper.connectorsnap.OnItemsnapClick onItemtwoClick) {
        this.listener = onItemtwoClick;
    }

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

        holder.itemBing.tvClassName.setText(name);
        if(position == data.size()-1){
            holder.itemBing.spaceView.setVisibility(View.VISIBLE);
        }else {
            holder.itemBing.spaceView.setVisibility(View.GONE);
        }

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

    }



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

    static class HomeParentHolder extends RecyclerView.ViewHolder {

        private ItemMainsnapBinding  itemBing;

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