package com.wall.turing.wallpapers.adapteres;

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.wall.turing.wallpapers.R;
import com.wall.turing.wallpapers.myselfbean.Wallpaper;
import com.wall.turing.wallpapers.connect.TuringItemClick;
import com.wall.turing.wallpapers.uutils.AppUtils;

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

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


    private List<Wallpaper> dataInfo = new ArrayList<>();

    private TuringItemClick onItemClick;
    private WeakReference<Context> weakReference;
    private int itemHeight;
    private boolean showSpace = false;


    public void setInfoWallpaperListener(TuringItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public ListAdapter(Context myCon, int itemHeight, boolean show) {
        weakReference = new WeakReference<>(myCon);
        this.itemHeight = itemHeight;
        showSpace = show;
    }

    public void setDataInfo(List<Wallpaper> dataInfo) {
        this.dataInfo = dataInfo;
        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.list_adapter, 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) {
        Wallpaper info = dataInfo.get(position);
        ImageView imageView = holder.getImageView();
        String previewUrl400 = info.getPreviewUrl400();
        String previewUrl200 = info.getPreviewUrl200();
        RelativeLayout item = holder.getLayout();
        String imId = info.getImId();
        int curheight;
        View spaceView = holder.getSpaceView();
        if (showSpace && position == dataInfo.size() - 1) {
            spaceView.setVisibility(View.VISIBLE);
        } else {
            spaceView.setVisibility(View.GONE);
        }


        MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
                new CenterCrop(),
                new RoundedCornersTransformation(AppUtils.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 (onItemClick != null) {
                    onItemClick.onItemClickAction(info);
                }

            }
        });

    }

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

    @Override
    public void onViewRecycled(@NonNull MyViewHolder holder) {
        super.onViewRecycled(holder);


    }


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


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

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

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

        public MyCallback(List<Wallpaper> oldList, List<Wallpaper> 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 spaceView;

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

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

        public RelativeLayout getLayout() {
            return layout;
        }

        public ImageView getImageView() {
            return imageView;
        }

        public View getSpaceView() {
            return spaceView;
        }
    }

}
