GameScreen.inv_fun = {}
GameScreen.inv_fun["screwdriver_001"] = function()
    Inventory:ClueAndHide()
    DragObject:CreateObj(OBJ_TYPE.ACTIVE, "drag_screw", "graphics/minigames/01_patch/screwdriver_minigame", 100, 100);
    Inventory:remove_tool("screwdriver_001")
    Inventory:UpdateData();
    Inventory:add_tool("screwdriver_001", true)

    CurrentScreen.drag_obj = "drag_screw";
    clApplication.lock_cursor = false;
end

function GameScreen:RestoreScreen()

    if(GetState("screen_mg_001_patch", "completed") == "true") then
        ItemSetPosition("patch", -518, 88)
        for i = 1, 5 do
            ItemRemove(self.name, "screw_0"..i)
        end  
        DelayedExec(1.0, function()
            clApplication:LoadLocation("002_hall")
        end, 113)
    else
        for i = 1, 5 do
            if(GetState(CURRENT_SCREEN, "screw_"..i) == "remove") then
                self.screw_apply_flags[i] = true
                ItemRemove(self.name, "screw_0"..i)
                if(self:InMinigameCompleted()) then
                    self:OnFinish();
                end
            end
        end    
    end
end

function GameScreen:InitScreen()
    self:LoadScreen()       
    
     self.screw_apply_flags = {}
     for i = 1, 5 do
        self.screw_apply_flags[i] = false
         local screw_apply = function()
            SetState(CURRENT_SCREEN, "screw_"..i, "remove")
            ItemSetType("screw_0"..i, OBJ_TYPE.NO_ACTIVE)
            self:SoundPlay("sound/screw_0"..math.random(3)..".ogg")
            DelayedExec(1.2, function()
                self:SoundPlay("sound/screwdrop_0"..math.random(3)..".ogg")
            end, 113)
            self.screw_apply_flags[i] = true
            ItemRotate("screw_0"..i, -M_PI, 0.5)
            ItemChangeSize("screw_0"..i, 1.2, 1.2, 0.5)
            ItemMove("screw_0"..i, 0.0, 1500, 1.0, 0.5)
            --ItemChangeOpacity("screw_0"..i, 0.0, 0.3, 0.)
            
            if(self:InMinigameCompleted()) then
                self:OnFinish();
            end
         end
         self:AddToolApplyZone("screw_0"..i, "screwdriver_001", screw_apply, false);
     end
     
     --Inventory:add_tool("screwdriver_001")
     
     if(GetState(CURRENT_SCREEN, "tutor") == "") then
         DelayedExec(0.1, function()
            SetState(CURRENT_SCREEN, "tutor", "1")
            local end_fun = function()
                 HUDScreen:SetMiniGameMode() 
            end
            CurrentScreen:ShowTutorial(3, end_fun);
            clApplication:SetCursor("graphics/interface/cursors/cursor")
         end, 113)
     else
        HUDScreen:SetMiniGameMode() 
     end
     
     MusicPlay({"sound/ambient/01_04_31_ambient.ogg", "sound/music/hospital.ogg"} , 1) 
     
     self:RestoreScreen()
     HUDScreen:CreateBackButton("001_ward")
end

function GameScreen:OnSkip()
    self:SoundPlay("sound/screw_0"..math.random(3)..".ogg")
    DelayedExec(1.2, function()
        self:SoundPlay("sound/screwdrop_0"..math.random(3)..".ogg")
    end, 113)
    DelayedExec(1.0, function()
        self:SoundPlay("sound/screwdrop_0"..math.random(3)..".ogg")
    end, 113)
    DelayedExec(1.1, function()
        self:SoundPlay("sound/screwdrop_0"..math.random(3)..".ogg")
    end, 113)
    for i = 1, 5 do
        if(self.screw_apply_flags[i] == false) then
            self.screw_apply_flags[i] = true
            ItemRotate("screw_0"..i, -M_PI, 0.5)
            ItemChangeSize("screw_0"..i, 1.2, 1.2, 0.5)
            ItemMove("screw_0"..i, 0.0, 1500, 1.0, 0.5)
           
            if(self:InMinigameCompleted()) then
                self:OnFinish();
            end
        end
    end  
end

function GameScreen:OnFinish()
    HUDScreen:SetInvntoryOnlyMode()
    HUDScreen:RemoveBackButton();
    
    SetState("screen_mg_001_patch", "completed", "true")
    
    DelayedExec(2.0, function()
        self:SoundPlay("sound/patch_slide.ogg")
    end, 113)
    Inventory:ActiveToolApply()
    
    HUDScreen:RemoveItem("drag_screw")
    Inventory:remove_tool("screwdriver_001")
    
    DelayedExec(1.5, function()
        ItemMove("patch", -900, 0.0, 3.0);
    
        DelayedExec(4.0, function()
            clApplication:LoadLocation("002_hall")
        end, 113)
    end, 113)
end

function GameScreen:InMinigameCompleted()   
    for i = 1, 5 do
        if(self.screw_apply_flags[i] == false) then
            return false;
        end
    end
    
    return true
end

function GameScreen:on_hide_drag_object()
    if(self.drag_obj) then
        SetVisibleCursor(true)
        ItemSetOpacity(self.drag_obj, 0.0)
    end
end

function GameScreen:Update()
    if(self.drag_obj) then
        ItemSetPosition(self.drag_obj, messenger_table.mouse_X, messenger_table.mouse_Y);
    end
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "01_patch", "graphics/minigames/01_patch/01_patch", 0, 0);
	
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "patch", "graphics/minigames/01_patch/patch", 418, 88);
	self:CreateObj(OBJ_TYPE.ACTIVE, "drawer_cover", "graphics/minigames/01_patch/drawer_cover", 0, 439);
	self:CreateObj(OBJ_TYPE.ACTIVE, "screw_05", "graphics/minigames/01_patch/screw_05", 1121, 632);
    self:CreateObj(OBJ_TYPE.ACTIVE, "screw_04", "graphics/minigames/01_patch/screw_04", 1056, 115);
    self:CreateObj(OBJ_TYPE.ACTIVE, "screw_03", "graphics/minigames/01_patch/screw_03", 494, 301);
    self:CreateObj(OBJ_TYPE.ACTIVE, "screw_02", "graphics/minigames/01_patch/screw_02", 503, 496);
    self:CreateObj(OBJ_TYPE.ACTIVE, "screw_01", "graphics/minigames/01_patch/screw_01", 471, 874);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "left_line", "graphics/trigger", -WINDOW_WIDTH, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
	
    clScreenManager:add_screen("hud") 
    HUDScreen:SetInvntoryOnlyMode()
end