package com.pesets.idle;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.pesets.game.GameScene;
import com.pesets.game.Player;
import com.pesets.idle.city.CityScene;
import com.pesets.idle.scenes.Scene;
import com.pesets.midi.DesktopMidiPlayer;
import com.pesets.midi.MidiPlayer;
import com.pesets.resources.ResourceManager;

public class IdleGame extends Game implements ISceneSwitcher{
	private Scene gameScene, cityScene;
	private Player player;
	private MidiPlayer midi = new DesktopMidiPlayer();
	
	@Override
	public void create() {
		ResourceManager.init();
		
		player = new Player();
		FileHandle file = Gdx.files.local("save");
		if (file.exists())
		{
			try {
				player.load(new DataInputStream(file.read()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
			
		gameScene = new GameScene(this);
		cityScene = new CityScene(this);
		
		setScreen(cityScene);
		
		midi.open("bgm.mid");
		midi.play();
	}

	@Override
	public void dispose() {
		FileHandle file = Gdx.files.local("save");
		try {
			player.save(new DataOutputStream(file.write(false)));
		} catch (IOException e) {
			e.printStackTrace();
		}
		gameScene.dispose();
	}

	@Override
	public void setActiveScene(int sceneId) {
		Scene scene = null;
		switch (sceneId)
		{
		case SceneGame:
			scene = gameScene;
			midi.stop();
			break;
		case SceneCity:
			scene = cityScene;
			if (ResourceManager.getInstance().isSound())
				midi.play();
			break;
		}
		
		setScreen(scene);
	}

	@Override
	public Player getPlayer() {
		return player;
	}

	@Override
	public GameScene getGameScene() {
		return (GameScene) gameScene;
	}

	@Override
	public void pause() {
		System.out.println("Paused");
		super.pause();
	}

	@Override
	public void resume() {
		System.out.println("Resumed");
		super.resume();
	}

	@Override
	public MidiPlayer getMidi() {
		return midi;
	}
	
	
}
