class_name StrategyPhase extends Node2D

@onready var player_controller: PlayerController = $PlayerController
@onready var combat_system: CombatSystem = $CombatSystem
@onready var combat_ui: CombatUI = $CombatUI
@onready var combat_map: CombatMap = $CombatMap
@onready var camera: CameraController = $Camera2D

func _ready() -> void:
	player_controller.combat_requested.connect(_on_combat_requested)
	player_controller.mouse_grid_changed.connect(_on_mouse_grid_changed)
	player_controller.camera_drag.connect(camera.apply_drag)
	combat_ui.fight_confirmed.connect(_on_fight_confirmed)
	combat_ui.fight_cancelled.connect(_on_fight_cancelled)
	combat_ui.combat_system = combat_system


func _on_mouse_grid_changed(coords: Vector2i) -> void:
	combat_map.target_tile(coords)

func _on_combat_requested(attacker: Unit, defender: Unit) -> void:
	var atk_coords := combat_map.world_to_coords(attacker.position)
	var def_coords := combat_map.world_to_coords(defender.position)
	var distance := absi(atk_coords.x - def_coords.x) + absi(atk_coords.y - def_coords.y)
	var proposal := combat_system.create_proposal(attacker, defender, distance)
	_set_input_disabled(true)
	combat_ui.show_proposal(proposal)


func _on_fight_confirmed(proposal: CombatProposal) -> void:
	combat_system.apply_proposal(proposal)
	_set_input_disabled(false)


func _on_fight_cancelled() -> void:
	_set_input_disabled(false)


func _set_input_disabled(disabled: bool) -> void:
	player_controller.input_disabled = disabled
	combat_map.set_highlight_enabled(not disabled)
