-----------------------------------------------------------------------------------
-- This implementation uses the default SimpleProject and the Project extensions are 
-- used to extend the SimpleProject behavior.

-- This is the global table name used by Appkit Basic project to extend behavior
Project = Project or {}

local Vector3            = stingray.Vector3
local Hololens           = stingray.Hololens

require 'script/lua/flow_callbacks'

Project.level_names = {
	empty = "content/levels/empty"
}

-- Can provide a config for the basic project, or it will use a default if not.
local SimpleProject = require 'core/appkit/lua/simple_project'
SimpleProject.config = {
	standalone_init_level_name = Project.level_names.empty,
	camera_unit = "core/appkit/units/camera/camera",
	camera_index = 1,
	shading_environment = nil, -- Will override levels that have env set in editor.
	create_free_cam_player = true, -- Project will provide its own player.
	exit_standalone_with_esc_key = true
}


function Project.on_init_complete()
    if not Hololens then return end
    
    local managed_world = Appkit.managed_world
    local world = managed_world.world
    
	local world_wrapper = Appkit.get_managed_world_wrapper(world)
	if not world_wrapper then return end

	local ar_camera = world_wrapper:get_enabled_camera()

	stingray.Camera.set_near_range(ar_camera, 0.01)
	stingray.Camera.set_far_range(ar_camera, 1000.0)
	
	local success = Hololens.initialize("output_target", ar_camera, world)
	assert(success == true)
	Project.ar_viewport = stingray.Application.create_viewport(world, "ar_main")
	
	Project.cube_unit = stingray.World.spawn_unit(world, "content/models/props/ChamfBox")
	stingray.Unit.set_local_position(Project.cube_unit, 1, Vector3(0, 2, 0)) 
	stingray.Unit.set_local_scale(Project.cube_unit, 1, Vector3(0.03, 0.03, 0.03))

	Project.rotation_angle = 0	
end

-- Optional function by SimpleProject after level, world and player is loaded 
-- but before lua trigger level loaded node is activated.
function Project.on_level_load_pre_flow()
end

-- Optional function by SimpleProject after loading of level, world and player and 
-- triggering of the level loaded flow node.
function Project.on_level_shutdown_post_flow()
end

-- Optional function called by SimpleProject after world update (we will probably want to split to pre/post appkit calls)
function Project.update(dt)
	if not Hololens then return end

    local held = Hololens.interaction_held(Hololens.HAND)
    
    if held then 
       local pose  =  Hololens.head_pose()
       local m = Vector3.lerp(stingray.Unit.local_position(Project.cube_unit, 1),  stingray.Matrix4x4.transform(pose, Vector3(0,2,0)), 0.2)
       stingray.Unit.set_local_position(Project.cube_unit, 1, m)
    end
    stingray.Unit.set_local_rotation(Project.cube_unit, 1, stingray.Quaternion.from_euler_angles_xyz(0,0,Project.rotation_angle))
    Project.rotation_angle = Project.rotation_angle + 30 * dt
    Hololens.set_focus_point(stingray.Unit.local_position(Project.cube_unit, 1))
end

-- Optional function called by SimpleProject *before* appkit/world render
function Project.render()
	local managed_world = Appkit.managed_world
	local world = managed_world.world
	local world_wrapper = Appkit.get_managed_world_wrapper(world)
	local ar_camera = world_wrapper:get_enabled_camera()

    if(managed_world and Project.ar_viewport) then
    	stingray.Application.render_world(world, ar_camera, Project.ar_viewport, managed_world.shading_env)
    else
		Appkit.render()
    end
end

-- Optional function called by SimpleProject *before* appkit/level/player/world shutdown
function Project.shutdown()
end

return Project
