package melody.offline.music.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 melody.offline.music.adapter.RealHumanVoiceAdapter
import melody.offline.music.adapter.SoundsOfAppliancesAdapter
import melody.offline.music.adapter.SoundsOfNatureAdapter
import melody.offline.music.databinding.FragmentHomeBinding
import melody.offline.music.util.AnalysisUtil
import melody.offline.music.util.GridSpacingItemDecoration

class HomeFragment : Fragment() {
    private lateinit var binding: FragmentHomeBinding
    private var realHumanVoiceAdapter: RealHumanVoiceAdapter? = null
    private var soundsOfAppliancesAdapter: SoundsOfAppliancesAdapter? = null
    private var soundsOfNatureAdapter: SoundsOfNatureAdapter? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentHomeBinding.inflate(layoutInflater)
        return binding.root
    }

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

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

    private fun initView() {
        if (melody.offline.music.App.resourcesList.categories.isNotEmpty()) {
            binding.soundsName.text = melody.offline.music.App.resourcesList.categories[1].name
            binding.natureName.text = melody.offline.music.App.resourcesList.categories[2].name
        }
        if (melody.offline.music.App.realHumanVoiceList.isNotEmpty()) {
            realHumanVoiceAdapter = RealHumanVoiceAdapter(requireActivity(), melody.offline.music.App.realHumanVoiceList)
            binding.realRv.layoutManager =
                GridLayoutManager(requireActivity(), 2, GridLayoutManager.VERTICAL, false)
            binding.realRv.addItemDecoration(GridSpacingItemDecoration(requireActivity(), 20, 2))
            binding.realRv.adapter = realHumanVoiceAdapter
        }
        if (melody.offline.music.App.soundsOfAppliancesList.isNotEmpty()) {
            soundsOfAppliancesAdapter =
                SoundsOfAppliancesAdapter(requireActivity(), melody.offline.music.App.soundsOfAppliancesList)
            binding.soundsRv.layoutManager =
                GridLayoutManager(requireActivity(), 2, GridLayoutManager.VERTICAL, false)
            binding.soundsRv.addItemDecoration(GridSpacingItemDecoration(requireActivity(), 20, 2))
            binding.soundsRv.adapter = soundsOfAppliancesAdapter
        }
        if (melody.offline.music.App.soundsOfNatureList.isNotEmpty()) {
            soundsOfNatureAdapter =
                SoundsOfNatureAdapter(requireActivity(), melody.offline.music.App.soundsOfNatureList)
            binding.natureRv.layoutManager =
                LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)
            binding.natureRv.adapter = soundsOfNatureAdapter
        }
    }

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

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

    override fun onDestroy() {
        super.onDestroy()
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun notifyDataSetChanged() {
        soundsOfAppliancesAdapter?.notifyDataSetChanged()
        realHumanVoiceAdapter?.notifyDataSetChanged()
        soundsOfNatureAdapter?.notifyDataSetChanged()
    }

}