package com.newapp.paperwall.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

import com.bumptech.glide.Glide;
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.newapp.paperwall.R;
import com.newapp.paperwall.javabean.Data;
import com.newapp.paperwall.listener.CollectListener;
import com.newapp.paperwall.tools.UiUtils;
import com.newapp.paperwall.tools.DbManager;

import java.util.List;

public class WallpaperAdapter extends RecyclerView.Adapter<WallpaperAdapter.ViewHolder> {

    private List<Data> wallPapers;
    private Context myContext;
    private CollectListener listener;

    public WallpaperAdapter(Context myCon,List<Data> wallPapers) {
        this.myContext=myCon;
        this.wallPapers=wallPapers;
    }

    public void setListener(CollectListener listener) {
        this.listener = listener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setDataInfo(List<Data> dataInfo) {
        this.wallPapers = dataInfo;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_wallpaper, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Data wallpaper = wallPapers.get(position);
        MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
                new CenterCrop(),
                new RoundedCorners(UiUtils.dp2Px(12)) // 圆角效果
        );
        Glide.with(myContext)
                .load(wallpaper.getPreviewUrl1080())
                .placeholder(R.mipmap.img_placeholder)
                .transform(multiTransformation)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.imageView);
        holder.rlSet.setOnClickListener(v->{
                listener.onSetClick(wallpaper,position);
        });
        holder.imageView.setClipToOutline(true);
        holder.rlDown.setOnClickListener(v->{
            listener.onDownloadClick(wallpaper,position);
        });
        holder.imgCollect.setSelected(true);
        holder.imgCollect.setOnClickListener(v->{
            if(position>=0&&position< wallPapers.size()){
                boolean selected = holder.imgCollect.isSelected();
                holder.imgCollect.setSelected(!selected);
                boolean selectedNew = holder.imgCollect.isSelected();
                wallpaper.setLike(selectedNew);
                DbManager.updateLike(wallpaper);
                listener.onRemoveClick(position);
            }
        });
        holder.tvName.setText(wallpaper.getDescription());

    }


    @Override
    public int getItemCount() {
        return wallPapers.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        RelativeLayout rlSet;
        RelativeLayout rlDown;
        ImageView imgCollect;
        TextView tvName;
        ImageView imgSet;

        public ViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.wallpaper);
            rlSet = view.findViewById(R.id.rlSet);
            imgSet = view.findViewById(R.id.imgSet);
            rlDown = view.findViewById(R.id.rlDown);
            imgCollect = view.findViewById(R.id.imgCollect);
            tvName = view.findViewById(R.id.tvName);
        }
    }
}
