-- Import the command handler.
local actionHandler = require("src/action_handler")
local skinHandler = require("src/skin_handler")
local bretUtils = require("src/brets_utils")
local animationHandler = require("src/animation_handler")

-- Import the config.
local config = require("config")

--hide vanilla model
vanilla_model.PLAYER:setVisible(false)
nameplate.ENTITY:setVisible(true)
vanilla_model.ELYTRA:setVisible(false)

-- Initialize the action wheel.
local mainPage = bretUtils.action_wheel.init()
local animationPage = actionHandler.create_page(mainPage)
local soundsPage = actionHandler.create_page(mainPage)
local skinPage = skinHandler.init(mainPage)

-- Define the animation events.
animationHandler.config = config.event_animations

-- Run on entity_init.
function events.entity_init()
    -- Determine render instructions
    events.RENDER:register(function(delta, context)
        -- Correct crouch animation
        models.model.root.Body.UpperBody.offset_head.Head:setRot(-vanilla_model.BODY:getOriginRot())
        models.model.root.Body.UpperBody.offset_head.Head:setPos(vanilla_model.BODY:getOriginPos())
        models.model.root.Body:setPos(vanilla_model.BODY:getOriginPos())
        models.model.root.Body.UpperBody.Arms:setRot(-vanilla_model.BODY:getOriginRot() / 2)
        models.model.root.Body.UpperBody.Arms:setPos(vanilla_model.BODY:getOriginPos())
        models.model.root.LeftLeg:setPos(-vanilla_model.LEFT_LEG:getOriginPos())
        models.model.root.RightLeg:setPos(-vanilla_model.RIGHT_LEG:getOriginPos())
        -- Show the psuedo arms since we replaced the default
        if host:isHost() then
            local firstPerson = context == "FIRST_PERSON"
            models.model.root.Body.UpperBody.Arms:setVisible(not firstPerson)
            models.model.root.DispArms:setVisible(firstPerson)
        end
    end)

    -- Add the animations to the animations page.
    actionHandler.action.add(animationPage, config.animations)

    -- Add the sounds to the sounds page.
    actionHandler.action.add(soundsPage, config.sounds)

    -- Define the skins.
    skinHandler.add(config.skins, "skins.{object}")

    -- Add the pages to the action wheel.
    bretUtils.action_wheel.add("ominous_bottle", "Animation Library", animationPage)
    bretUtils.action_wheel.add("book", "Skin Library", skinPage)
    bretUtils.action_wheel.add("jukebox", "Sound Library", soundsPage)

    -- Initiate the eye movement animations.
    animationHandler.setEye(models.model.root.Body.UpperBody.offset_head.Head.REye, 1, 0, 0.2, 0.2)
    animationHandler.setEye(models.model.root.Body.UpperBody.offset_head.Head.LEye, 0, 1, 0.2, 0.2)

    -- Add the items to the handler.
    animationHandler.items.add(config.items)
end

-- Runs each tick.
function events.TICK()
    if actionHandler.currentAnimations.sit == true or actionHandler.currentAnimations.crawling == true then
        renderer:setCameraPos(0, -0.5, 0)
    else
        renderer:setCameraPos(0, 0, 0)
    end
end
