package com.offline.music.task.player.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.gyf.immersionbar.ktx.immersionBar
import com.offline.music.task.player.ProApp
import com.offline.music.task.player.databinding.FragmentHome2Binding
import com.offline.music.task.player.tools.util.GridSpacingItemDecoration
import com.offline.music.task.player.tools.util.HorizontalSpaceItemDecoration
import com.offline.music.task.player.ui.adapter.HomeRecommendAdapter
import com.offline.music.task.player.ui.adapter.HomeWhiteNoiseAdapter
import com.offline.music.task.player.ui.adapter.HomeVoiceOfNatureAdapter

class HomeFragment : Fragment() {
    private lateinit var binding: FragmentHome2Binding
    private var homeRecommendAdapter: HomeRecommendAdapter? = null
    private var homeWhiteNoiseAdapter: HomeWhiteNoiseAdapter? = null
    private var homeVoiceOfNatureAdapter: HomeVoiceOfNatureAdapter? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentHome2Binding.inflate(layoutInflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
    }

    private fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(false)
            statusBarView(binding.view)
        }
    }

    private fun initView() {
        if (ProApp.resourcesList.categories.isNotEmpty()) {
            binding.soundsName.text = ProApp.resourcesList.categories[1].name
            binding.natureName.text = ProApp.resourcesList.categories[2].name
        }
    }

    private fun initData(){
        if (ProApp.realHumanVoiceList.isNotEmpty()) {
            homeRecommendAdapter = HomeRecommendAdapter(requireActivity(), ProApp.realHumanVoiceList)
            binding.realRv.layoutManager =
                LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false)
            binding.realRv.addItemDecoration(HorizontalSpaceItemDecoration(requireActivity()))
            binding.realRv.adapter = homeRecommendAdapter
        }
        if (ProApp.soundsOfAppliancesList.isNotEmpty()) {
            homeWhiteNoiseAdapter =
                HomeWhiteNoiseAdapter(requireActivity(), ProApp.soundsOfAppliancesList)
            binding.soundsRv.layoutManager =
                LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false)
            binding.soundsRv.addItemDecoration(HorizontalSpaceItemDecoration(requireActivity()))
            binding.soundsRv.adapter = homeWhiteNoiseAdapter
        }
        if (ProApp.soundsOfNatureList.isNotEmpty()) {
            homeVoiceOfNatureAdapter =
                HomeVoiceOfNatureAdapter(requireActivity(), ProApp.soundsOfNatureList)
            binding.natureRv.layoutManager =
                GridLayoutManager(requireActivity(), 3, GridLayoutManager.HORIZONTAL, false)
            binding.natureRv.addItemDecoration(GridSpacingItemDecoration(requireActivity(), 10, 3))
            binding.natureRv.adapter = homeVoiceOfNatureAdapter
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onResume() {
        super.onResume()
        if (ProApp.currentPlayingAudio != null) {
            if (ProApp.realHumanVoiceList.isNotEmpty()) {
                for ((index, audio) in ProApp.realHumanVoiceList.withIndex()) {
                    if (audio.file == ProApp.currentPlayingAudio?.file) {
                        notifyDataSetChanged()
                        break
                    }
                }
            }
            if (ProApp.soundsOfAppliancesList.isNotEmpty()) {
                for ((index, audio) in ProApp.soundsOfAppliancesList.withIndex()) {
                    if (audio.file == ProApp.currentPlayingAudio?.file) {
                        notifyDataSetChanged()
                        break
                    }
                }
            }
            if (ProApp.soundsOfNatureList.isNotEmpty()) {
                for ((index, audio) in ProApp.soundsOfNatureList.withIndex()) {
                    if (audio.file == ProApp.currentPlayingAudio?.file) {
                        notifyDataSetChanged()
                        break
                    }
                }
            }
        }
        initImmersionBar()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if(!hidden){
            initImmersionBar()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun notifyDataSetChanged(){
        homeWhiteNoiseAdapter?.notifyDataSetChanged()
        homeRecommendAdapter?.notifyDataSetChanged()
        homeVoiceOfNatureAdapter?.notifyDataSetChanged()
    }

}