using System;
using Godot;

namespace Hellswipers;

[Tool]
[GlobalClass]
public partial class TextureToggleButton : BaseButton {
	public Action<bool> StateChanged = null;
	
	[Export] public Texture2D FalseTexture;
	[Export] public Texture2D TrueTexture;
	[Export]
	public bool State {
		get => _state;
		set {
			_state = value;
			StateChanged?.Invoke(value);
			QueueRedraw();
		}
	}

	private bool _state;
	private Color _baseColor;
	private Color _hoverColor;
	private Color _pressColor;

	public override Vector2 _GetMinimumSize() => new(
		Math.Max(FalseTexture?.GetWidth() ?? 0, TrueTexture?.GetWidth() ?? 0),
		Math.Max(FalseTexture?.GetHeight() ?? 0, TrueTexture?.GetHeight() ?? 0)
	);

	public override void _Pressed() => State = !State;

	public override void _Draw() {
		SelfModulate = GetDrawMode() switch {
			DrawMode.Normal => _baseColor,
			DrawMode.Disabled => _baseColor,
			DrawMode.Hover => _hoverColor,
			DrawMode.Pressed => _pressColor,
			DrawMode.HoverPressed => _pressColor,
			_ => SelfModulate
		};
		
		if (TrueTexture != null && FalseTexture != null)
			DrawTexture(State ? TrueTexture : FalseTexture, Vector2.Zero);
	}

	public override void _Ready() {
		_baseColor = GetThemeColor("font_color", "Button");
		_hoverColor = GetThemeColor("font_hover_color", "Button");
		_pressColor = GetThemeColor("font_pressed_color", "Button");
	}
}
