# Duane's Dungeon - a rogue-like game
# Copyright (C) 2023 Duane Robertson

# game.gd - the game's root scene

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.


extends Node2D


const HELP_TEXT = """usage: godot . -- [-h] [-a] [--png[=N]]

Dgen, a simple rogue-like

options:
  -h, --help           show this help message and exit
  -a, --android        test in android format
  -p, --png[=N]        save N dungeon maps as png"""

var options = [
	['a', 'android', false],
	['h', 'help', false],
	['p', 'png', true],
]
var switches = {}


var background:TextureRect
var text_container:VBoxContainer


func _input(event):
	if event.is_action_pressed('ui_quit'):
		get_tree().quit(0)


# Called when the node enters the scene tree for the first time.
func _ready():
	parse_args()

	if switches.has('help'):
		print(HELP_TEXT)
		get_tree().quit(0)
		return

	G.game = self

	Creature.seed_names()

	var state = StateSetup.new()
	add_child(state)


func show_message(text: String, color: Color) -> void:
	var lab: Label = VanishingLabel.new(text, color)
	text_container.add_child(lab)


func parse_args():
	var args = OS.get_cmdline_user_args()
	var rs = RegEx.new()
	var rl = RegEx.new()

	rs.compile('^-(\\w+)$')
	rl.compile('^--(\\w+)(=(\\w+))?$')

	var wt
	for arg in args:
		var sw
		var va = ''

		var ms = rs.search(arg)
		if ms:
			sw = ms.get_string(1)

		var ml = rl.search(arg)
		if ml:
			sw = ml.get_string(1)
			va = ml.get_string(3)

		if wt and (ms or ml):
			switches[wt] = ''
			wt = null

		for opt in options:
			if opt[0] == sw or opt[1] == sw:
				if opt[2] and opt[0] == sw:
					wt = opt[1]
				else:
					switches[opt[1]] = va

				break

		if wt and not (ms or ml):
			switches[wt] = arg
			wt = null

	if wt:
		switches[wt] = ''

	# print(switches)
