#include "ui/menu_common.lua"
#include "avatarui/avatarui.lua"

isCameraTop = 0

function init()
    isCameraTop = 1
    topCamPosition = FindLocation("top_cam_position")
    camPosition = FindLocation("cam_position")
    camTarget = FindLocation("cam_target")
    topCamTarget = FindLocation("top_cam_target")
    local ct = GetLocationTransform(camPosition)
    local tt = GetLocationTransform(camTarget)
    camDir = VecNormalize(VecSub(ct.pos, tt.pos))
    time = 0
end

function tick(dt)
    if isCameraTop > 0 then
       local tct = GetLocationTransform(topCamPosition).pos
       local ct = GetLocationTransform(camPosition).pos
       local pos = VecLerp(tct, ct, 1 - isCameraTop)

       local tt = GetLocationTransform(camTarget).pos
       local ttt = GetLocationTransform(topCamTarget).pos
       tt = VecLerp(ttt, tt, 1 - isCameraTop)

       local dist = VecLength(VecSub(pos, tt))

       local camTransform = Transform(pos, QuatLookAt(pos, tt))
       SetCameraTransform(camTransform)
       SetCameraFov(70)
       SetCameraDof(dist, 1 - isCameraTop)              
    else    
        local ct = GetLocationTransform(camPosition)
        local tt = GetLocationTransform(camTarget)
        camDir = VecNormalize(VecSub(ct.pos, tt.pos))

        time = time + dt
        local v = Vec(0.25 * math.sin(time * 0.25), 0.1 * math.sin(time * 0.1), 0.3 * math.sin(time * 0.35))
        v = VecSub(v, VecScale(camDir, VecDot(v, camDir)))
        ct.pos = VecAdd(ct.pos, v)

        local dist = VecLength(VecSub(ct.pos, tt.pos))

        local camTransform = Transform(ct.pos, QuatLookAt(ct.pos, tt.pos))
        SetCameraTransform(camTransform)
        SetCameraFov(70)
        SetCameraDof(dist, 1)
    end

    if Avatar.Ui.viewModel.focusedSkin then
        if LastInputDevice() ~= UI_DEVICE_GAMEPAD then
            if InputDown("lmb") then
                local skinObject = Avatar.Ui.viewModel.focusedSkin
                local dx = InputValue("mousedx")
                local t = GetAnimatorTransform(skinObject.animator)
                if dx < -20 then dx = -20 end
                if dx > 20 then dx = 20 end
                local q = QuatRotateQuat(t.rot, QuatEuler(0, dx, 0))
                t.rot = q
                SetAnimatorTransform(skinObject.animator, t)
            end
        else
            if InputDown("menu_next") then
                local skinObject = Avatar.Ui.viewModel.focusedSkin
                local dx = 2
                local t = GetAnimatorTransform(skinObject.animator)
                local q = QuatRotateQuat(t.rot, QuatEuler(0, dx, 0))
                t.rot = q
                SetAnimatorTransform(skinObject.animator, t)
            elseif InputDown("menu_prev") then
                local skinObject = Avatar.Ui.viewModel.focusedSkin
                local dx = -2
                local t = GetAnimatorTransform(skinObject.animator)
                local q = QuatRotateQuat(t.rot, QuatEuler(0, dx, 0))
                t.rot = q
                SetAnimatorTransform(skinObject.animator, t)
            end
        end
    end
end

function draw()
    if not MainMenu.canDrawInState(MainMenu.State.Avatar) then
        return
    end

    local fade = MainMenu.getFadeFactor(MainMenu.State.Avatar)

    UiColorFilter(1, 1, 1, fade)

    if fade ~= 1 then
        UiDisableInput()
    end

    isCameraTop = 0
    UiMakeInteractive()
    if LastInputDevice() == UI_DEVICE_GAMEPAD then
        UiSetCursorState(UI_CURSOR_HIDE_AND_LOCK)   
    end

    UiPush()
        local canvas = UiCanvasSize()
        UiTranslate((canvas.w - Avatar.Ui.w) / 2, (canvas.h - Avatar.Ui.h) / 2)
        Avatar.Ui:draw()
    UiPop()

    if InputPressed("esc") or InputPressed("menu_cancel") then
        MainMenu.transitToState(MainMenu.State.Main)
    end
end

function postUpdate(dt)
    Avatar.postUpdate()

    local viewModel = Avatar.Ui.viewModel
    if viewModel.focusedSkin then
        viewModel.focusedSkin:postUpdate(dt)
    end
end
