package com.design.mischiefgo.ui

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.SystemClock
import androidx.appcompat.app.AppCompatActivity
import com.design.mischiefgo.R
import com.design.mischiefgo.databinding.ActivityListenSoundBinding
import com.design.mischiefgo.ut.StartPlayUtil
import com.design.mischiefgo.ut.EvenTopUtils

class ListenSoundActivity : AppCompatActivity() {
    private var binding: ActivityListenSoundBinding? = 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: StartPlayUtil? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EvenTopUtils.setStatusBar(this.window)
        EvenTopUtils.setLightStatusBar(this.window, true)
        binding = ActivityListenSoundBinding.inflate(layoutInflater)
        setContentView(binding!!.root)
        record = StartPlayUtil(this)
        binding!!.audioTime.setText(R.string.time_init)

        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)
            initDuplicate()
        }
    }

    private fun initDuplicate() {
        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@ListenSoundActivity,
                SaveToLocalActivity::class.java
            )
            intent.putExtra(SaveToLocalActivity.KEY_Time, elapsedTime)
            intent.putExtra(SaveToLocalActivity.KEY_Path, record?.getTempFileName())
            intent.putExtra(SaveToLocalActivity.KEY_import, 1)
            startActivity(intent)
            finish()
        }
    }

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