using Godot;
using System;

public partial class Controller : Window
{
	private double _closeTime = 0;
	private bool _screenReady = true;
	private bool _loadReady = true;
	
	private Button _btnScene1;
	private Button _btnScene2;
	private Button _btnScene3;
	private Button _btnScene4;
	private Button _btnLoad;
	private Button _btnScreen;
	private Button _btnClose;
	
	
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		this._btnScene1 = this.GetNode<Button>("BtnScene1");
		this._btnScene2 = this.GetNode<Button>("BtnScene2");
		this._btnScene3 = this.GetNode<Button>("BtnScene3");
		this._btnScene4 = this.GetNode<Button>("BtnScene4");

		this._btnLoad = this.GetNode<Button>("BtnLoad");
		this._btnScreen = this.GetNode<Button>("BtnScreen");
		this._btnClose = this.GetNode<Button>("BtnClose");
		
		
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		Vector2 border = new Vector2(15, 15);
		Vector2 rasterSize = this.Size / 4;
		Vector2 btnSize = rasterSize - 2 * border;
		
		this._btnScene1.Position = border + (new Vector2(0, 0)*rasterSize);
		this._btnScene2.Position = border + (new Vector2(1, 0)*rasterSize);
		this._btnScene3.Position = border + (new Vector2(2, 0)*rasterSize);
		this._btnScene4.Position = border + (new Vector2(3, 0)*rasterSize);
		this._btnScene1.Size = btnSize;
		this._btnScene2.Size = btnSize;
		this._btnScene3.Size = btnSize;
		this._btnScene4.Size = btnSize;
		
		this._btnLoad.Size = btnSize;
		this._btnLoad.Position = border + (new Vector2(3, 2)*rasterSize);
		this._btnScreen.Size = btnSize;
		this._btnScreen.Position = border + (new Vector2(3, 3)*rasterSize);
		
		this._btnClose.Size = btnSize * new Vector2(3, 1) + border * new Vector2(4, 0);
		this._btnClose.Position = border + (new Vector2(0, 3)*rasterSize);
		
		// handle close button
		if (this._btnClose.IsPressed())
			this._closeTime += delta;
		else
			this._closeTime = 0;
		
		this._btnClose.AddThemeColorOverride("font_pressed_color", new Color((float)this._closeTime, 0, 0));
		if (this._closeTime > 1) this.GetTree().Quit();
		
		// Get a reference to the root script
		MainScript mainScript = (MainScript)this.GetTree().GetRoot().GetNode<MainScript>("Main");
		
		if (this._btnScreen.IsPressed() && this._screenReady)
		{
			mainScript.SetFullScreen();
			this._screenReady = false;
		}
		else if (!this._btnScreen.IsPressed() && !this._screenReady)
		{
			this._screenReady = true;
		}
		
		if (this._btnScene1.IsPressed()) mainScript.SetScene(0);
		if (this._btnScene2.IsPressed()) mainScript.SetScene(1);
		if (this._btnScene3.IsPressed()) mainScript.SetScene(2);
		if (this._btnScene4.IsPressed()) mainScript.SetScene(3);

		if (this._btnLoad.IsPressed() && this._loadReady)
		{
			mainScript.UpdateConfig();
			this._loadReady = false;
		} else if (!this._btnLoad.IsPressed() && !this._loadReady)
		{
			this._loadReady = true;
		}
	}
	
}
