# Hurry Curry! - a game about cooking
# Copyright (C) 2025 Hurry Curry! contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
class_name TileFactory
extends Object


class TileName:
	var name# : String
	var variant #: String?
	func _init(raw_name: String):
		var c = Array(raw_name.split(":"))
		name = c[0]; variant = c[1] if c.size() >= 2 else null # TODO Array.get throws errors

class TileCC:
	var tile_name: TileName
	var position: Vector2i
	var neighbors: Array
	var floor_meshers: Dictionary[String, FloorMesher]

var floor_meshers: Dictionary[String, FloorMesher] = {
	"floor": FloorMesher.new(Floor.floor_mesh()),
	"path": FloorMesher.new(Path.floor_mesh()),
	"grass": GrassMesher.new(Grass.floor_mesh()),
	"street": FloorMesher.new(Street.floor_mesh())
}

func produce(raw_name: String, position: Vector2i, neighbors: Array) -> Tile:
	var tile_name = TileName.new(raw_name)

	var ctx := TileCC.new()
	ctx.tile_name = tile_name
	ctx.position = position
	ctx.neighbors = neighbors.map(func(n): return null if n == null else TileName.new(n).name)
	ctx.floor_meshers = floor_meshers

	match tile_name.name:
		"black-hole-counter": return ItemPortal.new(ctx, false)
		"black-hole": return PlayerPortal.new(ctx, false)
		"book": return Book.new(ctx)
		"ceiling-lamp": return CeilingLamp.new(ctx)
		"chair": return Chair.new(ctx)
		"chandelier": return Chandelier.new(ctx)
		"conveyor": return Conveyor.new(ctx)
		"counter-window": return CounterWindow.new(ctx)
		"counter": return CounterBase.new(ctx)
		"cutting-board": return CuttingBoard.new(ctx)
		"door": return Door.new(ctx)
		"fence": return Fence.new(ctx)
		"floor": return Floor.new(ctx)
		"freezer": return Freezer.new(ctx)
		"grass": return Grass.new(ctx)
		"lamp": return Lamp.new(ctx)
		"oven": return Oven.new(ctx)
		"path": return Path.new(ctx)
		"rolling-board": return RollingBoard.new(ctx)
		"deep-fryer": return DeepFryer.new(ctx)
		"sink": return Sink.new(ctx)
		"stove": return Stove.new(ctx)
		"street": return Street.new(ctx)
		"table": return Table.new(ctx)
		"trash": return Trash.new(ctx)
		"tree": return ExteriorTree.new(ctx)
		"wall-window": return WallWindow.new(ctx)
		"wall": return Wall.new(ctx)
		"white-hole-counter": return ItemPortal.new(ctx, true)
		"white-hole": return PlayerPortal.new(ctx, true)

		"house-balcony": return HouseBalcony.new(ctx)
		"house-door": return HouseDoor.new(ctx)
		"house-oriel": return HouseOriel.new(ctx)
		"house-wall": return HouseWall.new(ctx)
		"house-side": return HouseSide.new(ctx)
		"house-roof": return HouseRoof.new(ctx)
		"house-roof-chimney": return HouseRoofChimney.new(ctx)

		"bun-crate": return BunCrate.new(ctx)
		"cheese-crate": return CheeseCrate.new(ctx)
		"coconut-crate": return CoconutCrate.new(ctx)
		"fish-crate": return FishCrate.new(ctx)
		"flour-crate": return FlourCrate.new(ctx)
		"leek-crate": return LeekCrate.new(ctx)
		"lettuce-crate": return LettuceCrate.new(ctx)
		"mushroom-crate": return MushroomCrate.new(ctx)
		"noodles-crate": return NoodlesCrate.new(ctx)
		"steak-crate": return SteakCrate.new(ctx)
		"rice-crate": return RiceCrate.new(ctx)
		"strawberry-crate": return StrawberryCrate.new(ctx)
		"tomato-crate": return TomatoCrate.new(ctx)
		"potato-crate": return PotatoCrate.new(ctx)

		var t:
			push_warning("tile %s unknown" % t)
			return UnknownTile.new(ctx)
