extends Node2D
class_name ImprovedIsometricMapLayerHolder

# Components
@onready var camera = $Camera2D

var map_data : IsometricMapData
var map_renderer : IsometricMapRenderer
var camera_controller : IsometricCameraController
var player_controller : IsometricPlayerController

# Timing for click detection
var click_delay: float = 0.2
var last_click_time: float = 0.0
var clicked_position: Vector2i = Vector2i(1, 1)
var is_tile_selected: bool = false

func _ready() -> void:
    # Initialize map data
    map_data = IsometricMapData.new(50, 30, 30, 0)
    map_data.create_default_map()
    
    # Initialize the renderer
    map_renderer = IsometricMapRenderer.new(map_data, $TileMapLayer)
    
    # Initialize camera controller
    camera_controller = IsometricCameraController.new(camera)
    
    # Draw initial map state
    map_renderer.draw_visible_tiles()

func _process(_delta: float) -> void:
    # This method intentionally left empty - handling done in _input
    pass

func _input(event: InputEvent) -> void:
    # Handle camera movement
    camera_controller.handle_input(event)
    
    # Handle hover effects
    if event is InputEventMouseMotion:
        map_renderer.handle_hover(get_global_mouse_position())
    
    # Handle tile selection
    if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
        var current_time = Time.get_ticks_msec() / 1000.0
        if current_time - last_click_time >= click_delay:
            last_click_time = current_time
            map_renderer.handle_tile_selection()

# Set a player reference
func set_player(player):
    player_controller = IsometricPlayerController.new(player, map_data, map_renderer)
    player.set_map_renderer(map_renderer)
    
# Initialize player position
func init_player():
    return player_controller.init_player()

# Reset the map to initial state
func _on_reset_button_pressed() -> void:
    # Recreate map data
    map_data = IsometricMapData.new(50, 30, 30, 0)
    map_data.create_default_map()
    
    # Reinitialize map renderer
    map_renderer.initialize_map_layers($TileMapLayer)
    map_renderer.draw_visible_tiles()
    
    # Reset camera
    camera_controller.reset_camera()

# Rotation handlers
func _on_cw_pressed() -> void:
    map_data.rotate_around_z_axis(3)
    map_renderer.initialize_map_layers($TileMapLayer)
    map_renderer.draw_visible_tiles()

func _on_ccw_pressed() -> void:
    map_data.rotate_around_z_axis(1)
    map_renderer.initialize_map_layers($TileMapLayer)
    map_renderer.draw_visible_tiles()

func _on_x_cw_pressed() -> void:
    map_data.rotate_around_x_axis(1)
    map_renderer.initialize_map_layers($TileMapLayer)
    map_renderer.draw_visible_tiles()

func _on_x_ccw_pressed() -> void:
    map_data.rotate_around_x_axis(3)
    map_renderer.initialize_map_layers($TileMapLayer)
    map_renderer.draw_visible_tiles()

func _on_y_cw_pressed() -> void:
    map_data.rotate_around_y_axis(3)
    map_renderer.initialize_map_layers($TileMapLayer)
    map_renderer.draw_visible_tiles()

func _on_y_ccw_pressed() -> void:
    map_data.rotate_around_y_axis(1)
    map_renderer.initialize_map_layers($TileMapLayer)
    map_renderer.draw_visible_tiles()

# Player movement handlers
func _on_player_z_layer_change_pressed() -> void:
    print("Moving player up")
    var success = player_controller.move_vertical("up")
    print("Move success: ", success)
    # Always redraw the visible tiles to ensure consistent state
    map_renderer.draw_visible_tiles()
                
func _on_player_z_layer_change_down_pressed() -> void:
    print("Moving player down")
    var success = player_controller.move_vertical("down")
    print("Move success: ", success)
    # Always redraw the visible tiles to ensure consistent state
    map_renderer.draw_visible_tiles()
