package com.hd.wallpaper.free.walls.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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.resource.bitmap.CenterCrop;
import com.hd.wallpaper.free.walls.MyWallpaper;
import com.hd.wallpaper.free.walls.R;
import com.hd.wallpaper.free.walls.mylistener.ItemClickListener;
import com.hd.wallpaper.free.walls.utils.CommonUtils;
import com.hd.wallpaper.free.walls.db.DbManager;
import com.hd.wallpaper.free.walls.json.Data;

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class AdapterFavorite extends RecyclerView.Adapter<MyViewHolder> {


    private List<Data> dataInfo = new ArrayList<>();
    private Context myContext;
    private ItemClickListener itemClickListener;

    public void setInfoWallpaperListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public AdapterFavorite(Context myCon) {
        myContext = myCon;
    }

    public void setDataInfo(List<Data> dataInfo) {
        this.dataInfo = dataInfo;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(myContext).inflate(R.layout.item_preview, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Data info = dataInfo.get(position);
        ImageView imageView = holder.getImageView();
        ImageView imFavorite = holder.getImFavorite();
        String previewUrl400 = info.getPreviewUrl400();
        String imId = info.getImId();

        MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
                new CenterCrop(),
                new RoundedCornersTransformation(CommonUtils.dp2Px(12), 0) // 圆角效果
        );
        Glide.with(myContext)
                .load(previewUrl400)
                .placeholder(R.mipmap.im_placeholder)
                .transform(multiTransformation)
                .into(imageView);

        imFavorite.setSelected(true);
        imFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imFavorite.setSelected(false);
                DbManager.deleteLike(info);
                int adapterPosition = holder.getAdapterPosition();
                notifyItemRemoved(adapterPosition);
                Log.d(MyWallpaper.TAG,"----------notifyItemRemoved-adapterPosition="+adapterPosition);
            }
        });
        holder.getLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.onItemClickAction(info);
                }

            }
        });

    }

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


}
