function GameScreen:RestoreScreen()
    if(GetState(CURRENT_SCREEN, "state") == "") then
        for key, value in pairs(self.numbers) do
            if(GetState(CURRENT_SCREEN, "n_"..value[1]) == "remove")then
                ItemRemove(self.name, value[1]);
            end
        end
        
        for key, value in pairs(self.panel_slots) do    
            if(GetState(CURRENT_SCREEN, "s_"..value[3]) == "active")then
                ItemSetOpacity(value[3], 1.0)
                value.active = true;
            end
        end              
    elseif(GetState(CURRENT_SCREEN, "state") == "finish") then
        ItemSetOpacity("skip", 0)
    
        for key, value in pairs(self.numbers) do
            ItemRemove(self.name, value[1]);
        end
        
        for key, value in pairs(self.panel_slots) do
            ItemSetOpacity(value[3], 1.0)
        end
        
        ItemRemove(self.name, "card_cover")
        HUDScreen:SetInvntoryOnlyMode()
    end
end

function GameScreen:InitScreen()
    self:LoadScreen()       
 
    self.panel_slots = {{"5_affix_01", 5, "5_56"}, {"5_affix_02", 5, "5_57"}, {"7_affix", 7, "7_57"}, {"5_affix_03", 5, "5_58"},
                         {"5_affix_04", 5, "5_59"}, {"6_affix_01", 6, "6_60"}, {"6_affix_02", 6, "6_61"}, {"6_affix_03", 6, "6_62"},
                         {"6_affix_04", 6, "6_63"}, {"6_affix_05", 6, "6_65"}, {"5_affix_05", 5, "5_65"}, {"8_affix", 8, "8_58"}, 
                         {"9_affix", 9, "9_59"}, {"0_affix", 0, "0_60"}, {"1_affix", 1, "1_61"}, {"2_affix", 2, "2_62"},
                         {"4_affix", 4, "4_64"}, 
    }
    
    self.numbers = {{"9", 9}, {"5_5", 5}, {"2", 2}, {"6_5", 6}, {"5_4", 5}, {"6_4", 6}, {"0", 0}, {"5_3", 5}, 
                {"5_2", 5}, {"8", 8}, {"4", 4}, {"6_3", 6}, {"1", 1}, {"6_2", 6}, {"6_1", 6}, {"5_1", 5}, {"7", 7}, 
    }
  
    HUDScreen:SetMiniGameMode()
    
	for key, value in pairs(self.panel_slots) do
	    ItemSetOpacity(value[3], 0.0);
	end
	
    MusicPlay({"sound/ambient/02_ambient.ogg", "sound/music/hospital.ogg"}, 2) 
    
    self:RestoreScreen()
    
    HUDScreen:CreateBackButton("002_hall")
end

function GameScreen:onObjectClick(item_name)  
    if(GetState(CURRENT_SCREEN, "state") == "") then
        if(self.drag_num == nil) then
           self:take_number(item_name)
        elseif(self:try_place_number(item_name)) then
            SetVisibleCursor(true)
            self.drag_num = nil
            DragObject.active = false;
            if(self:InMinigameCompleted()) then
                self:OnFinish()
            end
        else    
            DragObject.active = false;
            SoundPlay("sound/interface/wrong.ogg");
            SetVisibleCursor(true)
            local old_x = ItemGetX(self.drag_num.old_name);
            local old_y = ItemGetY(self.drag_num.old_name);
            ItemMoveTo(self.drag_num.full_name, old_x, old_y, 0.5)
            local name1 = self.drag_num.old_name;
            local name2 = self.drag_num.full_name;
            DelayedExec(0.5, function()
                ItemSetOpacity(name1, 1.0)
                HUDScreen:RemoveItem(name2)
            end, 113)
            self.drag_num = nil
        end
    elseif(GetState(CURRENT_SCREEN, "state") == "finish") then
        SetVisibleCursor(true)
        
        if(item_name == "card") then
            ItemRemove(self.name, "card", 0.3)
            SoundPlay("sound/interface/found.ogg");
            
            clApplication.curret_win = clScreenManager:add_screen("tool_found_screen") 
            clApplication.curret_win._resource = "graphics/minigames/02_stand/object_found_02"
            clApplication.curret_win.end_fun = function()
                Inventory:ToolFound("card_002", 800, 600)
                local tip_end = function()
                    SetState("screen_002_hall", "state", "chief1")
                    DelayedExec(2.0, function()
                        clApplication:LoadLocation("002_hall")
                    end, 113)
                end
                
                HUDScreen:ShowTips({path = "graphics/minigames/02_stand/text_02a", x = 311, y = 1110, end_fun = tip_end})
            end
        end
    end
end
function GameScreen:try_place_number(item_name)
    if(not self.drag_num) then return false end
    
    for key, value in pairs(self.panel_slots) do
        if(item_name == value[1] and not value.active) then
            if(value[2] == self.drag_num.type) then
                SetState(CURRENT_SCREEN, "n_"..self.drag_num.old_name, "remove")
                SetState(CURRENT_SCREEN, "s_"..value[3], "active")
                SoundPlay("sound/fuse_take.ogg");
                SetVisibleCursor(true)
                HUDScreen:RemoveItem(self.drag_num.full_name)
                self:RemoveItem(self.drag_num.old_name)
                ItemSetOpacity(value[3], 1.0);
                value.active = true;
                return true;
            end
        end
    end
    
    return false
end

function GameScreen:take_number(item_name)
    for key, value in pairs(self.numbers) do
        if(item_name == value[1]) then
            SoundPlay("sound/fuse_take.ogg");
            SetVisibleCursor(false)
            self.drag_num = {}
            DragObject:CreateObj(OBJ_TYPE.NO_ACTIVE, value[2].."_full", "graphics/minigames/02_stand/"..value[2].."_full", -100, -100);
            DragObject.active = true;
            self.drag_num.full_name = value[2].."_full";
            self.drag_num.old_name = value[1];
            self.drag_num.type = value[2];
            ItemSetOpacity(value[1], 0.0)
            return true;
        end
    end
end


function GameScreen:OnSkip()
    self.drag_num = nil
    
    ItemSetOpacity("skip", 0)
    
    for key, value in pairs(self.numbers) do
        ItemRemove(self.name, value[1]);
    end
    
    for key, value in pairs(self.panel_slots) do
        ItemSetOpacity(value[3], 1.0)
    end
    
    DragObject.active = false;
    SetVisibleCursor(true)
    
    self:OnFinish()
end

function GameScreen:InMinigameCompleted() 
    for key, value in pairs(self.panel_slots) do
        if(not value.active) then
            return false;
        end
    end
    return true;
end

function GameScreen:OnFinish()
    SetState(CURRENT_SCREEN, "state", "finish")
    HUDScreen:SetInvntoryOnlyMode()
    
    DelayedExec(1.0, function()
        ItemMoveTo("card_cover", 700, 1200, 1.0)
        ItemRotate("card_cover", M_PI / 2, 1.0)
    end, 113)
end
--[[
function GameScreen:OnMouseMove(item_name)
    if(self.drag_num) then
        if(item_name == "skip" or item_name == "menu_button") then
            SetVisibleCursor(true)
            ItemSetOpacity(self.drag_num.full_name, 0.0)
        else
            SetVisibleCursor(false)
            ItemSetOpacity(self.drag_num.full_name, 1.0)
        end
    end
end
--]]
function GameScreen:Update()
    if(self.drag_num) then
        ItemSetPosition(self.drag_num.full_name, messenger_table.mouse_X - ItemGetWidth(self.drag_num.full_name) / 2, messenger_table.mouse_Y - ItemGetHeight(self.drag_num.full_name) / 2);
    end
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "02_stand", "graphics/minigames/02_stand/02_stand", 0, 0);
	
	self:CreateObj(OBJ_TYPE.TRIGGER, "5_affix_01", "graphics/minigames/02_stand/5_affix_01", 273, 463);
	self:CreateObj(OBJ_TYPE.TRIGGER, "5_affix_02", "graphics/minigames/02_stand/5_affix_02", 511, 463);
	self:CreateObj(OBJ_TYPE.TRIGGER, "7_affix", "graphics/minigames/02_stand/7_affix", 575, 463);
	self:CreateObj(OBJ_TYPE.TRIGGER, "5_affix_03", "graphics/minigames/02_stand/5_affix_03", 740, 463);
	self:CreateObj(OBJ_TYPE.TRIGGER, "5_affix_04", "graphics/minigames/02_stand/5_affix_04", 977, 463);
	self:CreateObj(OBJ_TYPE.TRIGGER, "6_affix_01", "graphics/minigames/02_stand/6_affix_01", 1207, 463);
	self:CreateObj(OBJ_TYPE.TRIGGER, "6_affix_02", "graphics/minigames/02_stand/6_affix_02", 270, 907);
	self:CreateObj(OBJ_TYPE.TRIGGER, "6_affix_03", "graphics/minigames/02_stand/6_affix_03", 510, 907);
	self:CreateObj(OBJ_TYPE.TRIGGER, "6_affix_04", "graphics/minigames/02_stand/6_affix_04", 740, 907);
	self:CreateObj(OBJ_TYPE.TRIGGER, "6_affix_05", "graphics/minigames/02_stand/6_affix_05", 1210, 907);
	self:CreateObj(OBJ_TYPE.TRIGGER, "5_affix_05", "graphics/minigames/02_stand/5_affix_05", 1276, 907);
	self:CreateObj(OBJ_TYPE.TRIGGER, "8_affix", "graphics/minigames/02_stand/8_affix", 804, 463);
	self:CreateObj(OBJ_TYPE.TRIGGER, "9_affix", "graphics/minigames/02_stand/9_affix", 1040, 463);
	self:CreateObj(OBJ_TYPE.TRIGGER, "0_affix", "graphics/minigames/02_stand/0_affix", 1272, 463);
	self:CreateObj(OBJ_TYPE.TRIGGER, "1_affix", "graphics/minigames/02_stand/1_affix", 336, 906);
	self:CreateObj(OBJ_TYPE.TRIGGER, "2_affix", "graphics/minigames/02_stand/2_affix", 576, 906);
	self:CreateObj(OBJ_TYPE.TRIGGER, "4_affix", "graphics/minigames/02_stand/4_affix", 1043, 904);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "9", "graphics/minigames/02_stand/9", 1217, 1037);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "5_5", "graphics/minigames/02_stand/5_5", 1150, 1036);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "2", "graphics/minigames/02_stand/2", 1069, 1043);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "6_5", "graphics/minigames/02_stand/6_5", 1015, 1022);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "5_4", "graphics/minigames/02_stand/5_4", 956, 1018);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "6_4", "graphics/minigames/02_stand/6_4", 878, 1047);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "0", "graphics/minigames/02_stand/0", 827, 1011);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "5_3", "graphics/minigames/02_stand/5_3", 787, 1055);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "5_2", "graphics/minigames/02_stand/5_2", 785, 1010);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "8", "graphics/minigames/02_stand/8", 734, 1006);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "4", "graphics/minigames/02_stand/4", 693, 1013);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "6_3", "graphics/minigames/02_stand/6_3", 583, 1018);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "1", "graphics/minigames/02_stand/1", 532, 1003);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "6_2", "graphics/minigames/02_stand/6_2", 488, 1015);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "6_1", "graphics/minigames/02_stand/6_1", 443, 1018);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "5_1", "graphics/minigames/02_stand/5_1", 329, 1042);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "7", "graphics/minigames/02_stand/7", 294, 1030);
	
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "5_65", "graphics/minigames/02_stand/5_65", 1263, 893);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "6_65", "graphics/minigames/02_stand/6_65", 1200, 893);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "4_64", "graphics/minigames/02_stand/4_64", 1030, 893);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "6_63", "graphics/minigames/02_stand/6_63", 729, 893);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "2_62", "graphics/minigames/02_stand/2_62", 563, 894);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "6_62", "graphics/minigames/02_stand/6_62", 501, 894);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "1_61", "graphics/minigames/02_stand/1_61", 325, 895);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "6_61", "graphics/minigames/02_stand/6_61", 264, 896);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "0_60", "graphics/minigames/02_stand/0_60", 1261, 451);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "6_60", "graphics/minigames/02_stand/6_60", 1196, 451);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "9_59", "graphics/minigames/02_stand/9_59", 1029, 451);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "5_59", "graphics/minigames/02_stand/5_59", 966, 453);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "8_58", "graphics/minigames/02_stand/8_58", 793, 455);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "5_58", "graphics/minigames/02_stand/5_58", 729, 454);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "7_57", "graphics/minigames/02_stand/7_57", 564, 454);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "5_57", "graphics/minigames/02_stand/5_57", 501, 452);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "5_56", "graphics/minigames/02_stand/5_56", 266, 455);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "card", "graphics/minigames/02_stand/card",  750, 586);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "card_cover", "graphics/minigames/02_stand/card_cover", 693, 549);
    
    clScreenManager:add_screen("hud") 
end