package com.phone.wall.paper.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.DecodeFormat;
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.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.phone.wall.paper.R;
import com.phone.wall.paper.data.DataBean;
import com.phone.wall.paper.listener.OnClick;
import com.phone.wall.paper.tools.Utils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class AllAda extends RecyclerView.Adapter<AllAda.MyViewHolder> {


    private List<DataBean> dataBeanInfo = new ArrayList<>();

    private OnClick onClick;
    private WeakReference<Context> weakReference;
    private int itemHeight;


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

    public AllAda(Context myCon, int itemHeight) {
        weakReference = new WeakReference<>(myCon);
        this.itemHeight = itemHeight;
    }

    public void setDataInfo(List<DataBean> dataBeanInfo) {
        this.dataBeanInfo = dataBeanInfo;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = weakReference.get();
        if (context != null) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.ada_all, parent, false);
            ImageView imageView = itemView.findViewById(R.id.wallpaper);
            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            layoutParams.height = itemHeight;
            imageView.setLayoutParams(layoutParams);
            return new MyViewHolder(itemView);
        }
        return null;


    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        DataBean info = dataBeanInfo.get(position);
        ImageView imageView = holder.getImageView();
        String previewUrl400 = info.getPreviewUrl400();
        String previewUrl200 = info.getPreviewUrl200();
        RelativeLayout item = holder.getLayout();
        View space = holder.getSpace();
        String imId = info.getImId();
        int curheight;

        int adapterPosition = holder.getAdapterPosition();
        if (adapterPosition == dataBeanInfo.size() - 1) {
            space.setVisibility(View.VISIBLE);
        } else {
            space.setVisibility(View.GONE);
        }
        MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
                new CenterCrop(),
                new RoundedCornersTransformation(Utils.dp2Px(12), 0) // 圆角效果
        );
        RequestOptions options = new RequestOptions()
                .format(DecodeFormat.PREFER_RGB_565);
        Context context = weakReference.get();
        if (context != null) {
            Glide.with(context)
                    .asDrawable()
                    .skipMemoryCache(true)
                    .apply(options)
                    .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) {
                            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(imageView);
        }


        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null) {
                    onClick.onItemClickAction(info);
                }

            }
        });

    }

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

    @Override
    public void onViewRecycled(@NonNull MyViewHolder holder) {
        super.onViewRecycled(holder);
//        Context context = weakReference.get();
//        if (context != null) {
//            Glide.with(context).clear(holder.getImageView());
//            Log.d(MyWallpaper.TAG, "----------onViewRecycled");
//        }

    }


    private static DiffUtil.ItemCallback<DataBean> diff = new DiffUtil.ItemCallback<DataBean>() {


        @Override
        public boolean areItemsTheSame(@NonNull DataBean oldItem, @NonNull DataBean newItem) {
            return oldItem.getImId().equals(newItem.getImId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull DataBean oldItem, @NonNull DataBean newItem) {
            return oldItem.getImId().equals(newItem.getImId());
        }
    };

    private static class MyCallback extends DiffUtil.Callback {
        private List<DataBean> mOldList;
        private List<DataBean> mNewList;

        public MyCallback(List<DataBean> oldList, List<DataBean> newList) {
            mOldList = oldList;
            mNewList = newList;
        }

        @Override
        public int getOldListSize() {
            return mOldList.size();
        }

        @Override
        public int getNewListSize() {
            return mNewList.size();
        }

        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            return mOldList.get(oldItemPosition).getImId().equals(mNewList.get(newItemPosition).getImId());
        }

        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            return mOldList.get(oldItemPosition).getImId().equals(mNewList.get(newItemPosition).getImId());
        }
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private RelativeLayout layout;

        private View space;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.wallpaper);
            layout = itemView.findViewById(R.id.layout);
            space = itemView.findViewById(R.id.view_space);
        }

//        public ImageView getImFavorite() {
//            return imFavorite;
//        }


        public View getSpace() {
            return space;
        }

        public RelativeLayout getLayout() {
            return layout;
        }

        public ImageView getImageView() {
            return imageView;
        }
    }

}
