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

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import com.audio.record.screen.test.R
import com.audio.record.screen.test.base.BaseFragment
import com.audio.record.screen.test.databinding.FragmentRecordingBinding
import com.audio.record.screen.test.service.FloatingCallback
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
import kotlinx.coroutines.launch


class RecordingFragment : BaseFragment<FragmentRecordingBinding>(), FloatingCallback {

    private lateinit var viewModel: MainViewModel
    private lateinit var mFindNavController: NavController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

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


    companion object {
        @JvmStatic
        fun newInstance() =
            RecordingFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(requireActivity())[MainViewModel::class.java]
        initClick()
        viewModel.navController.observe(requireActivity()){
            mFindNavController = it
        }
        viewModel.serviceConnectStatus.observe(requireActivity()) {
            FloatingWindowBridge.registerCallback(this)
        }
        viewModel.ballStatus.observe(requireActivity()) {
            binding.imBall.isSelected = it

        }
        viewModel.screenshotStatus.observe(requireActivity()) {
            binding.imScreenshot.isSelected = it
        }
        viewModel.webcamStatus.observe(requireActivity()) {
            binding.imWebcam.isSelected = it
        }
        viewModel.recordingStatus.observe(requireActivity()){
            when(it){
                ConstValue.status_pause->{
                    Common.showLog(" recordingStatus  status_pause")
                    binding.imPauseResume.isSelected = true
                }
            }
        }
    }

    private fun initClick() {
        binding.run {
            imWebcam.setOnClickListener {
                it.isSelected = !it.isSelected
                viewModel.updateWebcamStatus(it.isSelected)
            }
            imScreenshot.setOnClickListener {
                it.isSelected = !it.isSelected
                viewModel.updateScreenshotStatus(it.isSelected)
            }
            imBall.setOnClickListener {
                it.isSelected = !it.isSelected
                viewModel.updateBallStatus(it.isSelected)
            }

            imPauseResume.setOnClickListener {
                it.isSelected = !it.isSelected
                if (it.isSelected) {
                    FloatingWindowBridge.sendCommand(FloatingWindowBridge.COMMEND_pause_record)
                } else {
                    FloatingWindowBridge.sendCommand(FloatingWindowBridge.COMMEND_resume_record)
                }
            }
            imStop.setOnClickListener {
                FloatingWindowBridge.sendCommand(FloatingWindowBridge.COMMEND_stop_record)
            }
        }
    }

    override fun onUpdateRecordTime(time: String) {
        super.onUpdateRecordTime(time)
        binding.tvTimer.text = time
    }

    override fun onPauseRecord() {
        super.onPauseRecord()
        binding.imPauseResume.isSelected = true
    }

    override fun onResumeRecord() {
        super.onResumeRecord()
        binding.imPauseResume.isSelected = false
    }

    override fun onStopRecord() {
        super.onStopRecord()
        if (lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
            Common.showLog(" RecordingFragment  回调  onStopRecord")
            mFindNavController.navigateUp()
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        FloatingWindowBridge.unregisterCallback(this)
    }
}