----------------------------------------------------------------------------------------------------
-- Initialization script

-- global vars
mouse = nil
console = nil

Panel = nil
HODialogs = nil
fade_panel = nil
msg_box = nil
Bubble = nil
DebugInfo = nil
SplashScreen = nil
NameInput = nil
MinigameWin = nil
Cutscene = nil
QuitConfirm = nil

local isCreateConsole = false
local isCreateMainPanel = true
isEnableDebugInfo = false

is_show_debug_info = false

group_effect = nil
group_music = nil

local startLevelName = "SplashScreen"

hotkeys = nil

-- misc zorders
ZORDER_LEVEL0 = 100
ZORDER_LEVEL1 = 110
ZORDER_LEVEL2 = 120
ZORDER_LEVEL3 = 130
ZORDER_LEVEL4 = 140

ZORDER_MAIN_PANEL  = 700
ZORDER_GAME_MENU  = 800
ZORDER_MOUSE  = 900
ZORDER_FADE_PANEL  = 5000
ZORDER_CONSOLE  = 10000

current_cutscene = 1

----------------------------------------------------------------------------------------------------
function init_stuff()

    init_meta_stuff()

	mouse = _create_mouse_cursor()
	
	if isCreateConsole == true then
		console = _create_console()
	end

    group_effect = media_mgr():createGroup()
    group_music = media_mgr():createGroup()

	media_mgr():setVolume(0.5, group_effect)
    media_mgr():setVolume(0.5, group_music)
    --toggle_fullscreen(true)
    hotkeys = _create_hotkey_listener()


    hotkeys:addHotkey(SDLK_F4, KMOD_ALT, "quit_confirm()")
--[[
    hotkeys:addHotkey(SDLK_F1, KMOD_CTRL, "Hint.mTimer = -1.0; Hint:onClick()")
    hotkeys:addHotkey(SDLK_F9, KMOD_CTRL, "HOLevel:pickAll()")
    hotkeys:addHotkey(SDLK_1, KMOD_CTRL, "HOLevel:pickKey(1)")
    hotkeys:addHotkey(SDLK_2, KMOD_CTRL, "HOLevel:pickKey(2)")
    hotkeys:addHotkey(SDLK_3, KMOD_CTRL, "HOLevel:pickKey(3)")
    
    hotkeys:addHotkey(SDLK_c, KMOD_CTRL, 'level_mgr:switch_to_level("Collapse")')
    hotkeys:addHotkey(SDLK_s, KMOD_CTRL, 'Match2Core:shuffle()')]]
end

----------------------------------------------------------------------------------------------------
function save_game()
    HOLevel:commitState()
    ProfileList:saveCurrent()
end

----------------------------------------------------------------------------------------------------
function setup_options(change_fullscreen)
    local options = Profile:getUserData("Options")
    if options == nil then
        options = {}
        options.fullscreen = {}
        options.fullscreen.changed = false
        options.fullscreen.value = true
        options.effect = {}
        options.effect.changed = false
        options.effect.value = 0.5
        options.music = {}
        options.music.changed = false
        options.music.value = 0.5
        Profile:setUserData("Options", options)
    end 
    if change_fullscreen and is_fullscreen() ~= options.fullscreen.value then
        toggle_fullscreen(options.fullscreen.value)
    end
    media_mgr():setVolume(options.effect.value, group_effect)
    media_mgr():setVolume(options.music.value, group_music)
end

----------------------------------------------------------------------------------------------------
function load_game()
    ProfileList:load()
end

function switch_profile(profile_index)
    local filename = ProfileList:getProfileFile(ProfileList.data.current)
    
    if ProfileList.infile:lock('profile'..filename..'.txt') then
        local t = ProfileList.infile:read()
        Profile:unpack(t)
    else
        Profile:new()
    end
end

----------------------------------------------------------------------------------------------------
local function create_layer(tbl, name, zorder)
	tbl = render_layer:new(name)
	tbl:get_parent():init()
	layer_mgr():addLayer(tbl:get_parent(), zorder)	
end

----------------------------------------------------------------------------------------------------
function init_base_layers()
	layer_mgr():addLayer(mouse, ZORDER_MOUSE)

	if isCreateConsole == true then
		layer_mgr():addLayer(console, ZORDER_CONSOLE)
		console:setVisible(false)
	end

	if isCreateMainPanel == true then
		Panel = render_layer:new("Panel")
		Panel:get_parent():init()
		layer_mgr():addLayer(Panel:get_parent(), ZORDER_MAIN_PANEL)
		layer_mgr():addLayer(Panel.p_layer, ZORDER_MAIN_PANEL + 1)
		Panel:set_visible(true)
		Panel:set_active(true)
	end

    HODialogs = render_layer:new("HODialogs")
    HODialogs:get_parent():init()
	layer_mgr():addLayer(HODialogs:get_parent(), ZORDER_GAME_MENU)
	HODialogs:set_visible(false)
	HODialogs:set_active(false)
	
	Bubble = render_layer:new("Bubble")
    Bubble:get_parent():init()
	layer_mgr():addLayer(Bubble:get_parent(), ZORDER_GAME_MENU - 1)
	Bubble:set_visible(false)
	Bubble:set_active(false)

    SplashScreen = render_layer:new("SplashScreen")
    SplashScreen:get_parent():init()
	layer_mgr():addLayer(SplashScreen:get_parent(), ZORDER_GAME_MENU + 1)
	SplashScreen:set_visible(false)
	SplashScreen:set_active(false)

    if isEnableDebugInfo then
        DebugInfo = render_layer:new("DebugInfo")
        DebugInfo:get_parent():init()
        layer_mgr():addLayer(DebugInfo:get_parent(), ZORDER_GAME_MENU + 2)
        DebugInfo:set_visible(true)
        DebugInfo:set_active(false)
    end

	NameInput = render_layer:new("NameInput")
    NameInput:get_parent():init()
	layer_mgr():addLayer(NameInput:get_parent(), ZORDER_GAME_MENU - 2)
	NameInput:set_visible(false)
	NameInput:set_active(false)

	MinigameWin = render_layer:new("MinigameWin")
    MinigameWin:get_parent():init()
	layer_mgr():addLayer(MinigameWin:get_parent(), ZORDER_GAME_MENU - 2)
	MinigameWin:set_visible(false)
	MinigameWin:set_active(false)
	
    Cutscene = render_layer:new("Cutscene")
    Cutscene:get_parent():init()
	layer_mgr():addLayer(Cutscene:get_parent(), ZORDER_GAME_MENU + 1)
	Cutscene:set_visible(false)
	Cutscene:set_active(false)
	
	fade_panel = render_layer:new("fade_panel")
	fade_panel:get_parent():init()
	layer_mgr():addLayer(fade_panel:get_parent(), ZORDER_FADE_PANEL)
	fade_panel:set_visible(false)
	fade_panel:set_active(false)

    QuitConfirm = render_layer:new("QuitConfirm")
	QuitConfirm:get_parent():init()
	layer_mgr():addLayer(QuitConfirm:get_parent(), ZORDER_MOUSE - 1)
	QuitConfirm:set_visible(false)
	QuitConfirm:set_active(false)
	
	msg_box = render_layer:new("msg_box")
	msg_box:get_parent():init()
	layer_mgr():addLayer(msg_box:get_parent(), ZORDER_MAIN_PANEL + 2)
	msg_box:set_visible(false)
	msg_box:set_active(false)
end

----------------------------------------------------------------------------------------------------
function init_meta_stuff()
	--[[
	local mtd, mt = get_class_metatable("Vec2")
	
	mt.__towatch = function(v) return {y = v.y, x = v.x} end
	mtd.__towatch = mt.__towatch

	mt = get_class_metatable("gameswf::character")

	mt.__towatch = function(ch) return {frame = ch:get_current_frame(), name = ch:get_name()} end
	
	mt = get_class_metatable("RenderLayerScriptElement")
	
	mt.__towatch = function(el) return { name = el:getName() } end
	
	mt.__persist = function(el)
		local name = el:getName()
		local parent_layer_name = el:getParentLayer():getName()
		local frame = el:getCharacter():get_current_frame()
		local pos = el:getPos()
		
		return function()
			local lr = get_render_layer(parent_layer_name) -- level_mgr:get_layer(parent_layer_name)
			local el = lr:get_element(name)
			el:getCharacter():goto_frame(frame)
			el:setPos(pos)
			return el
		end
	end
	
	mt = get_class_metatable("RenderLayerScript")
	
	mt.__towatch = function(el) return { class = "RenderLayerScript", name = el:getName() } end
	
	mt.__persist = function(_layer)
		log_message("saving layer")
		local layer_name = _layer:getName()
		return function()
			log_message("loading layer "..layer_name)
			local l = get_render_layer(layer_name)
			
			if l ~= nil then
				return l:get_parent()
			else
				return new_layer(layer_name)
			end
			
			return nil
		end
	end]]

end

function input_char(aChar)
    trace(aChar)
end

---
init_stuff()
exec_script("_common.lua")


init_base_layers()
load_game()
setup_options(true)
--Tutorial:init()
Inventory:init()
Levels:init()
CollapseCore:init()
Match2Core:init()
TextTwist:init()
trace('Build: '..VERSION_STRING)
trace(getCurrentTime())

inventory_:init()

level_mgr:switch_to_level(startLevelName)
