package com.develop.setting.init.ui

import android.media.AudioManager
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.SeekBar
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.VolumeUtils
import com.develop.food.base.ui.BaseActivity
import com.develop.food.base.router.Screens
import com.develop.setting.R
import com.develop.setting.databinding.ActivitySoundBinding

@Route(path = Screens.Setting.SOUND)
class SoundActivity : BaseActivity<ActivitySoundBinding>() {
    override fun createViewBinding(inflater: LayoutInflater): ActivitySoundBinding {
        return ActivitySoundBinding.inflate(layoutInflater)
    }

    private var currentVolume = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.topBar.setTopBarTitle(getString(R.string.Sound))
        binding.topBar.listenBackClick {
            finish()
        }

        currentVolume = VolumeUtils.getVolume(AudioManager.STREAM_MUSIC)
        binding.sbVoice.max = VolumeUtils.getMaxVolume(AudioManager.STREAM_MUSIC)
        binding.sbVoice.progress = currentVolume
        binding.switchView.isChecked = currentVolume != 0
        binding.switchView.setOnCheckedChangeListener { compoundButton, b ->
            if (b) {
                VolumeUtils.setVolume(
                    AudioManager.STREAM_MUSIC,
                    currentVolume,
                    AudioManager.FLAG_PLAY_SOUND
                )
                binding.sbVoice.progress = currentVolume
            } else {
                VolumeUtils.setVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND)
                binding.sbVoice.progress = 0

            }
        }
        binding.sbVoice.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                currentVolume = p1
                binding.switchView.isChecked = currentVolume != 0
                VolumeUtils.setVolume(
                    AudioManager.STREAM_MUSIC,
                    currentVolume,
                    AudioManager.FLAG_PLAY_SOUND
                )
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }

        })
    }
}