GameScreen.inv_fun = {}
GameScreen.inv_fun["safety_fuse_004"] = function()
    clApplication.lock_cursor = false;
end


function GameScreen:RestoreScene()
    
    local fuses_names = {"fuse_b1", "fuse_t8", "fuse_e6", "fuse_t6", "fuse_e3", "fuse_m5", "fuse_a2",
            "fuse_v4", "fuse_o7", "fuse_l2", "fuse_e1", "fuse_s3", "fuse_a5", "fuse_r8", "fuse_e4", "fuse_n7"
        }
        
    for key, value in pairs(fuses_names) do
        if(GetState(CURRENT_SCREEN, value) ~= "") then
            ItemSetPosition(value, ItemGetX(GetState(CURRENT_SCREEN, value)) - 5, ItemGetY(GetState(CURRENT_SCREEN, value)))
            self:set_start_pos(value, ItemGetX(GetState(CURRENT_SCREEN, value)) - 5, ItemGetY(GetState(CURRENT_SCREEN, value)))
        end
    end    
    
    
    if(self:is_basement_completed()) then
        self.basement = true;
        ItemSetOpacity("lamp_basement", 1.0)
    end
    if(self:is_elevator_completed()) then
        self.elevator = true;
        ItemSetOpacity("lamp_elevator", 1.0)
    end
     if(self:IsMinigameCompleted()) then
        SetState("screen_005_security", "state", "mg2")
        DelayedExec(2.0, function()
            clApplication:LoadLocation("005_security")
        end, 113)
    end
end

function GameScreen:InitScreen()
    self:LoadScreen()       
    
    self.basement = false;
    self.elevator = false;
    
    self.drag_obj = nil;
    --Inventory:add_tool("safety_fuse_004")
    
    self:SetItemSound("fuse_h1_wrong", "sound/interface/wrong.ogg");
    self:SetItemSound("fuse_a2_wrong", "sound/interface/wrong.ogg");
    self:SetItemSound("fuse_l3_wrong", "sound/interface/wrong.ogg");
    self:SetItemSound("fuse_l4_wrong", "sound/interface/wrong.ogg");
    
    if(GetState(CURRENT_SCREEN, "switchboard") == "active")then
        self:InitFuses();
        self:RestoreScene();
        HUDScreen:SetMiniGameMode()
    else
        ItemSetOpacity("hint", 0.0)
        local fuses_names = {"fuse_b1", "fuse_t8", "fuse_e6", "fuse_t6", "fuse_e3", "fuse_m5", "fuse_a2",
            "fuse_v4", "fuse_o7", "fuse_l2", "fuse_e1", "fuse_s3", "fuse_a5", "fuse_r8", "fuse_e4"
        }
        for key, value in pairs(fuses_names) do
            ItemSetOpacity(value, 0.0)
            ItemSetPosition(value, -400, -400)
        end
        
        DelayedExec(0.5, function()
            
            local end_tip = function()
                SetState("screen_005_security", "state", "ho")
                --DelayedExec(1.0, function()
                --    clApplication:LoadLocation("005_security")
                --end, 113)
            end
            HUDScreen:ShowTips({path = "graphics/minigames/05_switchboard/text_05a", x = 452, y = 1130, end_fun = end_tip})
        end, 113)
        
        self:InitFuses();
        self:RestoreScene();

    end
    
    MusicPlay({"sound/ambient/05_ambient.ogg", "sound/music/hospital.ogg"}, 5) 
    
    HUDScreen:CreateBackButton("005_security")
end

function GameScreen:InitFuses()
    self.minigame_active = true
    
    local fuses_names = {"fuse_b1", "fuse_t8", "fuse_e6", "fuse_t6", "fuse_e3", "fuse_m5", "fuse_a2",
        "fuse_v4", "fuse_o7", "fuse_l2", "fuse_e1", "fuse_s3", "fuse_a5", "fuse_r8", "fuse_e4", "fuse_n7"
    }
    
    self.fuses = {}
    for key, value in pairs(fuses_names) do
        self.fuses[key] = {}
        self.fuses[key].name = value;
        self.fuses[key].start_x = ItemGetX(value);
        self.fuses[key].start_y = ItemGetY(value);
        self.fuses[key].active = true;
    end
end

function GameScreen:OnSkip()
    ItemSetOpacity("skip", 0.0)
    
    for key, value in pairs(self.fuses) do
        if(value.name == "fuse_n7") then
            Inventory:remove_tool("safety_fuse_004")
            ItemSetOpacity("fuse_n7", 1.0)
            ItemSetPosition(value.name, ItemGetX(value.name.."_insert"), ItemGetY(value.name.."_insert"))
            SetState(CURRENT_SCREEN, "apply_obj", GetState(CURRENT_SCREEN, "apply_obj").." "..value.name)
        else
            ItemMoveTo(value.name, ItemGetX(value.name.."_insert") - 5, ItemGetY(value.name.."_insert"), 0.5)
            SetState(CURRENT_SCREEN, "apply_obj", GetState(CURRENT_SCREEN, "apply_obj").." "..value.name)
        end
    end
    
    if(Inventory:GetActiveObj() == "safety_fuse_004") then
        Inventory:ActiveToolApply()
        Inventory.lock_drag = nil;
    end
    
    DelayedExec(0.5, function()
        SoundPlay("sound/lamp_on.ogg")
        
        ItemSetOpacity("lamp_basement", 1.0)
        ItemSetOpacity("lamp_elevator", 1.0)
        SetState("screen_005_security", "state", "mg2")
        DelayedExec(2.0, function()
            clApplication:LoadLocation("005_security")
        end, 113)
    end)
end

function GameScreen:is_basement_completed()
    local basement_word = {"fuse_b1", "fuse_a2", "fuse_s3", "fuse_e4", "fuse_m5", "fuse_e6", "fuse_n7", "fuse_t8"}
    
    for key, value in pairs(basement_word) do
        if(GetState(CURRENT_SCREEN, value) == "" or value.."_insert" ~= GetState(CURRENT_SCREEN, value)) then
            return false;
        end
    end   
    return true
end

function GameScreen:is_elevator_completed()
    local elevator_word = {"fuse_e1", "fuse_l2", "fuse_e3", "fuse_v4", "fuse_a5", "fuse_t6", "fuse_o7", "fuse_r8"}
    
    for key, value in pairs(elevator_word) do
        if(GetState(CURRENT_SCREEN, value) == "" or value.."_insert" ~= GetState(CURRENT_SCREEN, value)) then
            return false;
        end
    end   
    return true
end

function GameScreen:IsMinigameCompleted()
    if(self:is_basement_completed() and self:is_elevator_completed()) then
        return true;
    else
        return false;
    end
end

function GameScreen:onObjectClick(item_name)
    if(not self.minigame_active) then return end
    
    if(string.find(item_name, "_insert") and Inventory:GetActiveObj() == "safety_fuse_004") then
        Inventory:ActiveToolApply()
        SetState(CURRENT_SCREEN, "fuse_n7", item_name)
        ItemSetPosition("fuse_n7", ItemGetX(item_name) - 5, ItemGetY(item_name))
        ItemSetOpacity("fuse_n7", 1.0)
        local fuse = self:get_fuse_by_name("fuse_n7")
        if(fuse) then
            fuse.start_x = ItemGetX("fuse_n7") - 5;
            fuse.start_y = ItemGetY("fuse_n7");
        end
        Inventory.lock_drag = true;
        self:SetFuse()
    elseif(self.drag_obj) then
        if(string.find(item_name, "_insert")) then
            ItemMoveTo(self.drag_obj.name, ItemGetX(item_name) - 5, ItemGetY(item_name), 0.2)
            self.drag_obj.start_x = ItemGetX(item_name) - 5;
            self.drag_obj.start_y = ItemGetY(item_name);
            SetState(CURRENT_SCREEN, self.drag_obj.name, item_name)
            ItemSetType(self.drag_obj.name, OBJ_TYPE.ACTIVE_ALL)   
            ItemAttachChildInCurPos(self.name, self.drag_obj.name)
            DragObject.active = false
            self:SetFuse()
        elseif(self:get_fuse_by_name(item_name)) then
            fuse = self:get_fuse_by_name(item_name)
            local pos_x, pos_y = self.drag_obj.start_x, self.drag_obj.start_y;
            self.drag_obj.start_x, self.drag_obj.start_y = fuse.start_x, fuse.start_y;
            fuse.start_x, fuse.start_y = pos_x, pos_y;
            local temp = GetState(CURRENT_SCREEN, self.drag_obj.name)
            SetState(CURRENT_SCREEN, self.drag_obj.name, GetState(CURRENT_SCREEN, fuse.name))
            SetState(CURRENT_SCREEN, fuse.name, "")
            ItemSetPosition(self.drag_obj.name, ItemGetX(fuse.name), ItemGetY(fuse.name))
            --ItemMoveTo(self.drag_obj.name, ItemGetX(fuse.name), ItemGetY(fuse.name), 0.2)
            
            ItemSetType(fuse.name, OBJ_TYPE.NO_ACTIVE)
            ItemSetType(self.drag_obj.name, OBJ_TYPE.ACTIVE_ALL)
            ItemAttachChild("drag_trigger_img", fuse.name)
            ItemAttachChild(self.name, self.drag_obj.name)
            DragObject.active = true
            self.drag_obj = fuse;
            
            self:SetFuse()
            
            SetState(CURRENT_SCREEN, fuse.name, temp)
            return;
        else
            
            DragObject.active = false
            SetVisibleCursor(true)
            ItemAttachChild(self.name, self.drag_obj.name)
            ItemSetType(self.drag_obj.name, OBJ_TYPE.ACTIVE_ALL)
            ItemMoveTo(self.drag_obj.name, self.drag_obj.start_x, self.drag_obj.start_y, 0.5)
            
            self:UpdateLamp(); 
        end
        ItemAttachChildInCurPos(self.name, self.drag_obj.name)
        SetVisibleCursor(true)
        self.drag_obj = nil; 
    else
        local fuse = self:get_fuse_by_name(item_name)
        if(fuse and fuse.active) then
            local temp = GetState(CURRENT_SCREEN, item_name)
            SetState(CURRENT_SCREEN, item_name, "")
            self:UpdateLamp()
            DelayedExec(0.1, function()
                SetState(CURRENT_SCREEN, item_name, temp)
            end)
            SoundPlay("sound/fuse_insert.ogg")
            SetVisibleCursor(false)
            ItemSetType(fuse.name, OBJ_TYPE.NO_ACTIVE)
            ItemAttachChild("drag_trigger_img", fuse.name)
            DragObject.active = true
            self.drag_obj = fuse;
        end
    end
end

function GameScreen:SetFuse()
    if(not self.minigame_active) then return end
    
    SoundPlay("sound/fuse_insert.ogg")
    
    self:UpdateLamp();
            
    if(self:IsMinigameCompleted()) then
        self.minigame_active = false;
        SetState("screen_005_security", "state", "mg2")
        DelayedExec(2.0, function()
            clApplication:LoadLocation("005_security")
        end, 113)
    end
end

function GameScreen:UpdateLamp()
    if(self:is_basement_completed() and self.basement == false) then
        self.basement = true;
        ItemSetOpacity("lamp_basement", 1.0);
        SoundPlay("sound/lamp_on.ogg");
    elseif(not self:is_basement_completed() and self.basement) then
        self.basement = false;
        ItemSetOpacity("lamp_basement", 0.0);
        SoundPlay("sound/lamp_on.ogg");
    end
    if(self:is_elevator_completed() and self.elevator == false) then
        self.elevator = true;
        ItemSetOpacity("lamp_elevator", 1.0);
        SoundPlay("sound/lamp_on.ogg");
    elseif(not self:is_elevator_completed() and self.elevator) then
        self.elevator = false;
        ItemSetOpacity("lamp_elevator", 0.0);
        SoundPlay("sound/lamp_on.ogg");
    end
end

function GameScreen:get_fuse_by_name(item_name)
    for key, value in pairs(self.fuses) do
        if(value.name == item_name) then
            return value;
        end
    end
    
    return nil;
end

function GameScreen:set_start_pos(item_name, start_x, start_y)
    for key, value in pairs(self.fuses) do
        if(value.name == item_name) then
            value.start_x = start_x;
            value.start_y = start_y;
        end
    end
end

function GameScreen:on_hide_drag_object()
    if(self.drag_obj or self.buf_drag) then
        SetVisibleCursor(true) 
        ItemSetOpacity(self.drag_obj.name, 0.0)
        self.buf_drag = self.drag_obj;
        self.drag_obj = nil
    end
end


function GameScreen:OnFinish()
end

function GameScreen:Update()
    if(self.drag_obj) then
        ItemSetPosition(self.drag_obj.name, messenger_table.mouse_X - ItemGetWidth(self.drag_obj.name) / 2, messenger_table.mouse_Y - ItemGetHeight(self.drag_obj.name) / 2);
    end
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "05_switchboard", "graphics/minigames/05_switchboard/05_switchboard", 0, 0);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lamp_elevator", "graphics/minigames/05_switchboard/lamp_elevator", 965, 225);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lamp_basement", "graphics/minigames/05_switchboard/lamp_basement", 952, 650);
    ItemSetOpacity("lamp_basement", 0.0)
    ItemSetOpacity("lamp_elevator", 0.0)
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_t8_insert", "graphics/minigames/05_switchboard/fuse_t8_insert", 869, 726);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_n7_insert", "graphics/minigames/05_switchboard/fuse_n7_insert", 801, 726);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_e6_insert", "graphics/minigames/05_switchboard/fuse_e6_insert", 735, 726);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_m5_insert", "graphics/minigames/05_switchboard/fuse_m5_insert", 668, 726);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_e4_insert", "graphics/minigames/05_switchboard/fuse_e4_insert", 601, 726);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_s3_insert", "graphics/minigames/05_switchboard/fuse_s3_insert", 534, 726);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_a2_insert", "graphics/minigames/05_switchboard/fuse_a2_insert", 466, 726);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_b1_insert", "graphics/minigames/05_switchboard/fuse_b1_insert", 398, 726);
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_r8_insert", "graphics/minigames/05_switchboard/fuse_r8_insert", 869, 287);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_o7_insert", "graphics/minigames/05_switchboard/fuse_o7_insert", 802, 287);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_t6_insert", "graphics/minigames/05_switchboard/fuse_t6_insert", 735, 287);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_a5_insert", "graphics/minigames/05_switchboard/fuse_a5_insert", 668, 287);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_v4_insert", "graphics/minigames/05_switchboard/fuse_v4_insert", 601, 287);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_e3_insert", "graphics/minigames/05_switchboard/fuse_e3_insert", 533, 287);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_l2_insert", "graphics/minigames/05_switchboard/fuse_l2_insert", 466, 287);
    self:CreateObj(OBJ_TYPE.TRIGGER, "fuse_e1_insert", "graphics/minigames/05_switchboard/fuse_e1_insert", 399, 287);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_b1", "graphics/minigames/05_switchboard/fuse_b1", 394, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_t8", "graphics/minigames/05_switchboard/fuse_t8", 447, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_e6", "graphics/minigames/05_switchboard/fuse_e6", 499, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_t6", "graphics/minigames/05_switchboard/fuse_t6", 553, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_e3", "graphics/minigames/05_switchboard/fuse_e3", 604, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_m5", "graphics/minigames/05_switchboard/fuse_m5", 658, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_a2", "graphics/minigames/05_switchboard/fuse_a2", 710, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_v4", "graphics/minigames/05_switchboard/fuse_v4", 761, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_o7", "graphics/minigames/05_switchboard/fuse_o7", 815, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_l2", "graphics/minigames/05_switchboard/fuse_l2", 868, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_e1", "graphics/minigames/05_switchboard/fuse_e1", 920, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_s3", "graphics/minigames/05_switchboard/fuse_s3", 972, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_a5", "graphics/minigames/05_switchboard/fuse_a5", 1023, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_r8", "graphics/minigames/05_switchboard/fuse_r8", 1077, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_e4", "graphics/minigames/05_switchboard/fuse_e4", 1129, 939);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_n7", "graphics/minigames/05_switchboard/fuse_n7", -100, -100);--TODO

    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_h1_wrong", "graphics/minigames/05_switchboard/fuse_h1_wrong", 391, 505);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_a2_wrong", "graphics/minigames/05_switchboard/fuse_a2_wrong", 459, 505);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_l3_wrong", "graphics/minigames/05_switchboard/fuse_l3_wrong", 526, 505);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fuse_l4_wrong", "graphics/minigames/05_switchboard/fuse_l4_wrong", 594, 505);
    
    clScreenManager:add_screen("hud") 
end