package com.kitobochi.softapp.task.noisetimber.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.kitobochi.softapp.task.noisetimber.ProApp
import com.kitobochi.softapp.task.noisetimber.databinding.FragmentHome2Binding
import com.kitobochi.softapp.task.noisetimber.tools.GridSpacingItemDecoration
import com.kitobochi.softapp.task.noisetimber.tools.HorizontalSpaceItemDecoration
import com.kitobochi.softapp.task.noisetimber.ui.adapter.RealHumanVoiceAdapter
import com.kitobochi.softapp.task.noisetimber.ui.adapter.SoundsOfAppliancesAdapter
import com.kitobochi.softapp.task.noisetimber.ui.adapter.SoundsOfNatureAdapter

class HomeFragment : Fragment() {
    private lateinit var binding: FragmentHome2Binding
    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 = 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()) {
            realHumanVoiceAdapter = RealHumanVoiceAdapter(requireActivity(), ProApp.realHumanVoiceList)
            binding.realRv.layoutManager =
                LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false)
            binding.realRv.addItemDecoration(HorizontalSpaceItemDecoration(requireActivity()))
            binding.realRv.adapter = realHumanVoiceAdapter
        }
        if (ProApp.soundsOfAppliancesList.isNotEmpty()) {
            soundsOfAppliancesAdapter =
                SoundsOfAppliancesAdapter(requireActivity(), ProApp.soundsOfAppliancesList)
            binding.soundsRv.layoutManager =
                LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false)
            binding.soundsRv.addItemDecoration(HorizontalSpaceItemDecoration(requireActivity()))
            binding.soundsRv.adapter = soundsOfAppliancesAdapter
        }
        if (ProApp.soundsOfNatureList.isNotEmpty()) {
            soundsOfNatureAdapter =
                SoundsOfNatureAdapter(requireActivity(), ProApp.soundsOfNatureList)
            binding.natureRv.layoutManager =
                GridLayoutManager(requireActivity(), 3, GridLayoutManager.HORIZONTAL, false)
            binding.natureRv.addItemDecoration(GridSpacingItemDecoration(requireActivity(), 10, 3))
            binding.natureRv.adapter = soundsOfNatureAdapter
        }
    }

    @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(){
        soundsOfAppliancesAdapter?.notifyDataSetChanged()
        realHumanVoiceAdapter?.notifyDataSetChanged()
        soundsOfNatureAdapter?.notifyDataSetChanged()
    }

}