package com.themes.keyboard.black.recycler;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.bumptech.glide.Glide;
import com.themes.keyboard.black.R;
import com.themes.keyboard.black.activity.ApplyActivity;
import com.themes.keyboard.black.databinding.ItemChildBinding;
import com.themes.keyboard.black.listener.OnClickListener;
import com.themes.keyboard.black.localdata.ChildData;
import com.themes.keyboard.black.utils.Common;

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

public class AdapterMainChild extends RecyclerView.Adapter<AdapterMainChild.ChildViewHolder> {

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

    private OnClickListener mCallBack;
    private boolean showSpace = false;


    public AdapterMainChild(Context context, List<ChildData> list) {
        mContext = context;
        this.mList = list;
    }


    public void setClickAction(OnClickListener callback) {
        mCallBack = callback;
    }

    @NonNull
    @Override
    public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemChildBinding inflate = ItemChildBinding.inflate(LayoutInflater.from(parent.getContext()));


        return new ChildViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ChildViewHolder holder, int position) {
        ChildData childData = mList.get(position);


        String thumbGif = childData.getThumbGif();
        String thumb = childData.getThumbUrl();
        if (!thumbGif.isEmpty()) {
            Common.INSTANCE.loadWepJif(mContext, thumbGif, holder.binding.imageView);
        } else {
            Glide.with(mContext).load(thumb)
                    .placeholder(R.drawable.def_placeholder)
                    .into(holder.binding.imageView);
        }
        if (position == mList.size() - 1) {
            holder.binding.space.setVisibility(View.VISIBLE);
        } else {
            holder.binding.space.setVisibility(View.GONE);
        }

        holder.binding.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, ApplyActivity.class);
                intentApply.putExtra(ApplyActivity.SOURCE_KEY, childData);
                mContext.startActivity(intentApply);
                if (mCallBack != null) {
                    mCallBack.onItemClick();
                }

            }
        });


    }

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

    public static class ChildViewHolder extends RecyclerView.ViewHolder {

        private ItemChildBinding binding;


        public ChildViewHolder(@NonNull ItemChildBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;


        }

    }


}
