package com.apps.chillscreen.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.apps.chillscreen.R;
import com.apps.chillscreen.bean.Picture;
import com.apps.chillscreen.databinding.ItemBookmarkBinding;
import com.apps.chillscreen.listener.BookmarkClickListener;
import com.apps.chillscreen.tool.PictureManager;
import com.apps.chillscreen.tool.OpenUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

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


public class BookMarkAdapter extends RecyclerView.Adapter<BookMarkAdapter.CollectViewHolder> {
    private Context context;
    private List<Picture> pictures = new ArrayList<>();
    private BookmarkClickListener listener;

    @Override
    public void onBindViewHolder(@NonNull BookMarkAdapter.CollectViewHolder holder, int position) {
        Picture picture = pictures.get(position);
        MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
                new CenterCrop(),
                new RoundedCorners(OpenUtil.dpToPx(context, 12))
        );
        Glide.with(context)
                .load(picture.getPreviewUrl1080())
                .placeholder(R.mipmap.img_placeholder)
                .transform(multiTransformation)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .thumbnail(
                        Glide.with(context)
                                .asDrawable()
                                .load(picture.getPreviewUrl200())
                                .apply(new RequestOptions()
                                        .format(DecodeFormat.PREFER_RGB_565)
                                        .transform(multiTransformation)
                                )
                                .placeholder(R.mipmap.img_placeholder)
                )
                .into(holder.binding.wallpaper);
        holder.binding.rlSet.setOnClickListener(v -> {
            listener.onSetClick(picture, position);
        });
        holder.binding.imgCollect.setClipToOutline(true);
        holder.binding.rlDown.setOnClickListener(v -> {
            listener.onDownloadClick(picture, position);
        });
        holder.binding.imgCollect.setSelected(true);
        holder.binding.imgCollect.setOnClickListener(v -> {
            if (position >= 0 && position < pictures.size()) {
                boolean selected = holder.binding.imgCollect.isSelected();
                holder.binding.imgCollect.setSelected(!selected);
                boolean selectedNew = holder.binding.imgCollect.isSelected();
                picture.setLike(selectedNew);
                PictureManager.updateCollect(picture);
                listener.onRemoveClick(position);
            }
        });
        holder.binding.tvName.setText(picture.getDescription());
    }

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

    public void setPicture(List<Picture> pictureList) {
        this.pictures = pictureList;
        notifyDataSetChanged();
    }

    public Picture getPictureAt(int position) {
        if (position >= 0 && position < pictures.size()) {
            return pictures.get(position);
        }
        return null;
    }

    public void setClickListener(BookmarkClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public CollectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        ItemBookmarkBinding inflate = ItemBookmarkBinding.inflate(LayoutInflater.from(context), parent, false);
        return new CollectViewHolder(inflate);
    }

    static class CollectViewHolder extends RecyclerView.ViewHolder {
        private ItemBookmarkBinding binding;

        public CollectViewHolder(ItemBookmarkBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }
}

