package com.phone.wall.paper.adapter;

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.phone.wall.paper.R;
import com.phone.wall.paper.WallpaperApp;
import com.phone.wall.paper.data.DataBean;
import com.phone.wall.paper.databinding.AdaMainBinding;
import com.phone.wall.paper.listener.OnClick;
import com.phone.wall.paper.tools.Utils;
import com.phone.wall.paper.tools.RoomManager;

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class MainAda extends RecyclerView.Adapter<MainAda.HomeParentHolder> {
    private List<String> data = new ArrayList<>();
    private Context mContext;
    private OnClick listener;
    int max = 9;
    int min = 0;
    int index = 0;
    private MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
            new CenterCrop(),
            new RoundedCornersTransformation(Utils.dp2Px(12), 0) // 圆角效果
    );

    public void setData(List<String> data) {
        this.data = data;
        Random random = new Random();
        index = random.nextInt(max - min + 1) + min;
        Log.d("---------","-------index="+index);
        notifyDataSetChanged();
    }

    public void setInfoWallpaperListener(OnClick onClick) {
        this.listener = onClick;
    }

    @NonNull
    @Override
    public HomeParentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        AdaMainBinding inflate = AdaMainBinding.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<DataBean> dataBean1 = RoomManager.queryHome(name);
        String previewUrl400 = dataBean1.get(index).getPreviewUrl1080();
        holder.itemBing.tvClassName.setText(name);

        int adapterPosition = holder.getAdapterPosition();
        if(adapterPosition == data.size()-1){
            holder.itemBing.viewSpace.setVisibility(View.VISIBLE);
        }else {
            holder.itemBing.viewSpace.setVisibility(View.GONE);
        }

        Glide.with(mContext)
                .asDrawable()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .load(previewUrl400)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        Log.d(WallpaperApp.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)
                .into(holder.itemBing.preview);


        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 AdaMainBinding itemBing;

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