require("scripts/framework/HiddenObjectHelper")

DefaultScreen = {} 

DefaultScreen.move_triggers = {}
DefaultScreen.hidden_objects = {}
DefaultScreen.active_zone = {}
DefaultScreen.active_objects = {}
DefaultScreen.leave_fun_list = {}


function DefaultScreen:Init()
    --SetCustomCursor("cursors/main/cursor_main");
    self.LoadScene();    
end

function DefaultScreen:RestoreScene()
    --[[for item_name in string.gmatch(GetState(CURRENT_SCREEN, "found_objects"), "[_%a%d]+") do
		CurrentScreen:RemoveItem(item_name)
		CurrentScreen:RemoveItem(item_name.."_s")
    end--]]
end

function DefaultScreen:LoadScene()
	ApplicationAddWindow("black_back");
	
	CreateSprite("black_back", OBJ_TYPE.ACTIVE_ALL, "big_trigger", "graphics/trigger", 0, 0);
	ItemSetSize("big_trigger", 1024, 768);
end

function DefaultScreen:OnClick(_item_name)
    if(self.game_pause) then return end    
    if(clInventory and clInventory.DragObject ~= nil) then return end

    --[[if(clComics.state == "active") then
        clComics:OnClick(_item_name)
        return
    end--]]
    
	for key, value in pairs(DefaultScreen.active_zone) do
        if(value.trigger == _item_name and clInventory:GetDragObject() ~= nil and clInventory:GetDragObject() == GameObject[value.obj_name]) then
			value.fun_rezalt(gApplication.MouseX, gApplication.MouseY);
        end
    end
    
	for key, value in pairs(DefaultScreen.move_triggers) do
        if(value.name == _item_name) then
		    CGame:ReLoadScreen(value.scene_path, value.changer);
            return;
        end
    end
    
    for key, value in pairs(DefaultScreen.active_objects) do
        if(value.item_name == _item_name) then
            if(value.as_award) then
                SetState(FOUND_OBJECTS, value.object, "true")
                AwardList:Show(GameObject[value.object]);
                
                if(CurrentScreen.TaskSolved) then
                    CurrentScreen:TaskSolved(value.object.." found");
                end
            else
                SetState(FOUND_OBJECTS, value.object, "true")
                
                if(CurrentScreen.TaskSolved) then
                    CurrentScreen:TaskSolved(value.object.." found");
                end
                --InGameMenuWindow:InventoryEnabled(false)
                InGameMenuWindow.inv_enabled = false;
                InGameMenuWindow:ShowInventory()
                InGameMenuWindow:CreateObj(OBJ_TYPE.NO_ACTIVE, "found_object", GameObject[value.object].obj_path, ItemGetX(value.item_name) + ItemGetWidth(value.item_name) / 2, ItemGetY(value.item_name ) + ItemGetHeight(value.item_name) / 2);
                DelayedExec(inv_panel_moving_time, function()
                    clInventory:AddObj("found_object", GameObject[value.object], ItemGetX(value.item_name) + ItemGetWidth(value.item_name) / 2, ItemGetY(value.item_name) + ItemGetHeight(value.item_name) / 2)
                end)
                DelayedExec(1.0, function()
                    AwardList:UnLoadScreen()
                    InGameMenuWindow.inv_enabled = true;
                    InGameMenuWindow:UpdateInventory()
	                --InGameMenuWindow:InventoryEnabled(true)
	                InGameMenuWindow.mouse_state = "return_obj"
                    DelayedExec(0.5, function()
                        if(InGameMenuWindow.mouse_state == "return_obj") then
                            InGameMenuWindow.mouse_state = "no_mouse";
                            InGameMenuWindow:HideInventory()
                        end
                    end)
                end)
            end
            CurrentScreen:RemoveItem(_item_name, 0.2);
            CurrentScreen:RemoveItem(_item_name.."_s", 0.2);
            SetState(CURRENT_SCREEN, "found_objects", GetState(CURRENT_SCREEN, "found_objects").." ".._item_name)
            SetCustomCursor(CURSOR_DEFAULT);
            return;
        end
    end
    
    for key, value in pairs(DefaultScreen.active_zone) do
        if(value.trigger == _item_name and value.enable) then
            MessageBox:Show(value.phrase_id)
        end
    end
    
    if(CurrentScreen.mini_game) then CurrentScreen.mini_game:OnClick(_item_name) end
end

function DefaultScreen:OnHint()
    if(CurrentScreen.mini_game and CurrentScreen.mini_game.OnHint) then
        if(CurrentScreen.mini_game:OnHint()) then
            return true
        end
    end
    if(CurrentScreen.OnHint) then
        if(CurrentScreen:OnHint()) then
            return true
        end
    end
    
    return false
end

function DefaultScreen:OnKeyDown(key)
	if(key == VK_SPACE) then
		skip_fun();
	end
	if(key == VK_TAB) then
	    show_all_objects();
	end
	if(key == VK_L) then
	    show_all_objects_name();
	end
	if(key == VK_K) then
	    show_objects_name_step_by_step();
	end
end

function DefaultScreen:OnMouseMove(item_name)
end

function DefaultScreen:OnMouseUp(item_name)
	if(CurrentScreen.mini_game and CurrentScreen.mini_game.OnMouseUp) then CurrentScreen.mini_game:OnMouseUp(_item_name) end
end

function DefaultScreen:OnMouseEnter(_item_name)
    if(self.game_pause) then return end
    if(clInventory and clInventory.DragObject ~= nil) then return end
    --if(clComics.state == "active") then return end
    
    for key, value in pairs(DefaultScreen.move_triggers) do
        if(value.name == _item_name) then
            SetCustomCursor(value.cursor_path);
            return;
        end
    end
    
    for key, value in pairs(DefaultScreen.active_objects) do
        if(value.item_name == _item_name) then
            SetCustomCursor(value.cursor);
            return;
        end
    end
    
    for key, value in pairs(DefaultScreen.active_zone) do
        if(value.trigger == _item_name and value.cursor and value.enable) then
            SetCustomCursor(CURSOR_ACTION);
            return;
        end
    end
    
    if(_item_name == "big_trigger") then
        SetCustomCursor(CURSOR_DEFAULT);
    end
end

function DefaultScreen:OnMouseLeave(_item_name)
    if(self.game_pause) then return end
    if(clInventory and clInventory.DragObject ~= nil) then return end
    --if(clComics.state == "active") then return end
    
    for key, value in pairs(DefaultScreen.move_triggers) do
        if(value.name == _item_name) then
            SetCustomCursor(CURSOR_DEFAULT);
            return;
        end
    end
    
    for key, value in pairs(DefaultScreen.active_objects) do
        if(value.item_name == _item_name) then
            SetCustomCursor(CURSOR_DEFAULT);
            return;
        end
    end
    
    for key, value in pairs(DefaultScreen.active_zone) do
        if(value.trigger == _item_name) then
            SetCustomCursor(CURSOR_DEFAULT);
            return;
        end
    end
end

function DefaultScreen:OnButtonDown(item_name)
     if(CurrentScreen.mini_game and CurrentScreen.mini_game.OnButtonDown) then
        CurrentScreen.mini_game:OnButtonDown();
    end
    
    AwardList:OnButtonDown(item_name)
end

function DefaultScreen:Update()
	if(CurrentScreen.mini_game and CurrentScreen.mini_game.OnMouseUp) then CurrentScreen.mini_game:Update() end
end

function DefaultScreen:Clean()
    if(clHint.counter ~= nil) then
        InGameMenuWindow:RemoveItem("hint_"..clHint.counter)
        clHint.counter = nil
    end
    
    for key, value in pairs(DefaultScreen.leave_fun_list) do
        value();
    end
    
    MessageBox:UnLoadScreen();
    
    DefaultScreen.move_triggers = {}
    DefaultScreen.hidden_objects = {}
    DefaultScreen.active_zone = {}
    DefaultScreen.active_objects = {}
    DefaultScreen.leave_fun_list = {}
 
    clHint:ClearHintObjectsTable();
    
    self.current_obj_step_by_step = nil
    self.show_all_item_list = nil
end

function DefaultScreen:AddSceneLeaveFun(fun)
	table.insert(DefaultScreen.leave_fun_list, fun)
end

function DefaultScreen:IsItemFound(item_name)
    for found_item_name in string.gmatch(GetState(CURRENT_SCREEN, "found_objects"), "[_%a%d]+") do
		if(found_item_name == item_name) then
		    return true
		end
    end
    
    return false;
end

function InitHiddenObject(item_name, _instance)
	hidden_obj = {}
	hidden_obj.name = item_name;
	hidden_obj.instance = _instance;
	table.insert(DefaultScreen.hidden_objects, hidden_obj);
end

function CreateActiveZone(trigger, phrase_id, particle, cursor)
    --ItemSetOpacity(trigger, 1.0)
    
	local zone = {}
	zone.trigger = trigger;
	zone.phrase_id = phrase_id;
	zone.particle = particle;
	zone.cursor = cursor;
	zone.enable = true
	table.insert(DefaultScreen.active_zone, zone);
	
	if(zone.particle) then
	    zone.particle_id = CreateZoneParticles(zone.trigger, "fx/action_zone.lua")
	    DelayedExec(math.random(1000) / 100 + 5, function() ScriptGFX(CurrentScreen:GetName(), zone.particle_id, "gActive = true") end)
	end
end

function RemoveActiveZone(zone_name)
    for key, value in pairs(DefaultScreen.active_zone) do
        if(value.trigger == zone_name) then
            if(value.particle_id) then
                RemoveGFX(CurrentScreen:GetName(), value.particle_id)
            end
            table.remove(DefaultScreen.active_zone, key)
            return
        end 
    end
end

function ActiveZoneEnable(trigger, flag)
    for key, value in pairs(DefaultScreen.active_zone) do
        if(value.trigger == trigger) then
            value.enable = flag;
            if(value.enable == false) then
                if(value.particle_id) then
                    ItemSetEnabled(value.trigger, false)
                    RemoveGFX(CurrentScreen:GetName(), value.particle_id)
                end
            else
                if(value.particle) then
                    value.particle_id = CreateZoneParticles(value.trigger, "fx/action_zone.lua")
                    DelayedExec(math.random(1000) / 100 + 5, function() ScriptGFX(CurrentScreen:GetName(), zone.particle_id, "gActive = true") end)
                end
                ItemSetEnabled(value.trigger, true)
            end
            return;
        end
    end
end

function ActiveZoneSetText(trigger, text_id)
    for key, value in pairs(DefaultScreen.active_zone) do
        if(value.trigger == trigger) then
            value.phrase_id = text_id;
            return;
        end
    end
end

function LockMoveZone(item_name, task, text)
	for key, value in pairs(DefaultScreen.move_triggers) do
        if(value.name == item_name) then
			value.lock_task = task;
			value.lock_task_text = text;
        end
	end 
end

function CreateZoneParticles(zoneName, particlesName)
	local id = CreateGFX(CurrentScreen:GetName(), particlesName, 0, 0, 130)
	AttachGFX(CurrentScreen:GetName(), id, zoneName, 1, 0, 0, false)

	local itemw = ItemGetWidth(zoneName);
	local itemh = ItemGetHeight(zoneName);
	local w = itemw / 4
	local h = itemh / 4

	AdjustGFX(CurrentScreen:GetName(), id, {rect = {-w, -h, w, h}})
	
	SetLayerDepthGFX(CurrentScreen:GetName(), PARTICLE_ACTIVE_ZONE_ID, zoneName, 1);
	
	return id;
end

function CreateActiveObject(item_name, object, particlesName, cursor, as_award)
    if(GetState(FOUND_OBJECTS, object) == "true") then 
        ItemRemove(CurrentScreen:GetName(), item_name)
        ItemRemove(CurrentScreen:GetName(), item_name.."_s")
        return
    end

    if(cursor == nil) then cursor = CURSOR_HAND end
    
    local active_object = {}
    active_object.item_name = item_name;
    active_object.object = object;
    active_object.particle = particlesName;
    active_object.as_award = as_award;
    active_object.cursor = cursor;
    table.insert(DefaultScreen.active_objects, active_object);
end

-------------------------------------------------------------------------------------------
function on_scene_open(scene_name)
    DefaultScreen.states[scene_name] = "opened"
end

function on_scene_close(scene_name)
    DefaultScreen.states[scene_name] = "closed"
end

function on_scene_solve(scene_name)
    DefaultScreen.states[scene_name] = "solved"
end

function is_scene_open(scene_name)
    if(DefaultScreen.states[scene_name] == "opened") then
        return true
    end
    
    return false
end

function is_scene_closed(scene_name) 
   --[[ if(DefaultScreen.states[scene_name] == "closed" or DefaultScreen.states[scene_name] == nil) then
        return true
    end--]]
    
    return false
end

function is_scene_solved(scene_name)
   --[[ if(DefaultScreen.states[scene_name] == "solved") then
        return true
    end--]]
   
    return false
end

function get_scene_state(scene_name)
    return DefaultScreen.states[scene_name]
end
-------------------------------------------------------------------------------------------
