package com.offline.music.playermp3.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

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

import com.offline.music.playermp3.MusicApplication;
import com.offline.music.playermp3.databinding.ItemFooterLoadingBinding;
import com.offline.music.playermp3.databinding.ItemHomeBinding;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.javabean.response.child.ResponseCategory;
import com.offline.music.playermp3.javabean.response.child.ResponseHomeChild;
import com.offline.music.playermp3.javabean.response.child.ResponseSingle;

import java.util.List;

public class AdapterHome extends BaseAdapter<ResponseHomeChild, ItemHomeBinding> {

    @Override
    protected ItemHomeBinding getViewBinding(ViewGroup parent) {

        return ItemHomeBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == TYPE_ITEM) {
            VHolder<ItemHomeBinding> itemHolder = (VHolder<ItemHomeBinding>) holder;
            ItemHomeBinding vb = itemHolder.getVb();
            ResponseHomeChild responseHomeChild = data.get(position);
            vb.headTitle.setText(responseHomeChild.getHeaderTitle());

            List<ResponseSingle> singleList = responseHomeChild.getSingleList();
            List<ResponseCategory> categoryList = responseHomeChild.getCategoryList();
//            CommonUtils.LogMsg("position="+position+"-----------headTitle-=" + responseHomeChild.getHeaderTitle()+"--singleList="+singleList+"---categoryList="+categoryList);
            if (singleList != null && singleList.size() > 0) {
                vb.recyclerSinger.setVisibility(View.VISIBLE);
                AdapterSinger adapterSinger = new AdapterSinger();
                adapterSinger.setHomeItemClickListener(homeItemClickListener);
                adapterSinger.addData(singleList);
                vb.recyclerSinger.setLayoutManager(new GridLayoutManager(MusicApplication.myApplication, 4, RecyclerView.HORIZONTAL, false));
                vb.recyclerSinger.setAdapter(adapterSinger);
            } else {
                vb.recyclerSinger.setVisibility(View.GONE);
            }

            if (categoryList != null && categoryList.size() > 0) {
                vb.recyclerCategory.setVisibility(View.VISIBLE);
                AdapterCategory adapterCategory = new AdapterCategory();
                adapterCategory.setHomeItemClickListener(homeItemClickListener);
                adapterCategory.addData(categoryList);
                vb.recyclerCategory.setLayoutManager(new LinearLayoutManager(MusicApplication.myApplication, RecyclerView.HORIZONTAL, false));
                vb.recyclerCategory.setAdapter(adapterCategory);
            } else {
                vb.recyclerCategory.setVisibility(View.GONE);
            }

            if(position == data.size() - 1){
                vb.place.setVisibility(View.VISIBLE);
            }else {
                vb.place.setVisibility(View.GONE);
            }
        } else {
            VHolder<ItemFooterLoadingBinding> footerHolder = (VHolder<ItemFooterLoadingBinding>) holder;
            FrameLayout root = footerHolder.getVb().getRoot();
            if (isLoadingAdded) {
                root.setVisibility(View.VISIBLE);
            } else {
                root.setVisibility(View.GONE);
            }

        }

    }


}
