package com.hi.music.player.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.hi.music.player.MusicApplication;
import com.hi.music.player.databinding.ItemFooterLoadingBinding;
import com.hi.music.player.databinding.ItemHomeBinding;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.javabean.response.child.ResponseCategory;
import com.hi.music.player.javabean.response.child.ResponseHomeChild;
import com.hi.music.player.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();

            if (singleList != null && singleList.size() > 0) {
                vb.recyclerSinger.setVisibility(View.VISIBLE);
                CommonUtils.LogMsg("-----------singleList-=" + singleList.size());
                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);
            }
            List<ResponseCategory> categoryList = responseHomeChild.getCategoryList();
            if (categoryList != null && categoryList.size() > 0) {
                vb.recyclerCategory.setVisibility(View.VISIBLE);
                CommonUtils.LogMsg("-----------categoryList-=" + categoryList.size());
                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);
            }
        } else {
            VHolder<ItemFooterLoadingBinding> footerHolder = (VHolder<ItemFooterLoadingBinding>) holder;
            FrameLayout root = footerHolder.getVb().getRoot();
            if (isLoadingAdded) {
                root.setVisibility(View.VISIBLE);
            } else {
                root.setVisibility(View.GONE);
            }

        }

    }


}
