GameScreen.textures_path = "graphics/screens/09_generator/location/"
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
GameScreen.hint_table = {
    {obj = {"handle_hint", 492, 648}, fun = function() if(GetState(CURRENT_SCREEN, "state") == "handle_push") then return true end end},
    {obj = {"portal_hint", 1497, 357}, fun = function() if(GetState(CURRENT_SCREEN, "state") == "finish") then return true end end},
}
------------------------------------------------------------------------------------------------------
function GameScreen:RestoreScene()
    for found_item in string.gmatch(GetState(CURRENT_SCREEN, "found_obj"), "[_%a%d]+") do
		ItemRemove(self.name, found_item)
		local object = self:get_item_by_name(found_item);
		if(object) then
		    self:CreateObj(OBJ_TYPE.NO_ACTIVE, object.affix[1], self.textures_path..object.affix[1], object.affix[2].x, object.affix[2].y); 
		end
    end
    
    if(pc_sound) then
        StopSound(pc_sound)
        pc_sound = nil;
    end
    if(GetState(CURRENT_SCREEN, "state") == "") then
        local ho_end = function()
            HUDScreen:ShowTips({path = self.textures_path.."Text_09a", x = 434, y = 1128})
            HUDScreen:SetDefaultMode()
            SetState(CURRENT_SCREEN, "state", "handle_push") 
            self:SetItemCursor("handle_hand", "hand")
        end
        
        local start_ho = function()
             self:SetMiniGame(mgt_find_and_apply)   
            self.mini_game:Init(self.item_list, ho_end, true);
        end
        HUDScreen:ShowTips({path = self.textures_path.."text_09", x = 316, y = 1130, end_fun = start_ho})
              
    elseif(GetState(CURRENT_SCREEN, "state") == "handle_push") then
        HUDScreen:SetDefaultMode()
        self:SetItemCursor("handle_hand", "hand")
    else
        HUDScreen:SetDefaultMode()
        --HUDScreen:SetInvntoryOnlyMode()
        ItemSetOpacity("ventilator_anim", 1.0)
        AnimationPlay("ventilator_anim", "main")
        ItemSetOpacity("ventilator_affix", 0.0)
        ItemSetOpacity("fan_belt_affix", 0.0)
        ItemSetOpacity("handle_down", 1.0)
        ItemSetOpacity("handle_up", 0.0)
        ItemSetDepth("handle_down", LAYERS.FRONT_MOST)
         generator_sound = SoundPlay("sound/generator_idle.ogg", true)
        --end)
        ItemRemove(self.name, "handle_hand")
        --clApplication:LoadLocation("008_portal")
    end
   
    ItemSetDepthByOther("fan_belt_affix", "crystal_09_part")
end
------------------------------------------------------------------------------------------------------
function GameScreen:get_item_by_name(item_name)
    for key, value in pairs(self.item_list) do
        if(value.obj[1] == item_name) then
            return value
        end
    end
    
    return nil;
end
------------------------------------------------------------------------------------------------------
function GameScreen:InitScreen()
    self:LoadScreen()
    
    local belt_apply_fun = function()
        ItemSetDepthByOther("fan_belt_affix", "crystal_09_part")
    end
    
    self.item_list = {
        {obj = {"tube", {x = 375, y = 0}}, affix = {"tube_affix", {x = 1019, y = 0}}},
        {obj = {"ventilator", {x = 1237, y = 511}}, affix = {"ventilator_affix", {x = 1240, y = 552}}},
        {obj = {"fan_belt", {x = 671, y = 667}}, affix = {"fan_belt_affix", {x = 1202, y = 680}}, finding_fun = belt_apply_fun},
        {obj = {"fluted_rubber", {x = 813, y = 161}}, affix = {"fluted_rubber_affix", {x = 859, y = 821}}},
        {obj = {"red_cylinder", {x = 858, y = 354}}, affix = {"red_cylinder_affix", {x = 909, y = 724}}},
        {obj = {"indicator", {x = 514, y = 713}}, affix = {"indicator_affix", {x = 522, y = 724}}},
        {obj = {"metal_cover", {x = 568, y = 188}}, affix = {"metal_cover_affix", {x = 759, y = 504}}},
        {obj = {"green_cover", {x = 320, y = 680}}, affix = {"green_cover_affix", {x = 758, y = 692}}},
        {obj = {"plug", {x = 300, y = 610}}, affix = {"plug_affix", {x = 1016, y = 611}}},
        {obj = {"cable", {x = 636, y = 368}}, affix = {"cable_affix", {x = 630, y = 659}}},
        {obj = {"red_cover", {x = 542, y = 515}}, affix = {"red_cover_affix", {x = 704, y = 866}}},
        {obj = {"filter", {x = 162, y = 436}}, affix = {"filter_affix", {x = 873, y = 958}}},
    }
  
    self:RestoreScene();
    
    self:SetItemSound("chain_01", "sound/chain.ogg");
    self:SetItemSound("chain_01", "sound/chain.ogg");
    self:SetItemSound("jerrycan", "sound/tube_hit_01.ogg");
    self:SetItemSound("oil", "sound/oil.ogg");
    self:AddHiddenCrystal("crystal_09_part")
    
    MusicPlay({"sound/ambient/07_09_ambient.ogg", "sound/ambient/11_ambient.ogg"}, 9) 

    self:CreateLocationPortal("portal_hand", "008_portal", "hand")
end   
------------------------------------------------------------------------------------------------------
function GameScreen:onObjectClick(item_name)
    if(item_name == "handle_hand" and GetState(CURRENT_SCREEN, "state") == "handle_push") then
        --HUDScreen:SetInvntoryOnlyMode()
        SetState("screen_008_portal", "state", "museum")
        SetState(CURRENT_SCREEN, "state", "finish") 
        ItemSetOpacity("ventilator_anim", 1.0)
        AnimationPlay("ventilator_anim", "main")
        ItemSetOpacity("ventilator_affix", 0.0)
        ItemSetOpacity("fan_belt_affix", 0.0)
        ItemChangeOpacity("handle_down", 1.0, 0.3)
        ItemChangeOpacity("handle_up", 0.0, 0.3)
        SoundPlay("sound/generator_handle.ogg")
        ItemSetDepth("handle_down", LAYERS.FRONT_MOST)
        --DelayedExec(0.3, function()
        generator_sound2 = SoundPlay("sound/generator_start.ogg")
        --end, 113)
        ItemRemove(self.name, "handle_hand")
        DelayedExec(5.0, function()
            StopSound(generator_sound2, 0.3)
            generator_sound = SoundPlay("sound/generator_idle.ogg", true)
            --clApplication:LoadLocation("008_portal")
        end, 113)
    end
    if(item_name == "portal_hand") then
        StopSound(generator_sound, 0.3)
        StopSound(generator_sound2, 0.3)        
    end
end  
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "09_background", "graphics/screens/09_generator/location/09_generator", 0, 0);

    self:CreateObj(OBJ_TYPE.TRIGGER, "oil", "graphics/screens/09_generator/location/oil", 913, 1073);
    self:CreateObj(OBJ_TYPE.TRIGGER, "jerrycan", "graphics/screens/09_generator/location/jerrycan", 266, 767);
    self:CreateObj(OBJ_TYPE.TRIGGER, "chain_02", "graphics/screens/09_generator/location/chain_02", 1320, 0);
    self:CreateObj(OBJ_TYPE.TRIGGER, "chain_01", "graphics/screens/09_generator/location/chain_01", 1458, 0);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "crystal_09_part", "graphics/screens/09_generator/location/crystal_09_part", 5, 468);

    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "tube", "graphics/screens/09_generator/location/tube_part", 367, 692);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "ventilator", "graphics/screens/09_generator/location/ventilator_part", 457, 829);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fan_belt", "graphics/screens/09_generator/location/fan_belt_part", 684, 1003);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fluted_rubber", "graphics/screens/09_generator/location/fluted_rubber_part", 7, 1053);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "red_cylinder", "graphics/screens/09_generator/location/red_cylinder_part", 870, 368);  
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "handle_up", "graphics/screens/09_generator/location/handle_up", 506, 662);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "indicator", "graphics/screens/09_generator/location/indicator_part", 1172, 1005);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "metal_cover", "graphics/screens/09_generator/location/metal_cover_part", 582, 202);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "green_cover", "graphics/screens/09_generator/location/green_cover_part", 353, 955);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plug", "graphics/screens/09_generator/location/plug_part", 307, 1085);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "cable", "graphics/screens/09_generator/location/cable_part", 1086, 156);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "red_cover", "graphics/screens/09_generator/location/red_cover_part", 555, 529);    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "filter", "graphics/screens/09_generator/location/filter_part", 179, 451);
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "tube_where", "graphics/screens/09_generator/location/tube_where", 1019, 0);
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "fan_belt_where", "graphics/screens/09_generator/location/fan_belt_where", 1214, 680);  
    self:CreateObj(OBJ_TYPE.TRIGGER, "ventilator_where", "graphics/screens/09_generator/location/ventilator_where", 1254, 674); 
    self:CreateObj(OBJ_TYPE.TRIGGER, "fluted_rubber_where", "graphics/screens/09_generator/location/fluted_rubber_where", 862, 823);
    self:CreateObj(OBJ_TYPE.TRIGGER, "red_cylinder_where", "graphics/screens/09_generator/location/red_cylinder_where", 917, 734);
    self:CreateObj(OBJ_TYPE.TRIGGER, "indicator_where", "graphics/screens/09_generator/location/indicator_where", 522, 724);
    self:CreateObj(OBJ_TYPE.TRIGGER, "metal_cover_where", "graphics/screens/09_generator/location/metal_cover_where", 759, 504);
    self:CreateObj(OBJ_TYPE.TRIGGER, "green_cover_where", "graphics/screens/09_generator/location/green_cover_where", 758, 692);
    self:CreateObj(OBJ_TYPE.TRIGGER, "plug_where", "graphics/screens/09_generator/location/plug_where", 1016, 611);  
    self:CreateObj(OBJ_TYPE.TRIGGER, "cable_where", "graphics/screens/09_generator/location/cable_where", 633, 627);
    self:CreateObj(OBJ_TYPE.TRIGGER, "red_cover_where", "graphics/screens/09_generator/location/red_cover_where", 704, 866);
    self:CreateObj(OBJ_TYPE.TRIGGER, "filter_where", "graphics/screens/09_generator/location/filter_where", 873, 958);
    
    
    self:CreateAnim(OBJ_TYPE.NO_ACTIVE, "ventilator_anim", "graphics/screens/09_generator/location/ventilator_anim", 1210, 545);
    ItemSetOpacity("ventilator_anim", 0.0)

    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "handle_down", "graphics/screens/09_generator/location/handle_down", 481, 729);
    ItemSetOpacity("handle_down", 0.0)
    self:CreateObj(OBJ_TYPE.TRIGGER, "handle_hand", "graphics/screens/09_generator/location/handle_hand", 506, 662);
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "portal_hand", "graphics/screens/09_generator/location/portal_hand", 1512, 372);
    
    clScreenManager:add_screen("hud") 
    HUDScreen:SetInvntoryOnlyMode()
end