package com.wall.dream.wallpapers.ItemPag;

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.cardview.widget.CardView;
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.dream.wallpapers.DreamWallpaper;
import com.wall.dream.wallpapers.R;
import com.wall.dream.wallpapers.GJ.ObectBoxManagerGJ;
import com.wall.dream.wallpapers.data.DataBase;
import com.wall.dream.wallpapers.LianXi.OnItemXianClick;
import com.wall.dream.wallpapers.GJ.ComUtilsGJ;

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

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

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


    private List<DataBase> dataInfo = new ArrayList<>();
    private Context myContext;
    private OnItemXianClick onItemxianClick;
    private int itemHeight;

    public void setInfoWallpaperListener(OnItemXianClick onItemxianClick) {
        this.onItemxianClick = onItemxianClick;
    }

    public ItemLikePag(Context myCon) {
        myContext = myCon;
        this.itemHeight = 0; // 默认使用布局中的高度
    }

    public ItemLikePag(Context myCon, int itemHeight) {
        myContext = myCon;
        this.itemHeight = itemHeight;
    }

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

    @NonNull
    @Override
    public LikeVh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(myContext).inflate(R.layout.pag_like, parent, false);
        // 如果指定了高度，则动态设置
        if (itemHeight > 0) {
            CardView cardView = itemView.findViewById(R.id.like_card);
            if (cardView != null) {
                ViewGroup.LayoutParams params = cardView.getLayoutParams();
                params.height = itemHeight;
                cardView.setLayoutParams(params);
            }
        }
        return new LikeVh(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull LikeVh holder, int position) {
        DataBase info = dataInfo.get(position);
        ImageView imageView = holder.getImageView();
        View favoriteContainer = holder.getFavoriteContainer();
        ImageView favoriteIcon = holder.getFavoriteIcon();
        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(ComUtilsGJ.dp2Px(12), 0) // 圆角效果
        );
        Glide.with(myContext)
                .load(previewUrl400)
                .placeholder(R.mipmap.im_placeholder)
                .transform(multiTransformation)
                .into(imageView);

        // 设置心形图标为选中状态
        if (favoriteIcon != null) {
            favoriteIcon.setSelected(true);
        }
        
        // 设置收藏按钮点击事件
        if (favoriteContainer != null) {
            favoriteContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (favoriteIcon != null) {
                        favoriteIcon.setSelected(false);
                    }
                    info.setLike(false);
                    ObectBoxManagerGJ.updateLike(info);
                    int adapterPosition = holder.getAdapterPosition();
                    notifyItemRemoved(adapterPosition);
                    Log.d(DreamWallpaper.TAG, "----------notifyItemRemoved-adapterPosition=" + adapterPosition);
                }
            });
        }
        holder.getLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemxianClick != null) {
                    onItemxianClick.onItemClickAction(info);
                }

            }
        });

    }

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

    public class LikeVh extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private RelativeLayout layout;
        private View favoriteContainer;
        private ImageView favoriteIcon;
        private View spaceView;

        public LikeVh(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.wallpaper);
            layout = itemView.findViewById(R.id.layout);
            favoriteContainer = itemView.findViewById(R.id.image_favorite);
            favoriteIcon = itemView.findViewById(R.id.favorite_icon);
            spaceView = itemView.findViewById(R.id.space_view);
        }

        public View getSpaceView() {
            return spaceView;
        }

        public View getFavoriteContainer() {
            return favoriteContainer;
        }

        public ImageView getFavoriteIcon() {
            return favoriteIcon;
        }

        public RelativeLayout getLayout() {
            return layout;
        }

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