tool

extends StaticBody2D

class_name GameObjectComputer

enum Direction { LEFT, RIGHT, UP, DOWN }
enum ComputerType { ShipCommand, Comms, Medical, Research, Energy, ShipEngine, Atmos, Monitoring }

export var object_name := ""
export(Direction) var direction := Direction.DOWN setget set_direction
export(ComputerType) var computer_type := ComputerType.ShipCommand setget set_type

var screen_region_offset := Vector2.ZERO
var open := false

onready var activationRange := $ActivationRange as ActivationRange
onready var manager := $PowerManager as PowerManager

func _ready():
	if not Engine.editor_hint:
		activationRange.visible = true
		$computer/screen/Light2D.visible = true

func set_type(val):
	computer_type = val
	match computer_type:
		ComputerType.ShipCommand:
			screen_region_offset = Vector2(0, 0)
		ComputerType.Comms:
			screen_region_offset = Vector2(0, 32)
		ComputerType.Medical:
			screen_region_offset = Vector2(0, 64)
		ComputerType.Research:
			screen_region_offset = Vector2(128, 0)
		ComputerType.Energy:
			screen_region_offset = Vector2(128, 224)
		ComputerType.Atmos:
			screen_region_offset = Vector2(128, 192)
		ComputerType.ShipEngine:
			screen_region_offset = Vector2(0, 256)
		ComputerType.Monitoring:
			screen_region_offset = Vector2(128, 32)

	refresh_sprite()

func set_direction(dir):
	direction = dir
	refresh_sprite()

func refresh_sprite():
	match direction:
		Direction.DOWN:
			$computer.region_rect.position = Vector2(0, 0)
			$computer/screen.region_rect.position = screen_region_offset + Vector2(0, 0)
			$computer/screen/Light2D.rotation = 0
		Direction.UP:
			$computer.region_rect.position = Vector2(0, 32)
			$computer/screen.region_rect.position = screen_region_offset + Vector2(32, 0)
			$computer/screen/Light2D.rotation = PI
		Direction.LEFT:
			$computer.region_rect.position = Vector2(32, 32)
			$computer/screen.region_rect.position = screen_region_offset + Vector2(96, 0)
			$computer/screen/Light2D.rotation = PI/2
		Direction.RIGHT:
			$computer.region_rect.position = Vector2(32, 0)
			$computer/screen.region_rect.position = screen_region_offset + Vector2(64, 0)
			$computer/screen/Light2D.rotation = -PI/2

func manage_controls(show: bool):
	match computer_type:
		ComputerType.ShipCommand:
			$Control/ControlComp.visible = show
		ComputerType.Energy:
			$Control/EnergyComp.visible = show

func _input(event):
	if event is InputEventMouseButton and event.pressed and not is_inside and open:
		open = false
		$UIAnimation.play("fadeout")

func _input_event(_viewport, event, _shape_idx):
	if Engine.editor_hint:
		return
	if event is InputEventMouseButton and event.pressed and not open:
		# Must be in range
		if activationRange.in_range() and manager.powered:
			open = true
			manage_controls(true)
			$UIAnimation.play("fadein")

var is_inside = false
func _ui_focus_changed(entered):
	is_inside = entered

func _ui_fade_completed(anim_name):
	if anim_name == "fadeout":
		manage_controls(false)

func _force_close_ui():
	if open:
		open = false
		$UIAnimation.play("fadeout")
	match computer_type:
		ComputerType.ShipCommand:
			$Control/ControlComp.force_close()

func _power_status_changed(powered: bool) -> void:
	activationRange.visible = powered
	if powered:
		$ScreenAnimation.play("on")
	else:
		$ScreenAnimation.play("off")

func serialize():
	return {
		"direction": direction,
		"computer_type": computer_type
	}

func deserialize(data):
	set_direction(data["direction"])
	set_type(data["computer_type"])

func inspect():
	match computer_type:
		ComputerType.ShipCommand:
			return {
				"type": "Control board computer",
				"description": "A computer for moving the ship around the galaxy",
				"interaction": "Click when near to interact"
			}
		ComputerType.Comms:
			return {
				"type": "Comms computer",
				"description": "A computer for issuing and controlling communications",
				"interaction": "Click when near to interact"
			}
		ComputerType.Medical:
			return {
				"type": "Medical computer",
				"description": "A computer for checking health status and medical procedures",
				"interaction": "Click when near to interact"
			}
		ComputerType.Research:
			return {
				"type": "R&D computer",
				"description": "A computer for researching new tech and control printers",
				"interaction": "Click when near to interact"
			}
		ComputerType.Energy:
			return {
				"type": "Energy supply computer",
				"description": "A computer for monitoring and managing the energy grid",
				"interaction": "Click when near to interact"
			}
		ComputerType.Atmos:
			return {
				"type": "Atmos computer",
				"description": "A computer for monitoring and managing the air supply",
				"interaction": "Click when near to interact"
			}
		ComputerType.ShipEngine:
			return {
				"type": "Engine control computer",
				"description": "A computer for monitoring and managing the ship engines",
				"interaction": "Click when near to interact"
			}
		ComputerType.Monitoring:
			return {
				"type": "Monitoring computer",
				"description": "A computer for monitoring crew and ship status",
				"interaction": "Click when near to interact"
			}
