package com.audio.record.screen.test.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.audio.record.screen.test.R
import com.audio.record.screen.test.base.BaseFragment
import com.audio.record.screen.test.databinding.FragmentRecordBinding
import com.audio.record.screen.test.service.FloatingWindowBridge
import com.audio.record.screen.test.tool.Common
import com.audio.record.screen.test.tool.ConstValue
import com.audio.record.screen.test.viewmodel.MainViewModel

class RecordMainFragment : BaseFragment<FragmentRecordBinding>() {
    companion object {
        @JvmStatic
        fun newInstance() =
            RecordMainFragment().apply {

            }
    }

    private lateinit var viewModel: MainViewModel
    private lateinit var navController: NavController

    override fun initBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentRecordBinding =
        FragmentRecordBinding.inflate(inflater, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(requireActivity())[MainViewModel::class.java]
        initNavigation()
        viewModel.recordingStatus.observe(requireActivity()) { it ->
            when (it) {
                ConstValue.status_recording -> {
                    Common.showLog(" recordingStatus  status_recording")
                    navController.currentDestination?.let { navdestination ->
                        if (navdestination.id == R.id.record_normal_fragment) {
                            Common.showLog(" recordingStatus  status_recording  action_to_recording")
                            navController.navigate(R.id.action_to_recording)
                        }
                    }

                }

                ConstValue.status_complete -> {
                    Common.showLog(" recordingStatus  status_complete")
                    navController.currentDestination?.let { navdestination ->
                        if (navdestination.id == R.id.recording_fragment) {
                            Common.showLog(" recordingStatus  status_complete  navigateUp")
                            navController.navigateUp()
                        }
                    }
                }
            }
        }
    }


    private fun initNavigation() {
        val navHostFragment = childFragmentManager.findFragmentById(R.id.nav_host_view) as NavHostFragment
        navController = navHostFragment.navController
        viewModel.updateNavController(navController)
        navController.addOnDestinationChangedListener { controller, destination, arguments ->
            when (destination.id) {

            }

        }
    }


}