package com.newapp.paperwall.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.RenderEffect;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.os.Build;
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.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.newapp.paperwall.BaseApp;
import com.newapp.paperwall.R;
import com.newapp.paperwall.databinding.ItemHomeBinding;
import com.newapp.paperwall.javabean.Data;
import com.newapp.paperwall.listener.OnItemClickListener;
import com.newapp.paperwall.tools.UiUtils;
import com.newapp.paperwall.tools.DbManager;

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

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.HomeParentHolder> {
    private List<String> data = new ArrayList<>();
    private Context mContext;
    private OnItemClickListener listener;
    private MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
            new CenterCrop(),
            new RoundedCorners(UiUtils.dp2Px(20)) // 圆角效果
    );

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

    public void setInfoWallpaperListener(OnItemClickListener onItemClickListener) {
        this.listener = onItemClickListener;
    }

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

        holder.itemBing.tvClassName.setText(name);
        if(position==getItemCount()-1){
            holder.itemBing.bottomView.setVisibility(View.VISIBLE);
        }else{
            holder.itemBing.bottomView.setVisibility(View.GONE);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            RenderEffect blurEffect = RenderEffect.createBlurEffect(60, 60, Shader.TileMode.CLAMP);
            holder.itemBing.llBlur.setRenderEffect(blurEffect);
        }
        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(BaseApp.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.img_placeholder)
//                .transform(multiTransformation)
                .into(holder.itemBing.preview);

//        initChild(holder,data1);
        holder.itemBing.preview.setOnClickListener(v -> listener.onClickMore(name));

    }



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

    static class HomeParentHolder extends RecyclerView.ViewHolder {

        private ItemHomeBinding itemBing;

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