using Godot;
using System;

public partial class World : Node3D
{
    private bool _moving = false;
    [Export]
    private Node3D _yawNode;
    [Export]
    private Node3D _pitchNode;
    [Export]
    private Node3D _cameraNode;

    [Export] private float _moveSensitivity = 1f/500f;
    [Export] private float _zoomSensitivity = 55f;

    [Export] private MeshInstance3D _planet;
    
    private PlanetHelper _planetHelper;

    public override void _Ready()
    {
        base._Ready();
    }

    public override void _Input(InputEvent @event)
    {
        if (@event is InputEventMouseButton mouseEvent)
        {
            if (mouseEvent.ButtonIndex == MouseButton.Left)
            {
                _moving = mouseEvent.Pressed;
                Input.MouseMode = _moving ? Input.MouseModeEnum.Captured : Input.MouseModeEnum.Visible;
            }
            if (mouseEvent.ButtonIndex == MouseButton.WheelUp)
            {
                _cameraNode.Position -= new Vector3(0, 0, _zoomSensitivity);
            }
            
            if (mouseEvent.ButtonIndex == MouseButton.WheelDown)
            {
                _cameraNode.Position += new Vector3(0, 0, _zoomSensitivity);
            }
        }
        else if (@event is InputEventMouseMotion motionEvent && _moving)
        {
            _yawNode.RotateY(motionEvent.ScreenRelative.X * _moveSensitivity);
            _pitchNode.RotateX(motionEvent.ScreenRelative.Y * _moveSensitivity);
        }
    }

    public override void _Process(double delta)
    {
        if (Input.IsActionJustPressed("enter"))
        {
            _planetHelper.ToggleAutoRun();
        }
        if (Input.IsActionJustPressed("spacebar"))
        {
            _planetHelper.ToggleAdvance();
        }
        
        _planetHelper.Process();
    }
}
