function GameScreen:RestoreScreen()
    self:__mg_pre_init(); 

    if(GetState(CURRENT_SCREEN, "state") == "") then
        HUDScreen:SetInvntoryOnlyMode()
        HUDScreen:ShowTips({path = "graphics/screens/19_attic/location/Text_19b", x = 678, y = 1129})
        
        ItemSetOpacity("circle_square_square", 0.0)
        local wood_apply = function()
            HUDScreen:SetMiniGameMode()
            SetState(CURRENT_SCREEN, "state", "mg")
            Inventory:ActiveToolApply()
            ItemChangeOpacity("circle_square_square", 1.0, 0.3) 
        end
         self:AddToolApplyZone("circle_square_square", "wooden_circle_019", wood_apply, true);
        
    elseif(GetState(CURRENT_SCREEN, "state") == "mg")then   
        HUDScreen:SetMiniGameMode()
    end
end

function GameScreen:InitScreen()
    self:LoadScreen()       
    
    self:RestoreScreen();
      
    MusicPlay({"sound/ambient/19_ambient.ogg", "sound/music/past.ogg"} , 19) 
    
    HUDScreen:CreateBackButton("019_attic")
end
------------------------------------------------------------------------------------------------------
function GameScreen:__mg_pre_init()
    self:__add_circle("circle_heart_heart")
    self:__add_circle("circle_fork_fork")
    self:__add_circle("circle_heart_fork")
    self:__add_circle("circle_square_square")
    self:__add_circle("circle_triangle_heart")
    self:__add_circle("circle_triangle_triangle")
    self:__add_circle("circle_round_square")
    self:__add_circle("circle_round_round")
    self:__add_circle("circle_round_triangle")
    
    for key, circle in pairs(self.circles) do
        if(GetState(CURRENT_SCREEN, circle.name) == "") then
            self:__circle_set_angle(circle, math.random(3))
        else
           self:__circle_set_angle(circle, tonumber(GetState(CURRENT_SCREEN, circle.name)))
        end
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:__circle_roatate_angle(circle, _angle)
    SoundPlay("sound/circle_rotate.ogg")
    ItemRotate(circle.name, M_PI / 2, 0.3)
    circle.active = false;
    circle.angle = _angle;
    if(circle.angle >= 4) then circle.angle = 0; end
    SetState(CURRENT_SCREEN, circle.name, ""..circle.angle)
    
    DelayedExec(0.3, function()
        circle.active = true;

        if(circle.name == "circle_round_round" and circle.angle == 0) then
            SoundPlay("sound/lock_slide.ogg")
            circle.active = false;
            ItemMoveTo("lock_round", 758, 59 + 102, 0.4)
        end 
        if(circle.name == "circle_square_square" and circle.angle == 0) then
            SoundPlay("sound/lock_slide.ogg")
            circle.active = false;
            ItemMoveTo("lock_square", 1126 - 83, 566, 0.4)
        end 
        if(circle.name == "circle_fork_fork" and circle.angle == 0) then
            SoundPlay("sound/lock_slide.ogg")
            circle.active = false;
            ItemMoveTo("lock_fork", 757, 929 - 102, 0.4)
        end 
        if(circle.name == "circle_triangle_triangle" and circle.angle == 0) then
            SoundPlay("sound/lock_slide.ogg")
            circle.active = false;
            ItemMoveTo("lock_triangle", 264 + 90, 566, 0.4)
        end 
        
        ItemSetAngle(circle.name, M_PI / 2 * circle.angle)
    end, 113)
    
    if(self:InMinigameCompleted()) then
        DelayedExec(1.0, function()
            self:OnFinish()
        end, 113)
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:__circle_set_angle(circle, _angle)
    circle.angle = _angle;
    if(circle.angle >= 4) then circle.angle = 0; end
    
    ItemSetAngle(circle.name, M_PI / 2 * circle.angle)
    
    SetState(CURRENT_SCREEN, circle.name, ""..circle.angle)
    
    if(circle.name == "circle_round_round" and circle.angle == 0) then
        circle.active = false;
        ItemSetPosition("lock_round", 758, 59 + 102)
    end 
    if(circle.name == "circle_square_square" and circle.angle == 0) then
        circle.active = false;
        ItemSetPosition("lock_square", 1126 - 83, 566)
    end 
    if(circle.name == "circle_fork_fork" and circle.angle == 0) then
        circle.active = false;
        ItemSetPosition("lock_fork", 757, 929 - 102)
    end 
    if(circle.name == "circle_triangle_triangle" and circle.angle == 0) then
        circle.active = false;
        ItemSetPosition("lock_triangle", 264 + 90, 566)
    end 
    
    if(self:InMinigameCompleted()) then
        DelayedExec(1.0, function()
            self:OnFinish()
        end, 113)
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:__add_circle(circle_name)
     self.circles =  self.circles or {}
     
     local circle = {}
     circle.name = circle_name;
     circle.active = true;
     circle.angle = 0;
     
     table.insert(self.circles, circle)
end
------------------------------------------------------------------------------------------------------
function GameScreen:__get_circle_by_name(item_name)
    for key, value in pairs(self.circles) do
         if(item_name == value.name) then
            return value;
         end
    end
    
    return nil
end
------------------------------------------------------------------------------------------------------
function GameScreen:OnSkip()
    HUDScreen:SetInvntoryOnlyMode()
    
    for key, value in pairs(self.circles) do
         value.active = false
    end
    
    DelayedExec(0.5, function()
        self:OnFinish()
    end, 113)
end
------------------------------------------------------------------------------------------------------
function GameScreen:onObjectClick(item_name)
    if self.active == false then return end
    
    if(GetState(CURRENT_SCREEN, "state") == "mg")then
        local circle = self:__get_circle_by_name(item_name)
        if(circle and circle.active) then
            self:__circle_roatate_angle(circle, circle.angle + 1)
        end
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:InMinigameCompleted()   
    for key, value in pairs(self.circles) do
         if(tonumber(GetState(CURRENT_SCREEN, value.name)) ~= 0) then
            return false;
         end
    end
    
    return true
end
------------------------------------------------------------------------------------------------------
function GameScreen:OnFinish()
    SetState("screen_019_attic", "state", "house")
    clApplication:LoadLocation("019_attic")
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "01_patch", "graphics/minigames/19_door/19_door", 0, 0);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "circle_heart_heart", "graphics/minigames/19_door/circle_heart_heart", 896, 689);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "circle_fork_fork", "graphics/minigames/19_door/circle_fork_fork", 688, 691);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "circle_heart_fork", "graphics/minigames/19_door/circle_heart_fork", 482, 689);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "circle_square_square", "graphics/minigames/19_door/circle_square_square", 895, 492);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "circle_triangle_heart", "graphics/minigames/19_door/circle_triangle_heart", 688, 490);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "circle_triangle_triangle", "graphics/minigames/19_door/circle_triangle_triangle", 481, 493);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "circle_round_square", "graphics/minigames/19_door/circle_round_square", 897, 289);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "circle_round_round", "graphics/minigames/19_door/circle_round_round", 685, 293);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "circle_round_triangle", "graphics/minigames/19_door/circle_round_triangle", 481, 299);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lock_fork", "graphics/minigames/19_door/lock_fork", 757, 929);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lock_fork_cover", "graphics/minigames/19_door/lock_fork_cover", 751, 1052);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lock_square", "graphics/minigames/19_door/lock_square", 1126, 566);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lock_square_cover", "graphics/minigames/19_door/lock_square_cover", 1238, 559);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lock_round", "graphics/minigames/19_door/lock_round", 758, 59);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lock_round_cover", "graphics/minigames/19_door/lock_round_cover", 751, 88);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lock_triangle", "graphics/minigames/19_door/lock_triangle", 264, 566);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lock_triangle_cover", "graphics/minigames/19_door/lock_triangle_cover", 290, 560);
	
    clScreenManager:add_screen("hud") 
end
