package com.lazy.emulate.input

import androidx.compose.ui.geometry.Offset
import com.lazy.emulate.emulation.ConsoleType

data class ButtonPosition(
    val button: GamepadButton,
    val x: Float, // 0..1 normalized position
    val y: Float,
    val size: Float = 0.08f // normalized size relative to screen width
)

data class ControllerLayout(
    val consoleType: ConsoleType,
    val name: String,
    val buttons: List<ButtonPosition>,
    val dpadPosition: Offset = Offset(0.12f, 0.65f),
    val dpadSize: Float = 0.18f,
    val leftStickPosition: Offset? = null,
    val rightStickPosition: Offset? = null,
    val stickSize: Float = 0.14f,
    val opacity: Float = 0.6f
) {
    companion object {
        fun defaultForConsole(consoleType: ConsoleType): ControllerLayout {
            return when (consoleType) {
                ConsoleType.PS1 -> ps1Default()
                ConsoleType.NES -> nesDefault()
                ConsoleType.SNES -> snesDefault()
                ConsoleType.N64 -> n64Default()
                ConsoleType.GENESIS -> genesisDefault()
                ConsoleType.PS2 -> ps2Default()
            }
        }

        private fun ps1Default() = ControllerLayout(
            consoleType = ConsoleType.PS1,
            name = "Default",
            buttons = listOf(
                ButtonPosition(GamepadButton.FACE_BOTTOM, 0.85f, 0.72f),   // Cross
                ButtonPosition(GamepadButton.FACE_RIGHT, 0.92f, 0.62f),    // Circle
                ButtonPosition(GamepadButton.FACE_LEFT, 0.78f, 0.62f),     // Square
                ButtonPosition(GamepadButton.FACE_TOP, 0.85f, 0.52f),      // Triangle
                ButtonPosition(GamepadButton.L1, 0.08f, 0.42f, 0.10f),
                ButtonPosition(GamepadButton.R1, 0.85f, 0.42f, 0.10f),
                ButtonPosition(GamepadButton.L2, 0.08f, 0.35f, 0.10f),
                ButtonPosition(GamepadButton.R2, 0.85f, 0.35f, 0.10f),
                ButtonPosition(GamepadButton.START, 0.58f, 0.88f, 0.06f),
                ButtonPosition(GamepadButton.SELECT, 0.42f, 0.88f, 0.06f),
            ),
            dpadPosition = Offset(0.12f, 0.62f),
            leftStickPosition = Offset(0.25f, 0.80f),
            rightStickPosition = Offset(0.72f, 0.80f),
        )

        private fun nesDefault() = ControllerLayout(
            consoleType = ConsoleType.NES,
            name = "Default",
            buttons = listOf(
                ButtonPosition(GamepadButton.FACE_BOTTOM, 0.85f, 0.70f),   // B
                ButtonPosition(GamepadButton.FACE_RIGHT, 0.92f, 0.62f),    // A
                ButtonPosition(GamepadButton.START, 0.58f, 0.88f, 0.06f),
                ButtonPosition(GamepadButton.SELECT, 0.42f, 0.88f, 0.06f),
            ),
            dpadPosition = Offset(0.12f, 0.65f),
        )

        private fun snesDefault() = ControllerLayout(
            consoleType = ConsoleType.SNES,
            name = "Default",
            buttons = listOf(
                ButtonPosition(GamepadButton.FACE_BOTTOM, 0.85f, 0.72f),   // B
                ButtonPosition(GamepadButton.FACE_RIGHT, 0.92f, 0.62f),    // A
                ButtonPosition(GamepadButton.FACE_LEFT, 0.78f, 0.62f),     // Y
                ButtonPosition(GamepadButton.FACE_TOP, 0.85f, 0.52f),      // X
                ButtonPosition(GamepadButton.L1, 0.08f, 0.42f, 0.10f),
                ButtonPosition(GamepadButton.R1, 0.85f, 0.42f, 0.10f),
                ButtonPosition(GamepadButton.START, 0.58f, 0.88f, 0.06f),
                ButtonPosition(GamepadButton.SELECT, 0.42f, 0.88f, 0.06f),
            ),
            dpadPosition = Offset(0.12f, 0.65f),
        )

        private fun n64Default() = ControllerLayout(
            consoleType = ConsoleType.N64,
            name = "Default",
            buttons = listOf(
                ButtonPosition(GamepadButton.FACE_BOTTOM, 0.85f, 0.72f),   // A
                ButtonPosition(GamepadButton.FACE_RIGHT, 0.92f, 0.62f),    // B
                ButtonPosition(GamepadButton.L1, 0.08f, 0.42f, 0.10f),     // L
                ButtonPosition(GamepadButton.R1, 0.85f, 0.42f, 0.10f),     // R
                ButtonPosition(GamepadButton.L2, 0.25f, 0.50f, 0.10f),     // Z
                ButtonPosition(GamepadButton.START, 0.50f, 0.88f, 0.06f),
            ),
            dpadPosition = Offset(0.12f, 0.65f),
            leftStickPosition = Offset(0.25f, 0.75f),
        )

        private fun genesisDefault() = ControllerLayout(
            consoleType = ConsoleType.GENESIS,
            name = "Default",
            buttons = listOf(
                ButtonPosition(GamepadButton.FACE_BOTTOM, 0.78f, 0.72f),   // A
                ButtonPosition(GamepadButton.FACE_RIGHT, 0.86f, 0.65f),    // B
                ButtonPosition(GamepadButton.FACE_LEFT, 0.94f, 0.58f),     // C
                ButtonPosition(GamepadButton.START, 0.50f, 0.88f, 0.06f),
            ),
            dpadPosition = Offset(0.12f, 0.65f),
        )

        private fun ps2Default() = ps1Default().copy(consoleType = ConsoleType.PS2, name = "Default")
    }
}
