GameScreen.textures_path = "graphics/screens/11_gate/location/"

function GameScreen:RestoreScene()
    for i = 1, 16 do
        if(GetState(CURRENT_SCREEN, "plate_"..i) == "1")  then
            ItemSetOpacity("button_"..i, 1.0)
            ItemSetPosition("plate_"..i, self.open_coord[i][1], self.open_coord[i][2])
        else
            ItemSetOpacity("button_"..i, 0.01)
        end
    end
    self:__update_black(true)
    
    if(GetState(CURRENT_SCREEN, "state") == "finish") then
        self:CreateLocationPortal("11_black", "012_fresco", "eye");
    end
    
    for found_item in string.gmatch(GetState(CURRENT_SCREEN, "found_obj"), "[_%a%d]+") do
		ItemRemove(self.name, found_item)
		ItemRemove(self.name, found_item.."_shadow")
    end 
end

function GameScreen:InitScreen()
    self:LoadScreen()

    self.open_coord = { {1008, 398}, {377, 398}, {521, 637}, {1024, 219}, {408, 507}, 
                        {960, 637}, {682, -80}, {419, 217}, {1001, 507}, {754, 797}, 
    }
    self.close_coord = { {803, 398}, {583, 398}, {649, 637}, {877, 219}, {607, 507}, 
                        {830, 637}, {675, 198}, {569, 217}, {802, 507}, {754, 637}, 
    }
    
    self:RestoreScene();
   
    self:AddHiddenCrystal("crystal_11_part")
    
    HUDScreen:ShowHintText({path = self.textures_path.."hint_11", x = 315, y = 1128})
    
    MusicPlay({"sound/music/spaceship.ogg", "sound/ambient/11_ambient.ogg"} , 11)
    HUDScreen:SetMiniGameMode()
end  
------------------------------------------------------------------------------------------------------
function GameScreen:__update_black(immediately)
    local opacity = 1.0;
    for i = 1, 10 do
        if(GetState(CURRENT_SCREEN, "plate_"..i) == "1")  then
            opacity = opacity - 0.1
        end
    end
    
    if(immediately) then
        ItemSetOpacity("11_black", opacity)
    else
        ItemChangeOpacity("11_black", opacity, 0.5)
    end
    
end
------------------------------------------------------------------------------------------------------
function GameScreen:__get_button_id_by_name(item_name)
   for i = 1, 16 do
        if("button_"..i == item_name)  then
            return i;
        end
    end 
    
    return 0;
end
------------------------------------------------------------------------------------------------------
function GameScreen:onObjectClick(item_name)
    if(self.mouse_lock) then return end
    if(GetState(CURRENT_SCREEN, "state") == "finish") then return end
    
    local id = self:__get_button_id_by_name(item_name)
    
    if(id > 0) then
        if(id <= 10) then
            if(GetState(CURRENT_SCREEN, "plate_"..id) ~= "1")  then
                SetState(CURRENT_SCREEN, "plate_"..id, "1");
                ItemMoveTo("plate_"..id, self.open_coord[id][1], self.open_coord[id][2], 0.5)
                ItemChangeOpacity("button_"..id, 1.0, 0.3)
                self:__update_black();
                
                if(self:_is_mg_completed()) then
                    HUDScreen:SetInvntoryOnlyMode()
                    
                    SetState(CURRENT_SCREEN, "state", "finish")
                    self:CreateLocationPortal("11_black", "012_fresco", "eye");
                end
                SoundPlay("sound/plate_push.ogg")
                SoundPlay("sound/gate_open.ogg")
            end
        else
            SoundPlay("sound/plate_up.ogg")
            ItemChangeOpacity("button_"..id, 1.0, 0.3)
            ItemChangeOpacity("button_"..id, 0.0, 0.3, 0.3)
            self:_return_back_all()
        end
        
        self.mouse_lock = true
        DelayedExec(0.5, function()
            self.mouse_lock = false
        end, 113)
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:_is_mg_completed()
    for i = 1, 10 do
        if(GetState(CURRENT_SCREEN, "plate_"..i) ~= "1")  then
            return false
        end
    end
    
    return true
end
------------------------------------------------------------------------------------------------------
function GameScreen:_return_back_all()
    for i = 1, 10 do
        if(GetState(CURRENT_SCREEN, "plate_"..i) == "1")  then
            SetState(CURRENT_SCREEN, "plate_"..i, "0");
            ItemChangeOpacity("button_"..i, 0.0, 0.3)
            ItemMoveTo("plate_"..i, self.close_coord[i][1], self.close_coord[i][2], 0.5)
            self:__update_black();
            SoundPlay("sound/gate_close.ogg")
        end
    end
end

function GameScreen:OnSkip()
    HUDScreen:SetInvntoryOnlyMode()

    for i = 1, 10 do
        if(GetState(CURRENT_SCREEN, "plate_"..i) ~= "1")  then
            SetState(CURRENT_SCREEN, "plate_"..i, "1");
            ItemMoveTo("plate_"..i, self.open_coord[i][1], self.open_coord[i][2], 0.5)
            ItemChangeOpacity("button_"..i, 1.0, 0.3)
            self:__update_black();
            
            if(self:_is_mg_completed()) then
                SetState(CURRENT_SCREEN, "state", "finish")
                self:CreateLocationPortal("11_black", "012_fresco", "eye");
            end
        end
    end
    
    SoundPlay("sound/plate_push.ogg")
    SoundPlay("sound/gate_open.ogg")            
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "11_gate", "graphics/screens/11_gate/location/11_gate", 0, 0);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "11_black", "graphics/screens/11_gate/location/11_black", 559, 191);

    
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "plate_10", "graphics/screens/11_gate/location/plate_10", 754, 637);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "plate_9", "graphics/screens/11_gate/location/plate_09", 802, 507);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "plate_8", "graphics/screens/11_gate/location/plate_08", 569, 217);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "plate_7", "graphics/screens/11_gate/location/plate_07", 675, 198);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "plate_6", "graphics/screens/11_gate/location/plate_06", 830, 637);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "plate_5", "graphics/screens/11_gate/location/plate_05", 607, 507);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "plate_4", "graphics/screens/11_gate/location/plate_04", 877, 219);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "plate_3", "graphics/screens/11_gate/location/plate_03", 649, 637);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "plate_2", "graphics/screens/11_gate/location/plate_02", 583, 398);
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "plate_1", "graphics/screens/11_gate/location/plate_01", 803, 398);
	
	self:CreateObj(OBJ_TYPE.NO_ACTIVE, "door_11_cover", "graphics/screens/11_gate/location/door_11_cover", 377, 0);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_10", "graphics/screens/11_gate/location/button_10", 592, 821);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_16", "graphics/screens/11_gate/location/button_16", 710, 819);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_9", "graphics/screens/11_gate/location/button_09", 803, 818);	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_8", "graphics/screens/11_gate/location/button_08", 887, 822);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_15", "graphics/screens/11_gate/location/button_15", 594, 867);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_7", "graphics/screens/11_gate/location/button_07", 683, 867);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_14", "graphics/screens/11_gate/location/button_14", 803, 871);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_6", "graphics/screens/11_gate/location/button_06", 901, 892);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_5", "graphics/screens/11_gate/location/button_05", 625, 928);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_4", "graphics/screens/11_gate/location/button_04", 432, 918);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_3", "graphics/screens/11_gate/location/button_03", 818, 983);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_13", "graphics/screens/11_gate/location/button_13", 909, 925);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_2", "graphics/screens/11_gate/location/button_02", 979, 970);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_12", "graphics/screens/11_gate/location/button_12", 823, 1046);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_11", "graphics/screens/11_gate/location/button_11", 529, 1012);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_1", "graphics/screens/11_gate/location/button_01", 265, 970);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "crystal_11_part", "graphics/screens/11_gate/location/crystal_11_part", 515, 798);
	
	clScreenManager:add_screen("hud") 
end