package com.soundapp.soundgags.ui

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.SystemClock
import androidx.appcompat.app.AppCompatActivity
import com.ad.tradpluslibrary.TPAdManager.showTPAD
import com.soundapp.soundgags.R
import com.soundapp.soundgags.databinding.ActivityLocalMusicBinding
import com.soundapp.soundgags.setting.MediaPlayerManager
import com.soundapp.soundgags.setting.TopBarUtils

class LocalMusicActivity : AppCompatActivity() {
    private var binding: ActivityLocalMusicBinding? = null
    private var startTime: Long = 0
    private var elapsedTime: Long = 0
    private val handler = Handler()
    private var runnable: Runnable? = null
    private var isCreate = false

    private var record: MediaPlayerManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        TopBarUtils.setStatusBar(this.window)
        TopBarUtils.setLightStatusBar(this.window, true)
        binding = ActivityLocalMusicBinding.inflate(layoutInflater)
        setContentView(binding!!.root)
        record = MediaPlayerManager(this)
        binding!!.audioTime.setText(R.string.time_init)
        showTPAD(this){}
        initClick()
    }

    private fun initClick() {
        binding!!.back.setOnClickListener { finish() }
        binding!!.imMic.setOnClickListener {
            binding!!.imMic.isSelected = !binding!!.imMic.isSelected
            binding!!.audioTint.text=getString(R.string.click_to_finish)
            initReflection()
        }
    }

    private fun initReflection() {
        if (!isCreate) {
            record?.startRecording()
            isCreate = true
            runnable = object : Runnable {
                override fun run() {
                    val currentTime = SystemClock.elapsedRealtime()
                    elapsedTime = currentTime - startTime
                    val milliseconds = (elapsedTime % 1000).toInt() / 10
                    val seconds = (elapsedTime / 1000).toInt() % 60
                    val minutes = (elapsedTime / (1000 * 60)).toInt() % 60
                    binding!!.audioTime.text =
                        String.format("%02d : %02d : %02d", minutes, seconds, milliseconds)
                    handler.postDelayed(this, 10)
                }
            }
            startTime = SystemClock.elapsedRealtime()
            handler.post(runnable as Runnable)
        } else {
            record?.stopRecording()

            handler.removeCallbacks(runnable!!)
            val intent = Intent(
                this@LocalMusicActivity,
                VoiceRecorderActivity::class.java
            )
            intent.putExtra(VoiceRecorderActivity.KEY_Time, elapsedTime)
            intent.putExtra(VoiceRecorderActivity.KEY_Path, record?.getTempFileName())
            intent.putExtra(VoiceRecorderActivity.KEY_import, 1)
            startActivity(intent)
            finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if(runnable!=null){
            handler.removeCallbacks(runnable!!)
        }
    }
}