package com.blue.running.keyboard.run_adapter;


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.blue.running.keyboard.R;
import com.blue.running.keyboard.run_activity.ActivitySetThemes;
import com.blue.running.keyboard.run_jsonbean.ChildData;
import com.blue.running.keyboard.databinding.ItemChildBinding;
import com.blue.running.keyboard.run_manager.ComUttils;
import com.blue.running.keyboard.run_api.ApiClickItem;

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 ApiClickItem mCallBack;
    private boolean showSpace = false;


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


    public void setClickAction(ApiClickItem 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()) {
            ComUttils.INSTANCE.loadWepJif(mContext, thumbGif, holder.binding.imageView);
        } else {
            Glide.with(mContext).load(thumb)
                    .placeholder(R.drawable.run_im_placeholder)
                    .into(holder.binding.imageView);
        }
        if (showSpace&&(position == mList.size() - 1 || position == mList.size() - 2)) {
            holder.binding.listSpace.setVisibility(View.VISIBLE);
        } else {
            holder.binding.listSpace.setVisibility(View.GONE);
        }

        holder.binding.fragme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, ActivitySetThemes.class);
                intentApply.putExtra(ActivitySetThemes.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;


        }

    }


}
