package com.wall.wallpaper.hd.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 android.widget.RelativeLayout;

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.wall.wallpaper.hd.WallApp;
import com.wall.wallpaper.hd.R;
import com.wall.wallpaper.hd.tools.DbManager;
import com.wall.wallpaper.hd.javabean.Data;
import com.wall.wallpaper.hd.listener.ItemClickListener;
import com.wall.wallpaper.hd.tools.CommonUtils;

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class AdapterFavorite extends RecyclerView.Adapter<AdapterFavorite.LikeVh> {


    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 LikeVh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(myContext).inflate(R.layout.item_like, parent, false);
        return new LikeVh(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull LikeVh 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);
                info.setLike(false);
                DbManager.updateLike(info);
                int adapterPosition = holder.getAdapterPosition();
                notifyItemRemoved(adapterPosition);
                Log.d(WallApp.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();
    }

    public class LikeVh extends RecyclerView.ViewHolder{

        private ImageView imageView;
        private RelativeLayout layout;
        private ImageView imFavorite;
        public LikeVh(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.wallpaper);
            layout = itemView.findViewById(R.id.layout);
            imFavorite = itemView.findViewById(R.id.image_favorite);
        }

        public ImageView getImFavorite() {
            return imFavorite;
        }

        public RelativeLayout getLayout() {
            return layout;
        }

        public ImageView getImageView() {
            return imageView;
        }
    }
}
