
local __item_hint_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},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 1.0, time = 0.5, end_value = 0.2},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 1.5, time = 0.5, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 2.0, time = 0.5, end_value = 0.2},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 2.5, time = 0.5, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 3.0, time = 0.5, end_value = 0.0},
    }


function GameScreen:RestoreScene()
    if(GetState("zoom_004", "state") == "") then
        DelayedExec(15, function()
            self:show_hint();
        end, 113)
    elseif(GetState("zoom_004", "state") == "opened") then
        HUDScreen:SetInvntoryOnlyMode()
        self:open_safe(true);
    end
end

function GameScreen:InitScreen()
    self:LoadScreen()  
    
    SoundPlay("sound/safe_button.ogg")
              
    self.corrent_code = {"button_2", "button_6", "button_7", "button_9", "button_0"}       
        
    self:InitButtons()
    
    self:RestoreScene()
    HUDScreen:SetMiniGameMode()
    
    if(GetState("zoom_004", "state") == "opened") then
        HUDScreen:SetInvntoryOnlyMode()
  end
end

function GameScreen:OnSkip()
    ItemSetOpacity("skip", 0)
    self.finish = true

    for key, button in pairs(self.buttons) do
        if(not button.pushed and self:is_need_pushed(button.name)) then
           self:push_button(button.name)
        elseif(button.pushed and not self:is_need_pushed(button.name)) then
           self:push_button(button.name)
        end
    end
    
    DelayedExec(0.5, function()
        self:open_safe()
    end, 113)
    
    return true;
end

function GameScreen:onObjectClick(item_name) 
    
    if(item_name == "skip") then
        self:OnSkip()
    end
    
    if(clApplication.lock_cursor) then return end
    
    self:push_button(item_name);
    
    if(self:IsMinigameCompleted() and GetState("zoom_004", "state") ~= "opened") then
        SetState("zoom_004", "state", "opened")
        EffectorFinish("buttons_hint")
        ItemChangeOpacity("buttons_hint", 0.0, 0.3)
        self:open_safe();
    end
    
    if(item_name == "button_hand") then
        SoundPlay("sound/safe_button.ogg")
        ItemChangeOpacity("button_push", 1.0, 0.3)
        SetState("screen_004_secretroom", "state", "opened")
        ItemRemove(self.parent.name, "safe_eye")
        DelayedExec(1.0, function()
            self.parent:CloseZoom();
            SetState("screen_002_hall", "state", "security")
            self.parent:CandleLighting(true)
            DelayedExec(1.0, function()
                self.parent:DoorOpen()
            end, 113)
        end, 113)
    end
end

function GameScreen:open_safe(immediately)
    HUDScreen:SetInvntoryOnlyMode()
    
    --SetState("screen_004_secretroom", "state", "opened")
    
    if(not immediately) then
        ItemRotate("safe_handle", M_PI * 2, 0.8)
        SoundPlay("sound/safe_open.ogg")
        DelayedExec(1.1, function()
            self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "safe_open", "graphics/screens/04_secretroom/zoom_safe/safe_open", 633, 244);
            self:CreateObj(OBJ_TYPE.TRIGGER, "button_hand", "graphics/screens/04_secretroom/zoom_safe/button_hand", 867, 569);
            self:CreateObj(OBJ_TYPE.NO_ACTIVE, "button_push", "graphics/screens/04_secretroom/zoom_safe/button_push", 840, 541);
            ItemSetOpacity("button_push", 0.0)
        
            ItemSetOpacity("safe_open", 0.0);
            ItemChangeOpacity("safe_open", 1.0, 0.4)
        end, 113)
    else
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "safe_open", "graphics/screens/04_secretroom/zoom_safe/safe_open", 633, 241);
        self:CreateObj(OBJ_TYPE.TRIGGER, "button_hand", "graphics/screens/04_secretroom/zoom_safe/button_hand", 867, 569);
        self:CreateObj(OBJ_TYPE.NO_ACTIVE, "button_push", "graphics/screens/04_secretroom/zoom_safe/button_push", 840, 541);
        ItemSetOpacity("button_push", 0.0)
    end
end

function GameScreen:InitButtons()
    local buttons_names = {"button_grate", "button_0", "button_1", "button_2", "button_3", "button_4", 
                    "button_5", "button_6", "button_7", "button_8", "button_9", "button_star"}
                    
    local sound_names = {"safe_button_01", "safe_button_05", "safe_button_01", "safe_button_02", "safe_button_03", "safe_button_04", 
                    "safe_button_05", "safe_button_01", "safe_button_02", "safe_button_03", "safe_button_04", "safe_button_02"}
                                    
    self.buttons = {}
    for key, value in pairs(buttons_names) do
        local button = {name = value, pushed = false, sound = sound_names[key]}
        if(GetState(CURRENT_SCREEN, value) == "pushed") then
            button.pushed = true;
        end
        table.insert(self.buttons, button)
    end
    
    self:update_buttons()
end

function GameScreen:push_button(item_name)
    if(self.buttons) then
        for key, button in pairs(self.buttons) do
            if(button.name == item_name) then
                button.pushed = not button.pushed; 
                SoundPlay("sound/"..button.sound..".ogg")
                if(button.pushed) then
                    SetState(CURRENT_SCREEN, button.name, "pushed")
                else
                    SetState(CURRENT_SCREEN, button.name, "")
                end
                self:update_buttons();
                return;
            end
        end
    end
end

function GameScreen:update_buttons()
    for key, button in pairs(self.buttons) do
        if(button.pushed) then
            ItemSetOpacity(button.name, 0.0)
        else
            ItemSetOpacity(button.name, 1.0)
        end
    end
end

function GameScreen:IsMinigameCompleted()
    for key, button in pairs(self.buttons) do
        if(not button.pushed and self:is_need_pushed(button.name)) then
            return false;
        elseif(button.pushed and not self:is_need_pushed(button.name)) then
            return false;
        end
    end
    
    return true;
end

function GameScreen:is_need_pushed(item_name)
    for key, button in pairs(self.corrent_code) do
        if(item_name == button) then
            return true;
        end
    end
    
    return false;
end


function GameScreen:show_hint()
    
    EffectorPlay(__item_hint_eff, "buttons_hint", false)

    DelayedExec(15, function()
        if(GetState("zoom_004", "state") ~= "opened") then
            self:show_hint();
        end
    end, 113)
end

function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "back_trigger", "graphics/trigger", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
	ItemSetOpacity("back_trigger", 0.7)
	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "safe_closed", "graphics/screens/04_secretroom/zoom_safe/safe_closed", 530, 238);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_grate", "graphics/screens/04_secretroom/zoom_safe/button_grate", 975, 638);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_0", "graphics/screens/04_secretroom/zoom_safe/button_0", 936, 638);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_star", "graphics/screens/04_secretroom/zoom_safe/button_star", 896, 638);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_9", "graphics/screens/04_secretroom/zoom_safe/button_9", 977, 600);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_8", "graphics/screens/04_secretroom/zoom_safe/button_8", 937, 600);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_7", "graphics/screens/04_secretroom/zoom_safe/button_7", 896, 600);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_6", "graphics/screens/04_secretroom/zoom_safe/button_6", 977, 562);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_5", "graphics/screens/04_secretroom/zoom_safe/button_5", 937, 562);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_4", "graphics/screens/04_secretroom/zoom_safe/button_4", 896, 562);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_3", "graphics/screens/04_secretroom/zoom_safe/button_3", 977, 524);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_2", "graphics/screens/04_secretroom/zoom_safe/button_2", 937, 524);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_1", "graphics/screens/04_secretroom/zoom_safe/button_1", 896, 524);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "safe_handle", "graphics/screens/04_secretroom/zoom_safe/safe_handle", 727, 532);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "buttons_hint", "graphics/screens/04_secretroom/location/buttons_hint", 285, 383);
    ItemSetOpacity("buttons_hint", 0.0)
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "zoom_close", "graphics/interface/hud/close_minigame", 1504, 1008);
    
	clScreenManager:add_screen("hud") 
end