/*
 * Copyright (C) 2013 Darth Affe <http://wyrez.org> and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.wyrez.shootingstars;

import com.jme3.app.Application;
import com.jme3.app.DebugKeysAppState;
import com.jme3.app.SimpleApplication;
import com.jme3.app.StatsAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.renderer.Camera;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import org.wyrez.persij.PersiJContainer;
import org.wyrez.shootingstars.data.AudioDataManager;
import org.wyrez.shootingstars.data.YTDownloader;
import org.wyrez.shootingstars.factories.Materials;
import org.wyrez.shootingstars.game.GameSettings;
import org.wyrez.shootingstars.states.State;
import org.wyrez.shootingstars.states.StateManager;
import org.wyrez.shootingstars.states.util.OptionSettings;
import tonegod.gui.core.Screen;

/**
 *
 * @author Darth Affe
 */
public class ShootingStars extends SimpleApplication {

    private PersiJContainer container;
    private StateManager sManager;
    private OptionSettings optionSettings;

    public ShootingStars(OptionSettings optionSettings) {
        super(new StatsAppState(), new DebugKeysAppState());
        this.optionSettings = optionSettings;
    }

    @Override
    public void simpleInitApp() {
        System.setProperty("jna.library.path", "lib/vlc");
        registerTypes();
        initGUI();
        initKeys();

        Materials.initialize(assetManager);
        cam.setFrustumFar(2500f);
        sManager = container.resolve(StateManager.class);
        sManager.setState(State.START);
    }

    private void initGUI() {
        guiNode.addControl(container.resolve(Screen.class));
    }

    private void registerTypes() {
        container = new PersiJContainer();
        container.registerSingleton(ShootingStars.class, this);
        container.registerSingleton(Application.class, this);
        container.registerSingleton(Node.class, rootNode);
        container.registerSingleton(Camera.class, cam);
        container.registerSingleton(AppStateManager.class, stateManager);
        container.registerSingleton(InputManager.class, inputManager);
        container.registerSingleton(AssetManager.class, assetManager);
        container.registerSingleton(ViewPort.class, viewPort);
        container.registerSingleton(OptionSettings.class, optionSettings);
        container.registerType(GameSettings.class, true);
        container.registerType(StateManager.class, true);
        container.registerType(Screen.class, true);
        container.registerType(AudioDataManager.class, true);
        container.registerType(YTDownloader.class);
    }

    private void initKeys() {
        inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
        inputManager.deleteMapping(INPUT_MAPPING_EXIT);
        inputManager.addMapping("ESC", new KeyTrigger(KeyInput.KEY_ESCAPE));
    }

    public static void main(String[] args) {
        OptionSettings optionSettings = new OptionSettings();
        optionSettings.init();
        ShootingStars app = new ShootingStars(optionSettings);
        app.setShowSettings(false);
        app.setSettings(optionSettings.getAppSettings());
        app.start();
    }
}
