module vs.scene.config;



private import std.math;
private import vs.action.button;
private import gedo.system.scene;
private import gedo.video.texture;
private import gedo.audio.gal;
private import gedo.gui.label;
private import gedo.gui.table;
private import gedo.gui.cursor;
private import gedo.gui.component;
private import gedo.gui.digiteditor;
private import gedo.input.joystick;



alias void delegate(ConfigScene) OnQuitConfig;



public class ConfigScene : Scene
{
public:
	this(
			Scene        parent,
			Component    bg,
			Joystick     joystick,
			OnQuitConfig dg,
			uint         level,
			uint         arrowSize,
			bool         isHitCameraEnable,
			float        bgmVolume,
			float        seVolume
	)
	{
		_bg     = bg;
		_dg     = dg;
		_parent = parent;
		if(level >= _levels.length)
		{
			level = 0;
		}

		_cursor = new Cursor();

		_cursor.joystick = joystick;

		_levels[0] = Texture("$LIB/config/cplevel0.png", false);
		_levels[1] = Texture("$LIB/config/cplevel1.png", false);
		_levels[2] = Texture("$LIB/config/cplevel2.png", false);
		_levels[3] = Texture("$LIB/config/cplevel3.png", false);

		auto table = new Table(2, 6);

		auto item = new ImageLabel(Texture("$LIB/config/cplevel.png", false));
		table.set(0, 0, item);

		auto subtable = new Table(1, _levels.length);
		foreach(i, levelTexture; _levels)
		{
			item = new ImageLabel(levelTexture);
			subtable.set(0, i, item);
			subtable.rows[i].cbClick = &onClickCPLevel;
		}

		subtable.position[0] = 160.0;

		table.set(1, 0, new ImageLabel(_levels[level]));
		table.rows[0].add(subtable);

		_arrowSizeLabel = new DigitLabel(arrowSize);
		item = new ImageLabel(Texture("$LIB/config/arrowsize.png", false));
		table.set(0, 1, item);
		table.set(1, 1, _arrowSizeLabel);

		_arrowSizeEditor = new DigitEditor(arrowSize, 3);
		_arrowSizeEditor.position[] = 0.0;
		_arrowSizeEditor.link(0, _arrowSizeLabel);
		table.rows[1].add(_arrowSizeEditor.createTable());

		auto booleanTextures0 = Texture("$LIB/config/off.png", false);
		auto booleanTextures1 = Texture("$LIB/config/on.png", false);

		item = new ImageLabel(Texture("$LIB/config/hitcamera.png", false));
		_isHitCameraEnable = new BooleanLabel(booleanTextures0, booleanTextures1);
		_isHitCameraEnable.value = isHitCameraEnable;
		table.set(0, 2, item);
		table.set(1, 2, _isHitCameraEnable);

		_bgmVolumeLabel = new DigitLabel(cast(int)(bgmVolume * 9));
		item = new ImageLabel(Texture("$LIB/config/bgmvolume.png", false));
		table.set(0, 3, item);
		table.set(1, 3, _bgmVolumeLabel);

		_bgmVolumeEditor = new DigitEditor(_bgmVolumeLabel.value, 1);
		_bgmVolumeEditor.position[] = 0.0;
		_bgmVolumeEditor.link(0, _bgmVolumeLabel);
		_bgmVolumeEditor.cbChangeValue = &onChangeVolume;
		table.rows[3].add(_bgmVolumeEditor.createTable());

		_seVolumeLabel = new DigitLabel(cast(int)(seVolume * 9));
		item = new ImageLabel(Texture("$LIB/config/sevolume.png", false));
		table.set(0, 4, item);
		table.set(1, 4, _seVolumeLabel);

		_seVolumeEditor = new DigitEditor(_seVolumeLabel.value, 1);
		_seVolumeEditor.position[] = 0.0;
		_seVolumeEditor.link(0, _seVolumeLabel);
		_seVolumeEditor.cbChangeValue = &onChangeVolume;
		table.rows[4].add(_seVolumeEditor.createTable());

		item = new ImageLabel(Texture("$LIB/config/quit.png", false));
		table.set(0, 5, item);
		table.rows[5].cbClick = &quitScene;

		table.set(1, 5, new Component());

		_cursor.focus = table.rows[0];
		_cursor.onCancel = &quitScene;
	}


	uint cplevel()
	{
		auto table = cast(Table)_cursor.focus.root;

		auto label = cast(ImageLabel)table.rows[0].items[1];

		foreach(i, level; _levels)
		{
			if(label.texture is level)
			{
				return i;
			}
		}

		return 0;
	}


	bool isHitCameraEnable()
	{
		return _isHitCameraEnable.value;
	}


	uint arrowSize()
	{
		return _arrowSizeLabel.value;
	}


	float bgmVolume()
	{
		return _bgmVolumeLabel.value / 9.0;
	}


	float seVolume()
	{
		return _seVolumeLabel.value / 9.0;
	}


	override void update()
	{
		_bg.update();

		_cursor.updateWithFocus();
	}


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

		_cursor.drawWithFocus();
	}


private:
	OnQuitConfig _dg;
	BooleanLabel _isHitCameraEnable;
	DigitEditor  _arrowSizeEditor;
	DigitEditor  _bgmVolumeEditor;
	DigitEditor  _seVolumeEditor;
	DigitLabel   _arrowSizeLabel;
	DigitLabel   _bgmVolumeLabel;
	DigitLabel   _seVolumeLabel;
	Texture[4]   _levels;
	Component    _bg;
	Cursor       _cursor;
	Scene        _parent;


	void quitScene()
	{
		_dg(this);

		_nextScene = _parent;
	}


	void onClickCPLevel()
	{
		auto row = cast(RowItem)_cursor.focus;

		auto sublabel = cast(ImageLabel)row.items[0];

		auto table = cast(Table)row.parent.parent.parent;

		auto label = cast(ImageLabel)table.rows[0].items[1];

		label.texture = sublabel.texture;

		_cursor.focus = _cursor.focus.parent.parent;
	}


	void onChangeVolume(DigitEditor editor)
	{
		editor.sync();

		GAL_setVolume(bgmVolume, seVolume);
	}
}
