import Game from '../engine';
import Rooms from './rooms';
import Items from './items';
import MeowCommand from './commands/meow';
import DarkCommand from "./commands/dark";
import MobileCommands from '../engine/mobile-commands';

const states = {
    BEFORE_PLAY: "before-play",
    SAVE_GAME_FOUND: "save-game-found",
    PLAY_AREA: "play-area"
};

function setState(newState) {
    const allStates = Object.values(states);

    allStates.forEach(state => {
        document.getElementById(state).hidden = state !== newState;
    });
}

function addButtonEventListener(buttonId, onClick) {
    document.getElementById(buttonId).addEventListener("click", onClick);
}

if (localStorage.getItem("save")) {
    setState(states.SAVE_GAME_FOUND);

    addButtonEventListener("load-save-game", () => {
        setState(states.PLAY_AREA);
        startGame(false);
    });

    addButtonEventListener("start-new-game", () => {
        setState(states.PLAY_AREA);
        startGame(true);
    });
} else {
    setState(states.BEFORE_PLAY);
    
    addButtonEventListener("begin", () => {
        setState(states.PLAY_AREA);
        startGame(true);
    });
}

function startGame(newGame) {
    const game = new Game(newGame);

    game.init({
        rooms: Rooms,
        commands: [
            [["meow", "mew"], MeowCommand],
            [["dark"], DarkCommand]
        ],
        items: Items
    });
    
    // Initialize mobile commands interface
    if (document.getElementById('mobile-command-mode')) {
        game.mobileCommands = new MobileCommands(game.commandHandler, game);
        game.commandHandler.setMobileCommands(game.mobileCommands);
        
        // Auto-enable mobile mode on touch devices
        if ('ontouchstart' in window || navigator.maxTouchPoints > 0) {
            game.mobileCommands.toggleInputMode();
        }
    }
}
