package com.wall.snap.wallpaper.itemsnap;

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

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

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


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

    private com.wall.snap.wallpaper.connectorsnap.OnItemsnapClick onItemtwoClick;
    private WeakReference<Context> weakReference;
    private int itemHeight;
    private boolean showSpace = false;


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

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

    public void setDataInfo(List<MysnapData> 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.item_listsnap, 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) {
        MysnapData 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(ComUtilssnap.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 (onItemtwoClick != null) {
                    onItemtwoClick.onItemClickAction(info);
                }

            }
        });

    }

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

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


    }


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


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

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

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

        public MyCallback(List<MysnapData> oldList, List<MysnapData> 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;
        }
    }

}
