module config.videoconfig;



private import gedo.video.ggl;
private import gedo.video.color;
private import gedo.video.texture;
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.gui.digiteditor;
private import gedo.input.joystick;
private import gedo.system.scene;
private import gedo.file.config;
private import gedo.text.convert;
private import config.share;



public class VideoConfig : Scene
{
public:
	this(Scene parent)
	{
		_parent = parent;

		int[2] windowSize        = [640, 480];
		bool   isFullScreen      = false;
		bool   isExtensionEnable = true;

		windowSize[0] = .config.share.config.get("window.size.x", 640);
		windowSize[1] = .config.share.config.get("window.size.y", 480);

		isFullScreen = .config.share.config.get("fullscreen", false);
		isExtensionEnable = .config.share.config.get("extension", true);

		_camera = new Camera(640, 480, 0.0, 0.0, 0.5);

		_cursor = new Cursor();
		_cursor.joystick = Joysticks().keyboard;
		_cursor.onCancel = &backToParentScene;

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

			guide.position[0] = 0.0;
			guide.position[1] = -240.0 + guide.size[1]*0.5;

			guide.color = GREEN;

			_camera.add(guide);
		}

		{
			DigitEditor.Field[2] fields;

			fields[0].scale = 4;
			fields[0].value = windowSize[0];
			fields[0].delimiter = new ImageLabel(Texture("$LIB/config/x.png", false));

			fields[1].scale = 4;
			fields[1].value = windowSize[1];
			fields[1].delimiter = null;

			_digitEditor = new DigitEditor(fields);
		}

		auto rootMenu = new Component();
		{
			ImageLabel[3] menus;

			menus[0] = new ImageLabel(Texture("$LIB/config/windowsize.png", false));
			menus[1] = new ImageLabel(Texture("$LIB/config/fullscreen.png", false));
			menus[2] = new ImageLabel(Texture("$LIB/config/extension.png", false));

			auto table = new Table(2, 3);
			table.position[] = 0.0;

			foreach(y, menu; menus)
			{
				table.set(0, y, menu);
			}

			{
				auto subtable = new Table(3, 1);

				subtable.fill(rgba(0, 0, 0, 0));
				subtable.frame(rgba(0, 0, 0, 0));

				auto label = new DigitLabel(windowSize[0]);
				_digitEditor.link(0, label);
				subtable.set(0, 0, label);
				subtable.set(1, 0, new ImageLabel(Texture("$LIB/config/x.png", false)));
				label = new DigitLabel(windowSize[1]);
				_digitEditor.link(1, label);
				subtable.set(2, 0, label);

				table.set(1, 0, subtable);
			}

			auto booleanTextures0 = Texture("$LIB/config/off.png", false);
			auto booleanTextures1 = Texture("$LIB/config/on.png", false);
			_isFullScreen      = new BooleanLabel(booleanTextures0, booleanTextures1);
			_isExtensionEnable = new BooleanLabel(booleanTextures0, booleanTextures1);
			_isFullScreen.value = isFullScreen;
			_isExtensionEnable.value = isExtensionEnable;
			table.set(1, 1, _isFullScreen);
			table.set(1, 2, _isExtensionEnable);

			table.rows[0].add(_digitEditor.createTable());

			rootMenu.add(table);

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

		_cursor.color = YELLOW;
	}


	override void update()
	{
		_camera.update();
		_cursor.updateWithFocus();
	}


	override void draw()
	{
		_camera.drawRecursive();

		_cursor.drawWithFocus();
	}


private:
	BooleanLabel _isFullScreen;
	BooleanLabel _isExtensionEnable;
	DigitEditor  _digitEditor;
	Camera       _camera;
	Cursor       _cursor;
	Scene        _parent;


	void backToParentScene()
	{
		int[2] windowSize;
		bool   isFullScreen;
		bool   isExtensionEnable;

		auto table = cast(Table)_cursor.focus.parent;
		{
			auto subtable = cast(Table)table.rows[0].items[1];

			windowSize[0] = (cast(DigitLabel)subtable.rows[0].items[0]).value;
			windowSize[1] = (cast(DigitLabel)subtable.rows[0].items[2]).value;
		}

		isFullScreen      = _isFullScreen.value;
		isExtensionEnable = _isExtensionEnable.value;

		.config.share.config.set("window.size.x", windowSize[0]);
		.config.share.config.set("window.size.y", windowSize[1]);
		.config.share.config.set("fullscreen", isFullScreen);
		.config.share.config.set("extension", isExtensionEnable);

		_nextScene = _parent;
	}
}
