package com.lazy.emulate.emulation

import android.content.Context
import com.lazy.emulate.emulation.cores.GenesisCore
import com.lazy.emulate.emulation.cores.NesCore
import com.lazy.emulate.emulation.cores.PlaceholderCore
import com.lazy.emulate.emulation.cores.Ps1Core
import com.lazy.emulate.emulation.cores.SnesCore
import com.lazy.emulate.input.ButtonMappingManager

object EmulationEngine {

    private var activeCore: EmulatorCore? = null
    var buttonMappingManager: ButtonMappingManager? = null

    fun getCoreForConsole(context: Context, consoleType: ConsoleType): EmulatorCore {
        return when (consoleType) {
            ConsoleType.PS1 -> Ps1Core(context, buttonMappingManager)
            ConsoleType.NES -> NesCore(context, buttonMappingManager)
            ConsoleType.SNES -> SnesCore(context, buttonMappingManager)
            ConsoleType.N64 -> PlaceholderCore(ConsoleType.N64)
            ConsoleType.GENESIS -> GenesisCore(context, buttonMappingManager)
            ConsoleType.PS2 -> PlaceholderCore(ConsoleType.PS2)
        }
    }

    fun startGame(context: Context, consoleType: ConsoleType, romPath: String): EmulatorCore? {
        stopActiveGame()
        val core = getCoreForConsole(context, consoleType)
        if (!core.loadRom(romPath)) return null
        activeCore = core
        return core
    }

    fun getActiveCore(): EmulatorCore? = activeCore

    fun stopActiveGame() {
        activeCore?.stop()
        activeCore = null
    }
}
