package ei.game.gamestate;

import java.util.LinkedList;
import java.util.Queue;

import ei.engine.effects.BitmapProgressBar;
import ei.engine.scene.Sprite;
import ei.engine.sound.SoundLoader;
import ei.engine.state.GameState;
import ei.engine.state.GameStateManager;
import ei.engine.texture.TextureLoader;

/**
 * This class handels the loading of the
 * images and sounds to the memmory.
 * 
 * @author Ziver
 */
public class LoadingState extends GameState{
	//The files to load
	private static final String[] TEXTURES = {
		"data/font.bmp",
		"data/particle.bmp",
		"data/splash.png",
		"data/buildings/cc/cc_building.png",
		"data/buildings/cc/cc_ground.png",
		"data/buildings/factory/factory_1.png",
		"data/buildings/factory/factory_2.png",
		"data/buildings/prison/prison.png",
		"data/hud/buildmenu.png",
		"data/hud/money.png",
		"data/map/asfalt_horizontal.png",
		"data/map/asfalt_vertical.png",
		"data/map/asfalt.png",
		"data/map/grass.png",
		"data/map/redmud.png",
		"data/map/sand.png",
		"data/map/parking/parking_01.png",
		"data/map/parking/parking_04.png",
		"data/map/parking/parking_07.png",
		"data/map/parking/parking_02.png",
		"data/map/parking/parking_05.png",
		"data/map/parking/parking_08.png",
		"data/map/parking/parking_03.png",
		"data/map/parking/parking_06.png",
		"data/map/parking/parking_09.png",
		"data/map/redstone/redstone0000.png",
		"data/map/redstone/redstone0001.png",
		"data/map/redstone/redstone0002.png",
		"data/map/redstone/redstone0003.png",
		"data/map/stone/stone0000.png",
		"data/map/stone/stone0001.png",
		"data/map/stone/stone0002.png",
		"data/map/stone/stone0003.png",
		"data/ui/apu_button_selected.png",
		"data/ui/apu_button.png",
		"data/ui/bomber_button_selected.png",
		"data/ui/bomber_button.png",
		"data/ui/tank_button_selected.png",
		"data/ui/tank_button.png",
		"data/units/apu/apu0000.png",
		"data/units/bomber/bomber0000.png",
		"data/units/bomber/bomber0001.png",
		"data/units/bomber/bomber0002.png",
		"data/units/bomber/bomber0003.png",
		"data/units/tank/tank0000.png"	
	};
	private static final String[] SOUNDS = {
		"data/sounds/APUattack.wav",
		"data/sounds/APUmove1.wav",
		"data/sounds/APUmove2.wav",
		"data/sounds/APUselect.wav",
		"data/sounds/APUyes.wav",
		"data/sounds/BOMBERattack.wav",
		"data/sounds/BOMBERmove1.wav",
		"data/sounds/BOMBERmove2.wav",
		"data/sounds/BOMBERselect.wav",
		"data/sounds/bomberweapon.wav",
		"data/sounds/ei.ogg",
		"data/sounds/machinegun.wav",
		"data/sounds/TANKattack.wav",
		"data/sounds/TANKmove1.wav",
		"data/sounds/TANKmove2.wav",
		"data/sounds/TANKselect.wav",
		"data/sounds/tankweapon.wav"
	};
	// The things to load
	private Queue<String> loadTextures;
	private Queue<String> loadSounds;
	//  The name of the next state to activate after loading
	private String nextState;
	private BitmapProgressBar progress;

	
	/**
	 * Creates a loadingstate
	 * @param name The name of the state
	 */
	public LoadingState(String name,String nextState) {
		super(name);
		this.nextState = nextState;
	}	

	@Override
	public void init() {
		loadTextures = new LinkedList<String>();
		loadSounds = new LinkedList<String>();
		
		progress = new BitmapProgressBar("Loading");
		progress.centerToScreen();
		progress.setBarTexture(new Sprite("ProgressBar","data/loadbar_front.png"));
		progress.setProgressTexture(new Sprite("Progress","data/loadbar.png"));

		for(int i=0; i<TEXTURES.length ;i++){
			addTexture(TEXTURES[i]);
		}
		for(int i=0; i<SOUNDS.length ;i++){
			addSound(SOUNDS[i]);
		}
	}
	
	/**
	 * Add a texture to be loaded
	 * @param url
	 */
	public void addTexture(String url){
		loadTextures.add(url);
		progress.setMax(loadTextures.size()+loadSounds.size());
	}
	
	/**
	 * Add a sound to be loaded
	 * @param url
	 */
	public void addSound(String url){
		loadSounds.add(url);
		progress.setMax(loadTextures.size()+loadSounds.size());
	}
	
	/**
	 * Loads the things
	 */
	public void update() {
		if(!loadTextures.isEmpty()){
			TextureLoader.getTextureLoaderInstance().getTexture(loadTextures.poll());
			progress.setValue(progress.getValue()+1);
		}
		else if(!loadSounds.isEmpty()){
			SoundLoader.getInstnace().loadSound(loadSounds.poll());
			progress.setValue(progress.getValue()+1);
		}
		else{
			//progress.setValue(progress.getValue()+1);
			if(progress.getProcent() >= 100){
				//deactivate this state and activate the next one
				GameStateManager.getInstance().removeState(this);
				GameStateManager.getInstance().setActive(nextState);
			}			
		}
		progress.update();
	}
	
	/**
	 * Render the load bar
	 */
	public void render() {
		progress.render();
	}

}
