package com.hi.music.player.ui.fragmnt;

import android.content.Intent;
import android.content.RestrictionsManager;

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.hi.music.player.adapter.AdapterHome;
import com.hi.music.player.api.HomeItemClickListener;
import com.hi.music.player.api.OnHasUrlAction;
import com.hi.music.player.api.RequestListener;
import com.hi.music.player.databinding.FragmentHomeBinding;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.helper.MyValue;
import com.hi.music.player.javabean.response.ResponseHome;
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 com.hi.music.player.network.RetrofitManager;
import com.hi.music.player.ui.activity.CategoryListActivity;
import com.hi.music.player.ui.activity.PlayActivity;
import com.hi.music.player.ui.fragmnt.viewmodel.VMHome;

import org.json.JSONObject;

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

import okhttp3.ResponseBody;

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();



        vmHome.data.observe(getViewLifecycleOwner(), new Observer<ResponseHome>() {
            @Override
            public void onChanged(ResponseHome responseHome) {
                if(responseHome == null){
                    adapterHome.removeLoadingFooter();
                    requestCount--;
                    return;
                }
                List<ResponseHomeChild> childList1 = responseHome.getChildList();
                if(childList1 == null) return;
                childList.addAll(childList1);
                adapterHome.removeLoadingFooter();
                adapterHome.addData(childList1);


            }
        });
        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();
            if(pageType.equals(MyValue.PAGE_TYPE_MV)){
                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  if(pageType.equals(MyValue.PAGE_TYPE_ALBUM)){
                Intent intent = new Intent(activity, CategoryListActivity.class);
                intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_CATEGORY, data);
                startActivity(intent);
            }else {
                Intent intent = new Intent(activity, CategoryListActivity.class);
                intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_CATEGORY, data);
                startActivity(intent);
            }


        }
    }
}
