package com.wall.photography.wallpapers.item;

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.photography.wallpapers.MyApp;
import com.wall.photography.wallpapers.R;
import com.wall.photography.wallpapers.tool.ObectBoxManager;
import com.wall.photography.wallpapers.mybean.MyData;
import com.wall.photography.wallpapers.connector.OnItemClick;
import com.wall.photography.wallpapers.tool.ComUtils;

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

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


    private List<MyData> dataInfo = new ArrayList<>();
    private Context myContext;
    private OnItemClick onItemClick;

    public void setInfoWallpaperListener(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

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

    public void setDataInfo(List<MyData> 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) {
        MyData info = dataInfo.get(position);
        ImageView imageView = holder.getImageView();
        ImageView imFavorite = holder.getImFavorite();
        String previewUrl400 = info.getPreviewUrl400();
        String imId = info.getImId();
        View spaceView = holder.getSpaceView();
        if (position == dataInfo.size() - 1) {
            spaceView.setVisibility(View.VISIBLE);
        } else {
            spaceView.setVisibility(View.GONE);
        }
        MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
                new CenterCrop(),
                new RoundedCornersTransformation(ComUtils.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);
                ObectBoxManager.updateLike(info);
                int adapterPosition = holder.getAdapterPosition();
                notifyItemRemoved(adapterPosition);
                Log.d(MyApp.TAG, "----------notifyItemRemoved-adapterPosition=" + adapterPosition);
            }
        });
        holder.getLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null) {
                    onItemClick.onItemClickAction(info);
                }

            }
        });

    }

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

    public class LikeVh extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private RelativeLayout layout;
        private ImageView imFavorite;
        private View spaceView;

        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);
            spaceView = itemView.findViewById(R.id.space_view);
        }

        public View getSpaceView() {
            return spaceView;
        }

        public ImageView getImFavorite() {
            return imFavorite;
        }

        public RelativeLayout getLayout() {
            return layout;
        }

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