package com.key.board.journey.inputmethod.list;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.key.board.journey.inputmethod.R;
import com.key.board.journey.inputmethod.data.ChildBean;
import com.key.board.journey.inputmethod.tools.Common;
import com.key.board.journey.inputmethod.myInter.InterfaceUpdateLike;
import com.key.board.journey.inputmethod.activity.ApplyActivity;

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

public class ItemFavorite extends RecyclerView.Adapter<ItemFavorite.ForYouViewHolder> {

    private Context mContext;
    private List<ChildBean> mList = new ArrayList<>();

    private InterfaceUpdateLike mCallBack;

    public ItemFavorite(Context context) {
        mContext = context;
    }

    public void setForYouList(List<ChildBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }


    public void setRemoveLike(InterfaceUpdateLike callback) {
        mCallBack = callback;
    }

    @NonNull
    @Override
    public ForYouViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_favorite, parent, false);
        return new ForYouViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ForYouViewHolder holder, int position) {

        ChildBean childBean = mList.get(position);
        String thumbGif = childBean.getThumbGif();
        String thumb = childBean.getThumbUrl();
        if (!thumbGif.isEmpty()) {
            Common.INSTANCE.loadWepJif(mContext, thumbGif, holder.itemImg);
        } else {
            Glide.with(mContext).load(thumb)
                    .placeholder(R.drawable.default_placeholder)
                    .into(holder.itemImg);
        }

        boolean b = position == mList.size() - 1;
        boolean b1 = position == mList.size() - 2;
        if (mList.size() % 2 == 0 && (b || b1)) {
            holder.ViewSpace.setVisibility(View.VISIBLE);
        } else if (mList.size() % 2 != 0 && b) {
            holder.ViewSpace.setVisibility(View.VISIBLE);
        } else {
            holder.ViewSpace.setVisibility(View.GONE);
        }
        holder.itemFavorite.setSelected(true);
        holder.layoutFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.itemFavorite.setSelected(false);
                int adapterPosition = holder.getAdapterPosition();
                notifyItemRemoved(adapterPosition);
                if (mCallBack != null) {
                    mCallBack.onRemoveLike(childBean);
                }
            }
        });
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, ApplyActivity.class);
                intentApply.putExtra(ApplyActivity.SOURCE_KEY, childBean);
                mContext.startActivity(intentApply);


            }
        });


    }

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

    public static class ForYouViewHolder extends RecyclerView.ViewHolder {

        private CardView cardView;
        private FrameLayout layoutFavorite;
        private ImageView itemImg, itemFavorite;
        private View ViewSpace;

        public ForYouViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.card_view);
            layoutFavorite = itemView.findViewById(R.id.layout_favorite);
            itemImg = itemView.findViewById(R.id.im);
            itemFavorite = itemView.findViewById(R.id.im_favorite);
            ViewSpace = itemView.findViewById(R.id.like_space);

        }

    }

}
