
local actions = require 'staff.actions'
local helpers = require 'staff.helpers'
local objects = require 'objects'

objects.assign_properties("base:teach_objs/skeleton", {
    -- This controls how much an object helps
    -- a teacher with teaching
    bonus_skill = 0.1,
    -- This controls how much an object bonus
    -- drops off with multiple of the object in a
    -- room
    bonus_dropoff = 0.2,
    happiness = 0.3,
    happiness_dropoff = 0.1,
})
objects.assign_properties("base:teach_objs/bookcase", {
    bonus_skill = 0.05,
    bonus_dropoff = 0.4,
})

actions.register("base:chairs/single_chair", function(obj)
    actions.set_state()
    local obj_e = obj.entities[1]
    local x, y = obj_e:get_position()
    local s = math.sin(obj_e.rotation + math.pi * 0.5)
    local c = math.cos(obj_e.rotation + math.pi * 0.5)
    helpers.walk_to_face(x + 0.4 * s, y + 0.4 * c, obj_e.rotation)
    helpers.wait_ticks(20 * math.random(4, 20))
end, function(e, obj)

end, 0.05)

local project_data = serialize.define({
    {"object_index", "u16"},
    {"teaching", "bool"},
})
actions.register("base:teach_objs/projector", function(obj)
    actions.set_state(project_data, {teaching = false})
    local obj_e = obj.entities[1]
    local x, y = obj_e:get_position()
    local s = math.sin(obj_e.rotation + math.pi * 0.5)
    local c = math.cos(obj_e.rotation + math.pi * 0.5)
    helpers.walk_to_face(x + 0.7 * s, y + 0.7 * c, obj_e.rotation)
    actions.set_state(project_data, {teaching = true})
    helpers.wait_ticks(20 * math.random(4, 20))
    helpers.walk_to_face(x - 0.7 * s, y - 0.7 * c, obj_e.rotation)
    helpers.wait_ticks(20 * math.random(4, 20))
    actions.set_state(project_data, {teaching = false})
end, function(e, obj)
    local data = project_data.decode(e.state)
    if not data.teaching then
        if e.animation ~= "idle" and not e.is_walking then
            e:play_animation("idle")
        end
    else
        if e.animation ~= "stand_chat" and not e.is_walking then
            e:play_animation("stand_chat")
        end
    end
end, 5.0)

actions.register("base:teach_objs/bookcase", function(obj)
    actions.set_state()
    local obj_e = obj.entities[1]
    local x, y = obj_e:get_position()
    local c = math.cos(obj_e.rotation)
    local s = math.sin(obj_e.rotation)
    helpers.walk_to_face(
        x - s * 0.3,
        y - c * 0.3,
        math.pi+obj_e.rotation
    )
    helpers.wait_ticks(20 * math.random(4, 10))
end, function(e, obj)

end, 1.0)

local globe_data = serialize.define({
    {"object_index", "u16"},
    {"spin", "bool"},
})
actions.register("base:teach_objs/globe", function(obj)
    actions.set_state(globe_data, {spin = false})
    local obj_e = obj.entities[1]
    local x, y = obj_e:get_position()
    local angle = math.random() * math.pi * 2
    local s = math.sin(angle + math.pi * 0.5)
    local c = math.cos(angle + math.pi * 0.5)
    helpers.walk_to_face(x + 0.5 * s, y + 0.5 * c, angle + math.pi * 0.5)
    actions.set_state(globe_data, {spin = true})
    helpers.wait_ticks(25)
    actions.set_state(globe_data, {spin = false})
    helpers.wait_ticks(20)
end, function(e, obj)
    local data = globe_data.decode(e.state)
    if data.spin then
        local oe = obj.entities[1]
        if oe.animation ~= "spin" then
            oe:play_animation("spin")
            oe:queue_animation("idle")
        end
    end
end, 1.0)