# 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 Counter
extends Floor

const COUNTERS: Array = [
	"counter",
	"pan",
	"sink",
	"oven",
]

const FLOORS: Array = [
	"floor",
	"chandelier",
	"ceiling-lamp",
	"grass",
	"table",
	"chair"
]

enum CounterKind {
	OUTER_CORNER,
	STRAIGHT,
	STRAIGHT_BACKSPLASH
}

var kind: CounterKind = CounterKind.STRAIGHT

static func interact_target() -> Vector3:
	return Vector3(0, 0.5, 0)

func _init(ctx: TileFactory.TileCC):
	super(ctx)

	var facing: int = 0
	var max_series: int = 0
	var max_idx: int = 0
	for start in range(4):
		var series = 0
		for i in range(4):
			if Counter.is_floor(ctx.neighbors[(start + i) % 4]):
				series += 1
			else:
				break
		if series > max_series:
			max_series = series
			max_idx = start

	# we can neither find out whether it is an inner corner nor an outer corner
	# backsplash
	facing = max_idx
	if max_series == 1:
		if WallTile.WALLS.has(ctx.neighbors[(max_idx + 2) % 4]):
			kind = CounterKind.STRAIGHT_BACKSPLASH
		else:
			kind = CounterKind.STRAIGHT
	elif max_series == 2:
		kind = CounterKind.OUTER_CORNER

	turn_facing(facing)

static func is_counter(tile_name_t) -> bool:
	if tile_name_t == null:
		return false
	return tile_name_t.ends_with("crate") or COUNTERS.has(tile_name_t)

static func is_floor(floor_name) -> bool:
	return FLOORS.has(floor_name)
