package com.phone.wall.paper.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.phone.wall.paper.WallpaperApp;
import com.phone.wall.paper.R;
import com.phone.wall.paper.data.DataBean;
import com.phone.wall.paper.tools.RoomManager;
import com.phone.wall.paper.listener.OnClick;
import com.phone.wall.paper.tools.Utils;

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

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


    private List<DataBean> dataBeanInfo = new ArrayList<>();
    private Context myContext;
    private OnClick onClick;

    public void setInfoWallpaperListener(OnClick onClick) {
        this.onClick = onClick;
    }

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

    public void setDataInfo(List<DataBean> dataBeanInfo) {
        this.dataBeanInfo = dataBeanInfo;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull LikeVh holder, int position) {
        DataBean info = dataBeanInfo.get(position);
        ImageView imageView = holder.getImageView();
        ImageView imFavorite = holder.getImFavorite();
        String previewUrl400 = info.getPreviewUrl400();
        String imId = info.getImId();
        View space = holder.getSpace();

        int adapterPosition = holder.getAdapterPosition();
        if (adapterPosition == dataBeanInfo.size() - 1) {
            space.setVisibility(View.VISIBLE);
        } else {
            space.setVisibility(View.GONE);
        }

        MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(
                new CenterCrop(),
                new RoundedCornersTransformation(Utils.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);
                RoomManager.updateLike(info);
                int adapterPosition = holder.getAdapterPosition();
                notifyItemRemoved(adapterPosition);
                Log.d(WallpaperApp.TAG,"----------notifyItemRemoved-adapterPosition="+adapterPosition);
            }
        });
        holder.getLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null) {
                    onClick.onItemClickAction(info);
                }

            }
        });

    }

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

    public class LikeVh extends RecyclerView.ViewHolder{

        private ImageView imageView;
        private RelativeLayout layout;
        private ImageView imFavorite;
        private View space;
        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);
            space = itemView.findViewById(R.id.view_space);
        }

        public View getSpace() {
            return space;
        }

        public ImageView getImFavorite() {
            return imFavorite;
        }

        public RelativeLayout getLayout() {
            return layout;
        }

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