package app.furmeet.events.system

import android.content.Context
import android.os.Bundle
import android.os.PowerManager
import android.view.View
import app.furmeet.events.system.helper.*
import app.furmeet.events.bff.R
import app.furmeet.events.bff.databinding.ActivityCountdownBinding
//import kotlinx.android.synthetic.main.activity_countdown.*
//import org.jetbrains.anko.doAsync
import java.util.*


class CountdownActivity : TranslatableAppCompatActivityWithScheduleServiceWrapped() {

    lateinit var binding: ActivityCountdownBinding
    private var screenOn = true
    private lateinit var pm: PowerManager
    private val now get() = System.currentTimeMillis()
    private var objective = now
    private var finishesAt: Long? = null
    private lateinit var timer: Timer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCountdownBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        resourcesAliased = this.resources
        binding.batteryWarning.visibility = View.GONE
        pm = getSystemService(Context.POWER_SERVICE) as PowerManager
        binding.countdownTitle.text = intent.getStringExtra("label") ?: ""
        objective = intent.getLongExtra("counter", objective)
        finishesAt = intent.getLongExtra("finishesAt", -1).let { if (it < 0) null else it }
        screenOn = pm.isInteractive
        timer = Timer()
        updateTime()
        timerScreen()
    }

    private fun timerScreen() {
        timer.cancel()
        timer.purge()
        timer = Timer()
        doAsync {
            timer.scheduleAtFixedRate(
                    TimerTask {
                        uiThreadWithLogger {
                            updateTime()
                        }
                    },
                    1,
                    1000.toLong()
            )
        }
    }

    private fun updateTime() {
        val remaining = objective - now
        if (remaining >= 0) {
            binding.countdownData.text = countdownString(remaining)
            binding.countdownLabel.text = R.string.until_it_starts.getString()
            binding.countdownData.visibility = View.VISIBLE
            binding.countdownLabel.visibility = View.VISIBLE
        } else {
            val finishes = finishesAt?.let { it - now }
            if ((finishes != null) && (finishes >= 0)) {
                binding.countdownData.text = countdownString(finishes)
                binding.countdownLabel.text = R.string.until_it_ends.getString()
                binding.countdownData.visibility = View.VISIBLE
                binding.countdownLabel.visibility = View.VISIBLE
            } else {
                binding.countdownData.text = R.string.already_finished.getString()
                binding.countdownData.visibility = View.VISIBLE
                binding.countdownLabel.visibility = View.INVISIBLE
            }
        }
        if (pm.isInteractive != screenOn) {
            screenOn = pm.isInteractive
            timerScreen()
        }
    }

    private fun countdownString(remaining: Long): String {
        val (_, s, m, h, d) = countdownSplitter(remaining)
        return "%dd %02dh %02dm %02ds"// %03dms"
                .format(d, h, m, s)//, ms)
    }

    private fun countdownSplitter(remaining: Long) = listOf(
            remaining % 1000,
            (remaining / (1000)) % 60,
            (remaining / (60 * 1000)) % 60,
            (remaining / (3600 * 1000)) % 24,
            remaining / (24 * 3600 * 1000)
    )
}
