package partisan.weforge.xyz.pulse

import android.app.role.RoleManager
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import nl.dionsegijn.konfetti.core.Party
import nl.dionsegijn.konfetti.core.Position
import nl.dionsegijn.konfetti.core.emitter.Emitter
import nl.dionsegijn.konfetti.xml.KonfettiView
import partisan.weforge.xyz.pulse.databinding.ActivityWelcomeBinding
import java.util.concurrent.TimeUnit
import partisan.weforge.xyz.pulse.hasGeneralPermissions
import partisan.weforge.xyz.pulse.hasDrawOverlays
import partisan.weforge.xyz.pulse.hasCallRedirectionRole
import partisan.weforge.xyz.pulse.REQUIRED_PERMISSIONS

class WelcomeActivity : AppCompatActivity() {
    private lateinit var binding: ActivityWelcomeBinding
    private lateinit var prefs: Preferences
    private var roleManager: RoleManager? = null

    private val requestPermissionsLauncher = registerForActivityResult(
        ActivityResultContracts.RequestMultiplePermissions()
    ) {}

    private val requestOverlayLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) {}

    private val requestRoleLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) {}

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (hasGeneralPermissions(this) && hasDrawOverlays(this) && hasCallRedirectionRole(this)) {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
            return
        }

        binding = ActivityWelcomeBinding.inflate(layoutInflater)
        setContentView(binding.root)

        prefs = Preferences(this)
        roleManager = getSystemService(RoleManager::class.java)

        binding.activateButton.setOnClickListener {
            when {
                !hasGeneralPermissions(this) -> {
                    requestPermissionsLauncher.launch(REQUIRED_PERMISSIONS)
                }
                !hasDrawOverlays(this) -> {
                    requestOverlayLauncher.launch(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION))
                }
                !hasCallRedirectionRole(this) -> {
                    requestRoleLauncher.launch(roleManager?.createRequestRoleIntent(RoleManager.ROLE_CALL_REDIRECTION))
                }
                else -> {
                    showConfettiAndContinue()
                }
            }
        }
    }

    private fun showConfettiAndContinue() {
        binding.appIcon.post {
            val iconLocation = IntArray(2)
            binding.appIcon.getLocationOnScreen(iconLocation)

            val iconCenterX = iconLocation[0] + binding.appIcon.width / 2f
            val iconCenterY = iconLocation[1] + binding.appIcon.height / 2f

            val rootWidth = binding.root.width.toFloat()
            val rootHeight = binding.root.height.toFloat()

            val relativeX = (iconCenterX / rootWidth).toDouble()
            val relativeY = (iconCenterY / rootHeight).toDouble()

            binding.konfettiView.visibility = View.VISIBLE
            binding.konfettiView.start(
                Party(
                    speed = 25f,
                    maxSpeed = 50f,
                    damping = 0.9f,
                    spread = 360,
                    colors = listOf(0xfce18a, 0xff726d, 0xf4306d, 0xb48def),
                    position = Position.Relative(relativeX, relativeY),
                    emitter = Emitter(duration = 1, TimeUnit.SECONDS).perSecond(80)
                )
            )

            binding.konfettiView.postDelayed({
                startActivity(Intent(this, MainActivity::class.java))
                finish()
            }, 1500)
        }
    }
}
