package com.offline.music.playermp3.ui.fragmnt;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.offline.music.playermp3.R;
import com.offline.music.playermp3.adapter.AdapterHome;
import com.offline.music.playermp3.api.HomeItemClickListener;
import com.offline.music.playermp3.databinding.FragmentHomeBinding;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.helper.MyValue;
import com.offline.music.playermp3.javabean.response.ResponseHome;
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 com.offline.music.playermp3.ui.activity.CategoryListActivity;
import com.offline.music.playermp3.ui.activity.PlayActivity;
import com.offline.music.playermp3.ui.fragmnt.viewmodel.VMHome;

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

public class HomeFragment extends BaseFragment<FragmentHomeBinding> implements HomeItemClickListener {


    private VMHome vmHome;


    private int requestCount = 1;

    private int totalPage = 3;


    private List<ResponseHomeChild> childList = new ArrayList<>();


    @Override
    protected FragmentHomeBinding getFragmentVb() {
        // 返回正确的 ViewBinding 实例
        return FragmentHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {

//        //标题导航栏颜色设置
//        Vb.toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        AdapterHome adapterHome = new AdapterHome();
        adapterHome.setHomeItemClickListener(this);


        vmHome = getFragmentScopeViewModel(VMHome.class);
        adapterHome.addLoadingFooter();
        vmHome.getHome();
        Vb.tvRetry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapterHome.addLoadingFooter();
                vmHome.getHome();
            }
        });

        vmHome.data.observe(getViewLifecycleOwner(), new Observer<ResponseHome>() {
            @Override
            public void onChanged(ResponseHome responseHome) {
                if (responseHome == null) {
                    adapterHome.removeLoadingFooter();
                    requestCount--;
                    if(requestCount == 0){
                        Vb.layoutError.setVisibility(View.VISIBLE);
                        Vb.recyclerSongOfTheDay.setVisibility(View.GONE);
                    }
                    return;
                }
                Vb.layoutError.setVisibility(View.GONE);
                Vb.recyclerSongOfTheDay.setVisibility(View.VISIBLE);

                List<ResponseHomeChild> childList1 = responseHome.getChildList();
                if (childList1 == null) return;
                childList.addAll(childList1);
                adapterHome.removeLoadingFooter();
                adapterHome.addData(childList1);
                if (requestCount == 1) {
                    adapterHome.addLoadingFooter();
                    vmHome.getHomeMore();
                    requestCount++;
                }


            }
        });
        Vb.recyclerSongOfTheDay.setLayoutManager(new LinearLayoutManager(requireContext()));
        Vb.recyclerSongOfTheDay.setAdapter(adapterHome);
        Vb.recyclerSongOfTheDay.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (layoutManager != null && layoutManager.findLastVisibleItemPosition() == childList.size() - 1) {
                    if (requestCount < totalPage && !adapterHome.isLoadingAdded()) {
                        CommonUtils.LogMsg("------loadmore--");
                        adapterHome.addLoadingFooter();
                        vmHome.getHomeMore();
                        requestCount++;
                    }

                }
            }
        });

    }


    @Override
    public void onClickItemSinger(ResponseSingle data) {
        FragmentActivity activity = getActivity();
        if (activity != null) {
            Intent intent = new Intent(activity, PlayActivity.class);
            intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_SINGER, data);
            startActivity(intent);
        }
    }

    @Override
    public void onClickItemCategory(ResponseCategory data) {
        FragmentActivity activity = getActivity();
        if (activity != null) {
            String pageType = data.getPageType();
            String browseId = data.getBrowseId();
            String twoSubtitle = data.getTwoSubtitle();
            if (pageType.equals(MyValue.PAGE_TYPE_MV)||pageType.equals(MyValue.PAGE_TYPE_MV_LIST)) {
                Intent intent = new Intent(activity, PlayActivity.class);
                intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_MV, data);
                intent.putExtra(MyValue.KEY_ENTER_SOURCE, MyValue.TYPE_ENTER_SOURCE_MV);
                startActivity(intent);
            }
            else {
                Intent intent = new Intent(activity, CategoryListActivity.class);
                intent.putExtra(MyValue.KEY_CATEGORY_LIST_TYPE, pageType);
                intent.putExtra(MyValue.KEY_CATEGORY_LIST_SINGER_NAME, twoSubtitle);
                intent.putExtra(MyValue.KEY_CATEGORY_LIST_BROWSER_ID, browseId);
                startActivity(intent);
            }


        }
    }
}
