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

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import com.audio.record.screen.test.App
import com.audio.record.screen.test.base.BaseFragment
import com.audio.record.screen.test.databinding.FragmentVolumeBinding
import com.audio.record.screen.test.tool.Common
import com.audio.record.screen.test.tool.FFmpegKitTool
import com.audio.record.screen.test.viewmodel.PreviewViewModel
import java.io.File

class VolumeFragment : BaseFragment<FragmentVolumeBinding>(), View.OnClickListener {


    private lateinit var viewModel: PreviewViewModel
    private lateinit var navController: NavController
    private lateinit var copyFilePath: String
    companion object {

        @JvmStatic
        fun newInstance() =
            VolumeFragment().apply {
                arguments = Bundle().apply {
//                    putString(ARG_PARAM1, param1)
//                    putString(ARG_PARAM2, param2)
                }
            }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
//            param1 = it.getString(ARG_PARAM1)
//            param2 = it.getString(ARG_PARAM2)
        }
    }


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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(requireActivity())[PreviewViewModel::class.java]
        navController = findNavController()

        initClick()
        viewModel.copySuccess.observe(requireActivity()){
            if (it.first) {
                Common.showLog("-------11111111111111")
                copyFilePath = it.second

            }
        }
    }


    private fun initClick() {
        binding.imClose.setOnClickListener(this)
        binding.imSave.setOnClickListener(this)
        binding.seekbar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                binding.progressText.text = "${progress}%"
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {

            }

        })
    }


    override fun onClick(p0: View?) {
        p0?.let {
            if (it == binding.imSave) {
                val volumeFile = File(App.instanceApp.cacheDir, "volume_video_${System.currentTimeMillis()}.mp4")
                val volume = (binding.seekbar.progress.coerceIn(0, 200)) / 100.0f
                FFmpegKitTool.setVideVolume(copyFilePath,volumeFile.absolutePath,volume)

            } else if (it == binding.imClose) {
                navController.navigateUp()
            }
        }
    }

}