local Engine = require("engine")
local KeyCode = require("keycode")



local transform = nil

local currentRotation = 0  -- Current Y-axis rotation in degrees

local rotationSpeed = 240   -- Degrees per second
local moveSpeed = 600       -- Units per second

function OnInit()
    local gameObject = Engine.GetGameObjectByTag("Camera")
    if not gameObject then
        Engine.Log("Error: Camera GameObject not found!")
        return
    end

    transform = gameObject:GetComponent("Transform")
    if not transform then
        Engine.Log("Error: Transform component not found on Camera!")
        return
    end
    
    -- Initialize currentRotation based on the transform's current rotation
    local rotation = transform:GetRotation()
    currentRotation = rotation.y
    
    Engine.Expose("Position X", rotation.x)
    Engine.Expose("Position Z", rotation.z)
    Engine.Expose("Rotation Y", currentRotation)
    Engine.Log("Init Done", {0,1,0,1})
end

function OnUpdate(deltaTime)
    if not transform then
        Engine.Log("Warning: Transform is not initialized.")
        return
    end

    -- Retrieve current rotation and position
    local rotation = transform:GetRotation()
    local position = transform:GetPosition()

    -- Update currentRotation based on input
    if Engine.KeyDown(KeyCode.Q) then
        currentRotation = currentRotation - (deltaTime * rotationSpeed)
    end
    if Engine.KeyDown(KeyCode.E) then
        currentRotation = currentRotation + (deltaTime * rotationSpeed)
    end

    -- Normalize rotation to [0, 360) degrees
    if currentRotation >= 360 then
        currentRotation = currentRotation - 360
    elseif currentRotation < 0 then
        currentRotation = currentRotation + 360
    end

    -- Apply the updated rotation to the Transform component
    local newRotation = {
        x = rotation.x,       -- Preserving existing rotation on X-axis
        y = currentRotation,  -- Updated rotation on Y-axis
        z = rotation.z        -- Preserving existing rotation on Z-axis
    }
    transform:SetRotation(newRotation)

    -- Calculate movement direction based on currentRotation
    local rotationRad = math.rad(currentRotation)

    -- Forward vector (positive Z direction)
    local forward = {
        x = math.cos(rotationRad),
        z = math.sin(rotationRad)
    }

    -- Right vector (positive X direction)
    local right = {
        x = math.sin(rotationRad),
        z = -math.cos(rotationRad)
    }

    -- Initialize movement deltas
    local deltaX = 0
    local deltaZ = 0

    if Engine.KeyDown(KeyCode.W) then
        -- Move forward
        deltaX = deltaX + forward.x * moveSpeed * deltaTime
        deltaZ = deltaZ + forward.z * moveSpeed * deltaTime
    end
    if Engine.KeyDown(KeyCode.S) then
        -- Move backward
        deltaX = deltaX - forward.x * moveSpeed * deltaTime
        deltaZ = deltaZ - forward.z * moveSpeed * deltaTime
    end
    if Engine.KeyDown(KeyCode.A) then
        -- Move right
        deltaX = deltaX + right.x * moveSpeed * deltaTime
        deltaZ = deltaZ + right.z * moveSpeed * deltaTime
    end
    if Engine.KeyDown(KeyCode.D) then
        -- Move left
        deltaX = deltaX - right.x * moveSpeed * deltaTime
        deltaZ = deltaZ - right.z * moveSpeed * deltaTime
    end

    -- Update position based on calculated deltas
    position.x = position.x + deltaX
    position.z = position.z + deltaZ
    transform:SetPosition(position)

    -- Expose updated position and rotation
    Engine.Expose("Position X", position.x)
    Engine.Expose("Position Z", rotation.z)
    Engine.Expose("Rotation Y", currentRotation)
end