require("scripts/const")
require("scripts/framework/clWindow")
require("scripts/framework/clLocationScreen")
require("scripts/framework/clZoomZoneScreen")
require("scripts/framework/clComicsScreen")
require("scripts/framework/clGameItem")
require("scripts/game_init")
require("scripts/framework/clScreenManager")
require("scripts/framework/audio")

SHOW_MAIN_MENU = true
SHOW_LOGO = true

clApplication = {}

clApplication.game_pause = false

function clApplication:LoadScreen()  
    cpp_application_add_window("back_black");

    cpp_create_sprite("back_black", OBJ_TYPE.ACTIVE_ALL, "black_background", "graphics/trigger", -WINDOW_WIDTH / 2, -WINDOW_HEIGHT / 2);
    cpp_item_set_size("black_background", WINDOW_WIDTH * 2, WINDOW_HEIGHT * 2);
    ItemSetDepth("black_background", LAYERS.BACK_MOST)
    
    clApplication:SetCursor("graphics/interface/cursors/cursor")
    DelayedExec(0.1, function()
        last_level = start_level or "partner_logo"
        CURRENT_SCREEN = "screen_"..last_level;   
        CurrentScreen = clScreenManager:add_screen(last_level)  
        --SetState("GLOBAL", "last_screen", last_level)
        
        --[[local last_level = "000_before"
        if(start_level) then
            last_level = start_level;
        elseif(GetState("GLOBAL", "last_screen") ~= "") then
            last_level = GetState("GLOBAL", "last_screen");
        else
            
        end
      
        CURRENT_SCREEN = "screen_"..last_level;   
        CurrentScreen = clScreenManager:add_screen(last_level)   
        if(SHOW_MAIN_MENU) then
            CurrentScreen:PauseScreen()
            local main_menu = clScreenManager:add_screen("MainMenuScreen") 
            if(SHOW_LOGO) then
                main_menu:ShowLogo()
            end
        end--]]
        self:SetBlackLines()
    end)
end
--[[
function clApplication:LoadScreen()  
    clApplication:SetCursor("graphics/interface/cursors/cursor")
    local level_name = start_level
    if(level_name == nil) then
        if(GetState("GLOBAL", "last_screen") == "") then
            level_name = "001_ward"
        else
            level_name = GetState("GLOBAL", "last_screen");
        end
        
    end
    CURRENT_SCREEN = "screen_"..level_name;   
    CurrentScreen = clScreenManager:add_screen(level_name)   
end
--]]
function clApplication:Update()	
    clScreenManager:Update(messenger_table);
   
    messenger_table.on_click = "";
    messenger_table.on_mouse_up = "";
    messenger_table.on_mouse_enter = "";
    messenger_table.on_mouse_leave = "";
    messenger_table.on_mouse_over = "";
    messenger_table.on_mouse_right_down = "";
    messenger_table.on_mouse_right_up = "";
    messenger_table.on_button_down = "";
    messenger_table.on_key_down = "";
end

function clApplication:LoadLocation(_loction_name, no_hide)
    cpp_save_players();
    --CurrentScreen:UnLoadScreen();
    CURRENT_SCREEN = "screen_".._loction_name; 
    --StopMusic(clApplication.m_id)
    
    cpp_application_add_window("hide_screen");
    cpp_create_sprite("hide_screen", OBJ_TYPE.ACTIVE_ALL, "wp_back_trigger", "graphics/trigger", 0, 0);
    cpp_item_set_size("wp_back_trigger", WINDOW_WIDTH, WINDOW_HEIGHT);
    ItemSetOpacity("hide_screen", 0.0)
    if(not no_hide) then
        ItemChangeOpacity("hide_screen", 1.0, 0.3)
    end
    __stop_all_sfx();
    HUDScreen:RemoveScreen();
    HintTest:RemoveLoop()
    Tip:RemoveLoop()
    clScreenManager:remove_screen(HUDScreen) 
    clScreenManager:remove_screen(Inventory) 
    clScreenManager:remove_screen(DragObject) 
    
    Inventory.state = nil;
    HUDScreen.state = nil;
    
    StopRequests(113)
    
    DelayedExec(0.3, function()
        --cpp_application_remove_window("hide_screen");
        clScreenManager:remove_screen(CurrentScreen);
        CurrentScreen = clScreenManager:add_screen(_loction_name) 
        
        ItemSetDepth("hide_screen", LAYERS.FRONT_MOST)    
    
        --if(not no_hide) then
            ItemChangeOpacity("hide_screen", 0.0, 0.5)
        --end
        
        DelayedExec(0.3, function()
            cpp_application_remove_window("hide_screen");
        end)
        clApplication:SetCursor("graphics/interface/cursors/cursor")
        if(_loction_name ~= "company_logo" and _loction_name ~= "partner_logo" and _loction_name ~= "000_before") then
            SetState("GLOBAL", "last_screen", CurrentScreen.name)
        end
        self:SetBlackLines()
    end)
end

function clApplication:ChangeLocation(_loction_name)
    if(_loction_name ~= "company_logo" and _loction_name ~= "partner_logo" and _loction_name ~= "000_before") then
        SetState("GLOBAL", "last_screen", _loction_name)
    end
    cpp_save_players();
    CURRENT_SCREEN = "screen_".._loction_name; 
    --StopMusic(clApplication.m_id)
    __stop_all_sfx();
    Inventory:ClearData();
    HUDScreen:RemoveScreen();
    Inventory:RemoveScreen();
    clScreenManager:remove_screen(HUDScreen) 
    clScreenManager:remove_screen(Inventory) 
    clScreenManager:remove_screen(DragObject) 
    
    Inventory.state = nil;
    HUDScreen.state = nil;
    
    clScreenManager:remove_all_screen()
    CurrentScreen = clScreenManager:add_screen(_loction_name)   
    CurrentScreen:PauseScreen()
    clScreenManager:add_screen("MainMenuScreen")    
    self:SetBlackLines()     
end
---------------------------------------------------------------
function clApplication:SetBlackLines()
    cpp_application_remove_window("black_line")
    cpp_application_add_window("black_line");
    cpp_create_sprite("black_line", OBJ_TYPE.ACTIVE_ALL, "left_line", "graphics/trigger", -WINDOW_WIDTH, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    cpp_item_set_size("left_line", WINDOW_WIDTH, WINDOW_HEIGHT);
    cpp_create_sprite("black_line", OBJ_TYPE.ACTIVE_ALL, "right_line", "graphics/trigger", WINDOW_WIDTH, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    cpp_item_set_size("right_line", WINDOW_WIDTH, WINDOW_HEIGHT);
    cpp_create_sprite("black_line", OBJ_TYPE.ACTIVE_ALL, "top_line", "graphics/trigger", 0, -WINDOW_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT);
    cpp_item_set_size("top_line", WINDOW_WIDTH, WINDOW_HEIGHT);
    cpp_create_sprite("black_line", OBJ_TYPE.ACTIVE_ALL, "bottom_line", "graphics/trigger", 0, WINDOW_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT);
    cpp_item_set_size("bottom_line", WINDOW_WIDTH, WINDOW_HEIGHT);
end
---------------------------------------------------------------
function clApplication:SetCursor(cursor_path)
    SetCustomCursor(cursor_path)
    if(cursor_path == "graphics/interface/cursors/cursor") then
        SetSystemCursor("")
    else
        SetSystemCursor(cursor_path.."_s")
    end 
end
---------------------------------------------------------------
messenger_table =
{
	on_click = "",
	on_mouse_up = "",
	on_mouse_enter = "",
	on_mouse_leave = "",
	on_mouse_over = "",
	on_mouse_right_down = "",
	on_mouse_right_up = "",
	on_button_down = "",
	on_key_down = "",
	mouse_X = 0,
	mouse_Y = 0,
	mouse_delta_X = 0,
	mouse_delta_Y = 0,
};

MakeDialog
{
	Application
	{
		name = "HO Project",
		x = 0,
		y = 0,
	}
}

clApplication:LoadScreen();