
local loading_eff = {
            {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 0.0, time = 0.5, end_value = 0.0},
            {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 0.5, time = 0.5, end_value = 1.0},
	    }

local but_eff = {
            {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 0.0, time = 0.5, end_value = 0.3},
            {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 0.5, time = 0.5, end_value = 1.0},
	    }

function GameScreen:RestoreScene()
    if(pc_sound) then
        StopSound(pc_sound)
        pc_sound = nil;
    end
    
    if(GetState(CURRENT_SCREEN, "mgbuf") == "1") then
        SetState(CURRENT_SCREEN, "mg", "finish")
    end
    
    pc_sound_mg = SoundPlay("sound/pc_idle.ogg", true)
    
    if(GetState(CURRENT_SCREEN, "mg") == "started") then
        self:StartMG()  
    elseif(GetState(CURRENT_SCREEN, "mg") == "finish") then  
        HUDScreen:SetInvntoryOnlyMode()
        HUDScreen:RemoveBackButton();
        
        ItemRemove(self.name, "activate_on")
        ItemRemove(self.name, "portal_manual")
        
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "warning", "graphics/minigames/08_password/warning", 451, 449);	
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "white_border", "graphics/minigames/08_password/white_border", 309, 160);	
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "ok", "graphics/minigames/08_password/ok",678, 576);
    elseif(GetState(CURRENT_SCREEN, "mg") == "finish2") then  
        HUDScreen:SetInvntoryOnlyMode()
        HUDScreen:RemoveBackButton();
    
        ItemRemove(self.name, "activate_on")
        ItemRemove(self.name, "portal_manual")
        
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "unlock_generator", "graphics/minigames/08_password/unlock_generator", 451, 449);	
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "white_border", "graphics/minigames/08_password/white_border", 309, 160);	
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "confirm", "graphics/minigames/08_password/confirm",678, 576);
    end
end

function GameScreen:InitScreen()
    self:LoadScreen()
    
    self.password = {"4", "8", "6", "2", "9", "7", "3"}
    HUDScreen:SetInvntoryOnlyMode()
    
              
    self:RestoreScene()
    MusicPlay({"sound/ambient/08_ambient.ogg", "sound/music/basement.ogg"}, 8) 
    
    HUDScreen:CreateBackButton("008_portal")
end
------------------------------------------------------------------------------------------------------
function GameScreen:InitQuads()

   local buttons_name = {{"74", "2", "16", "0", "12", "6"},
                            {"32", "15", "89", "20", "4", "31"},
                            {"22", "55", "11", "5", "21", "7"},
                            {"30", "9", "1", "29", "35", "93"},
                            {"8", "13", "68", "23", "3", "44"},        
                            };
                
    self.buttons = {}
    for i = 1, 6 do
        self.buttons[i] = {}
        for j = 1, 5 do
            self:CreateObj(OBJ_TYPE.NO_ACTIVE, buttons_name[j][i], "graphics/minigames/08_password/"..buttons_name[j][i], 438 + 120 * (i - 1), 300 + 115 * (j - 1));	
            ItemSetOpacity(buttons_name[j][i], 0.0)
            ItemChangeOpacity(buttons_name[j][i], 1.0, 0.4)   
            self.buttons[i][j] = {}
            self.buttons[i][j].number = buttons_name[j][i];
        end
    end 
    
    for i = 1, 6 do
        for j = 1, 5 do
            self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "gray_"..i.."_"..j, "graphics/minigames/08_password/gray", 438 + 120 * (i - 1), 300 + 115 * (j - 1));	
            self.buttons[i][j].back = "gray_"..i.."_"..j;
            DelayedExec(0.02 * i + 0.12 * j, function()
                ItemChangeOpacity("gray_"..i.."_"..j, 0.0, 0.05)
            end, 113);
            DelayedExec(2.0 + 0.02 * i + 0.12 * j, function()
                ItemChangeOpacity("gray_"..i.."_"..j, 1.0, 0.05)
            end, 113);
        end
    end --]]
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "password_4", "graphics/minigames/08_password/password_4", 977, 211);	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "password_8", "graphics/minigames/08_password/password_8", 905, 211);	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "password_6", "graphics/minigames/08_password/password_6", 832, 211);	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "password_2", "graphics/minigames/08_password/password_2", 760, 211);	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "password_9", "graphics/minigames/08_password/password_9", 687, 211);	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "password_7", "graphics/minigames/08_password/password_7", 615, 211);	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "password_3", "graphics/minigames/08_password/password_3", 542, 211);    
    for i = 1, 7 do
        ItemSetOpacity("password_"..self.password[i], 0.0)
    end
    
    self.open_number = 0;
    
    DelayedExec(2.5, function()
        self.active = true;
    end, 113)
end
------------------------------------------------------------------------------------------------------
function GameScreen:OnClick(item_name)  
    if(GetState(CURRENT_SCREEN, "mg") == "started") then
        if(self.active) then
            local quad = self:get_quad_by_back_name(item_name)
            if(quad) then
                ItemChangeOpacity(quad.back, 0.0, 0.5)
                ItemSetType(quad.back, OBJ_TYPE.NO_ACTIVE)
                if(self:is_password_number(quad.number)) then
                    ItemChangeOpacity("password_"..quad.number, 1.0, 0.4)
                    self.open_number = self.open_number + 1
                    
                    if(self:IsMinigameCompleted()) then
                        self.active = false
                        
                        SetState(CURRENT_SCREEN, "mgbuf", "1")
                        
                        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "activate_on2", "graphics/minigames/08_password/activate_on", 626, 873);	
                        DelayedExec(0.2, function()	
                            EffectorPlay(but_eff, "activate_on2", true)
                        end, 113)
                    end                    
                else
                    DelayedExec(0.8, function()
                        self:ResetField();
                    end, 113)
                end
            end
        end
        
        if(item_name == "activate_on2") then
            SetState(CURRENT_SCREEN, "mgbuf", "0")
            SetState(CURRENT_SCREEN, "mg", "finish")
            self:OnFinish();
        end
    elseif(GetState(CURRENT_SCREEN, "mg") == "finish") then  
        if(item_name == "ok") then
            SetState(CURRENT_SCREEN, "mg", "finish2")
            ItemRemove(self.name, "activate_on")
            ItemRemove(self.name, "portal_manual")
            ItemRemove(self.name, "warning")
            ItemRemove(self.name, "ok")
            
            self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "unlock_generator", "graphics/minigames/08_password/unlock_generator", 451, 449);	
            self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "white_border", "graphics/minigames/08_password/white_border", 309, 160);	
            self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "confirm", "graphics/minigames/08_password/confirm",678, 576);         
        end
    elseif(GetState(CURRENT_SCREEN, "mg") == "finish2") then  
        if(item_name == "confirm") then
            SetState("screen_008_portal", "state", "generator")
            DelayedExec(1.0, function()
                clApplication:LoadLocation("008_portal")
            end, 113)           
        end
    else
        if(item_name == "activate_on") then
            self:StartMG()            
        end
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:StartMG()

    SetState(CURRENT_SCREEN, "mg", "started")
    ItemSetType("activate_on", OBJ_TYPE.NO_ACTIVE)
    ItemRemove(self.name, "activate_on", 0.3)
    ItemRemove(self.name, "portal_manual", 0.3)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "loading", "graphics/minigames/08_password/loading", 700, 788);
    EffectorPlay(loading_eff, "loading", true)
    HUDScreen:ShowTips({path = "graphics/minigames/08_password/text_08b", x = 373, y = 1130})
    DelayedExec(4.0, function()
        HUDScreen:SetMiniGameMode()
        ItemRemove(self.name, "loading", 0.3)
        ItemSetOpacity("white_border", 0.0)
        ItemRemove(self.name, "white_border", 0.3)
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "password_border", "graphics/minigames/08_password/password_border", 309, 148);	
        ItemSetOpacity("password_border", 0.0)
        ItemChangeOpacity("password_border", 1.0, 0.4)

        self:InitQuads();  
    end, 113)
end
------------------------------------------------------------------------------------------------------
function GameScreen:ResetField()
    self.active = false;
    self.open_number = 0;
    for i = 1, 6 do
        for j = 1, 5 do
            ItemChangeOpacity(self.buttons[i][j].back, 1.0, 0.5)
            ItemSetType(self.buttons[i][j].back, OBJ_TYPE.ACTIVE_ALL)
        end
    end
    
    for i = 1, 7 do
        ItemChangeOpacity("password_"..self.password[i], 0.0, 0.5)
    end
    
    DelayedExec(0.5, function()
        self:mix_field()
    end, 113)
    
    DelayedExec(1.0, function()
        for i = 1, 6 do
            for j = 1, 5 do
                DelayedExec(0.02 * i + 0.12 * j, function()
                    ItemChangeOpacity(self.buttons[i][j].back, 0.0, 0.05)
                end, 113);
                DelayedExec(2.0 + 0.02 * i + 0.12 * j, function()
                    ItemChangeOpacity(self.buttons[i][j].back, 1.0, 0.05)
                end, 113);
             end
         end
    end, 113)
    DelayedExec(3.5, function()
        self.active = true;
    end, 113)
end
------------------------------------------------------------------------------------------------------
function GameScreen:is_password_number(num)
    for key, value in pairs(self.password) do
        if(value == num) then
            return true;
        end
    end
    return false;
end
------------------------------------------------------------------------------------------------------
function GameScreen:get_quad_by_back_name(item_name)
    for i = 1, 6 do
        for j = 1, 5 do
            if(self.buttons[i][j].back == item_name) then
                return self.buttons[i][j];
            end
        end
    end
    
    return nil;
end
------------------------------------------------------------------------------------------------------
function GameScreen:IsMinigameCompleted()
    if(self.open_number == 7) then
        return true;
    end
    
    return false
end
------------------------------------------------------------------------------------------------------
function GameScreen:mix_field()
    local new_array = {}
    for i = 1, 6 do
        new_array[i] = {}
        for j = 1, 5 do
            new_array[i][j] = ""
        end
    end
    
    --Сначало запихнет пасворд
    for key, value in pairs(self.password) do
        local index_i = 0;
        local index_j = 0;
        repeat
            index_i = math.random(6);
            index_j = math.random(5);
            
            if(new_array[index_i][index_j] == "" and 
                    (new_array[index_i - 1] == nil or new_array[index_i - 1][index_j] == "") and 
                    (new_array[index_i + 1] == nil or new_array[index_i + 1][index_j] == "") and
                    (new_array[index_i][index_j - 1] == nil or new_array[index_i][index_j - 1] == "") and    
                    (new_array[index_i][index_j + 1] == nil or new_array[index_i][index_j + 1] == "")
                    )then
                break;
            end
        until false
        
        new_array[index_i][index_j] = value
    end
    --и всю остальную хрень
    local buttons_name = {"74", "16", "0", "12", "32", "15", "89", "20", "31",
                            "22", "55", "11", "5", "21", "30", "1", "29", "35", "93",
                            "13", "68", "23", "44"}   
  
    for key, value in pairs(buttons_name) do
        local index_i = 0;
        local index_j = 0;
        repeat
            index_i = math.random(6);
            index_j = math.random(5);
            
            if(new_array[index_i][index_j] == "") then
                break;
            end
        until false
        
        new_array[index_i][index_j] = value
    end           

   for i = 1, 6 do
        for j = 1, 5 do
            self.buttons[i][j].number = new_array[i][j]
            ItemSetPosition(self.buttons[i][j].number, 438 + 120 * (i - 1), 300 + 115 * (j - 1))
        end
    end     
end
------------------------------------------------------------------------------------------------------
function GameScreen:OnFinish()
    if(self.finish) then return end
    self.finish = true;
    
    HUDScreen:SetInvntoryOnlyMode()
    HUDScreen:RemoveBackButton();
    
    for i = 1, 6 do
        for j = 1, 5 do
            ItemRemove(self.name, self.buttons[i][j].back, 0.2)
            ItemRemove(self.name, self.buttons[i][j].number, 0.2)

        end
    end
    for i = 1, 7 do
        ItemRemove(self.name, "password_"..self.password[i], 0.2)
    end
    
    ItemRemove(self.name, "password_border", 0.2)
    ItemRemove(self.name, "activate_on2", 0.2)
    
    DelayedExec(0.3, function()
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "warning", "graphics/minigames/08_password/warning", 451, 449);	
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "white_border", "graphics/minigames/08_password/white_border", 309, 160);	
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "ok", "graphics/minigames/08_password/ok", 678, 576);
    end, 113)
end
------------------------------------------------------------------------------------------------------
function GameScreen:OnSkip()
    --SetState("screen_008_portal", "state", "generator")
    --DelayedExec(1.0, function()
     --   clApplication:LoadLocation("008_portal")
    --end, 113
    SetState(CURRENT_SCREEN, "mgbuf", "0")
    SetState(CURRENT_SCREEN, "mg", "finish")
    self:OnFinish();
end
--------------------- ---------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "08_password", "graphics/minigames/08_password/08_password", 0, 0);	  

    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "blue_screen", "graphics/minigames/08_password/blue_screen", 289, 124);	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "portal_manual", "graphics/minigames/08_password/portal_manual", 331, 216);	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "white_border", "graphics/minigames/08_password/white_border", 309, 160);	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "activate_on", "graphics/minigames/08_password/activate_on", 626, 873);	
    
   
    clScreenManager:add_screen("hud") 
end