module config.top;



private import gedo.video.ggl;
private import gedo.video.color;
private import gedo.video.texture;
private import gedo.audio.gal;
private import gedo.audio.music;
private import gedo.gui.label;
private import gedo.gui.table;
private import gedo.gui.camera;
private import gedo.gui.cursor;
private import gedo.gui.component;
private import gedo.file.config;
private import gedo.input.joystick;
private import gedo.system.scene;
private import gedo.system.gameloop;
private import config.share;
private import config.inputconfig;
private import config.videoconfig;



public class Top : Scene
{
public:
	this()
	{
		Joysticks().config(.config.share.config);

		Joysticks().keyboard.config(false);

		auto camera = new Camera(640.0, 480.0, 0.0, 0.0, 0.5);

		auto guide = new ImageLabel(Texture("$LIB/config/guide_top.png", false));

		guide.position[1] = -240.0 + guide.size[1];
		guide.color = GREEN;

		camera.add(guide);

		_table = new Table(1, 3);
		_table.position[] = 0.0;

		camera.add(_table);

		ImageLabel[3] menus;
		menus[0] = new ImageLabel(Texture("$LIB/config/configvideo.png", false));
		menus[1] = new ImageLabel(Texture("$LIB/config/configinput.png", false));
		menus[2] = new ImageLabel(Texture("$LIB/config/quit.png", false));

		foreach(i, menu; menus)
		{
			_table.set(0, i, menu);
		}

		_cursor = new Cursor();
		_cursor.joystick = Joysticks().keyboard;

		_cursor.color = YELLOW;

		_table.rows[0].cbClick = &onClickVideoConfig;
		_table.rows[1].cbClick = &onClickInputConfig;
		_table.rows[2].cbClick = &onClickQuit;

		GAL_setVolume(.config.share.config.get("volume.bgm", 0.75), .config.share.config.get("volume.se", 0.75));

		_bgm = new Music("$LIB/title/bgm.txt");

		_bgm.play();
	}


	override void start()
	{
		super.start();

		_cursor.focus = _table.rows[0];
	}


	override void update()
	{
		_cursor.focus.root.update();
		_cursor.update();
	}


	override void draw()
	{
		_cursor.focus.root.drawRecursive();

		_cursor.draw();
	}


private:
	Cursor _cursor;
	Table  _table;
	Music  _bgm;


	void onClickVideoConfig()
	{
		_nextScene = new VideoConfig(this);
	}


	void onClickInputConfig()
	{
		auto camera = new Camera(640.0, 480.0, 0.0, 0.0, 0.5);

		_cursor.focus = new PushButton(&onJoystickFound);

		camera.add(_cursor.focus);
	}


	void onJoystickFound(Joystick joystick)
	{
		_nextScene = new InputConfig(joystick, this);
	}


	void onClickQuit()
	{
		GameLoop().quit();
	}
}



alias void delegate(Joystick) OnJoystickFound;



private class PushButton : Component
{
public:
	this(OnJoystickFound dg)
	{
		position[] = 0.0;

		_dg = dg;

		_guides[0] = new ImageLabel(Texture("$LIB/config/pushbutton0.png", false));
		_guides[1] = new ImageLabel(Texture("$LIB/config/pushbutton1.png", false));
		_guides[2] = new ImageLabel(Texture("$LIB/config/pushbutton2.png", false));

		Component.alignVertical(_guides, position);

		size[0] = _guides[1].size[0];
		size[1] = _guides[0].position[1] + _guides[0].size[1] - _guides[2].position[1];

		foreach(guide; _guides)
		{
			guide.slideIn();
			guide.color = GREEN;
			add(guide);
		}
	}


	override void onUpdate()
	{
		if(auto pad = Joysticks().detectPressed())
		{
			_dg(pad);
		}
	}


private:
	OnJoystickFound _dg;
	ImageLabel[3]   _guides;
}
