GameScreen.textures_path = "graphics/screens/08_portal/location/"

GameScreen.hint_table = {
    {obj = {"cupboard_hint", 157, 365}, fun = function() if GetState(CURRENT_SCREEN, "state") == "microscope" then return true end end},
    {obj = {"pc_hint", 1265, 794}, fun = function() if GetState(CURRENT_SCREEN, "state") == "microscope_finish" then return true end end, tool = "cd_003"},
    {obj = {"monitor_hint", 1267, 547}, fun = function() if GetState(CURRENT_SCREEN, "state") == "password" then return true end end},
    
    {obj = {"generator_hint", 866, 486}, fun = function() if GetState(CURRENT_SCREEN, "state") == "generator" then return true end end},
}
--generator_hint 866 486

eye_eff = {
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 0.0, time = 0.1, end_value = 0.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 0.5, time = 0.1, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 1.5, time = 0.1, end_value = 0.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 2.0, time = 0.1, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 3.5, time = 0.1, end_value = 0.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 4.0, time = 0.1, end_value = 1.0},
	    
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 5.5, time = 0.1, end_value = 0.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 6.0, time = 0.1, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 6.5, time = 0.1, end_value = 0.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 7.0, time = 0.1, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 10.5, time = 0.1, end_value = 0.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 11.0, time = 0.1, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 13.5, time = 0.1, end_value = 0.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 14.0, time = 0.1, end_value = 1.0},
	    
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 14.5, time = 0.1, end_value = 0.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 15.0, time = 0.1, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 17.5, time = 0.1, end_value = 0.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 18.0, time = 0.1, end_value = 1.0},
	    
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 18.5, time = 0.1, end_value = 0.0},
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 19.0, time = 0.1, end_value = 1.0},
	    
	    {key=EFFECTOR_MODIFIER.KEY_OPACITY, start_time = 28.0, time = 0.1, end_value = 1.0},
    }

function GameScreen:RestoreScene()
    self:SetItemCursor("generator_hand", "hand")
    
    StopSound(pc_sound_mg, 0.3)
    
    if(GetState(CURRENT_SCREEN, "state") == "") then
        DelayedExec(0.2, function()
            --стартовый диалог
            local comics_finish = function()
                SetState(CURRENT_SCREEN, "state", "microscope")
                HUDScreen:ShowHintText({path = self.textures_path.."hint_08", x = 531, y = 1128})
                self:CreateLocationPortal("cupboard_eye", "mg_008_microscope", "eye");
            end
            
            comics = clScreenManager:add_screen("008_portal_dlg") 	
            comics.on_finish = comics_finish;
        end, 113)
    elseif(GetState(CURRENT_SCREEN, "state") == "microscope") then
        self:CreateLocationPortal("cupboard_eye", "mg_008_microscope", "eye");
    elseif(GetState(CURRENT_SCREEN, "state") == "microscope_finish") then
        self:SetItemCursor("pc_hand", "hand")
        HUDScreen:ShowTips({path = self.textures_path.."text_08", x = 317, y = 1130})
        
        local cd_apply = function()            
            ItemChangeOpacity("activate_on", 1.0, 0.3)
            pc_sound = SoundPlay("sound/pc_on.ogg")
            DelayedExec(4, function()
                pc_sound = SoundPlay("sound/pc_idle.ogg", true)
            end, 113)
            self:RemoveItem("pc_hand")
            Inventory:ActiveToolApply()
            SetState(CURRENT_SCREEN, "state", "password") 
            self:CreateLocationPortal("monitor_eye", "mg_008_password", "hand");
        end 
        self:AddToolApplyZone("pc_hand", "cd_003", cd_apply, true);
    elseif(GetState(CURRENT_SCREEN, "state") == "password") then
        pc_sound = SoundPlay("sound/pc_idle.ogg", true)
        ItemSetOpacity("activate_on", 1.0)
        self:CreateLocationPortal("monitor_eye", "mg_008_password", "hand");
    elseif(GetState(CURRENT_SCREEN, "state") == "generator") then
        pc_sound = SoundPlay("sound/pc_idle.ogg", true)
        ItemSetOpacity("ok", 1.0)
        HUDScreen:ShowHintText({path = self.textures_path.."hint_08a", x = 319, y = 1128})
        self:CreateLocationPortal("generator_hand", "009_generator", "hand");
    elseif(GetState(CURRENT_SCREEN, "state") == "museum") then
        --pc_sound = SoundPlay("sound/pc_idle.ogg", true)
        ItemSetOpacity("hint", 0.0)
        self:CreateObj(OBJ_TYPE.NO_ACTIVE, "08_portal_active", "graphics/screens/08_portal/location/08_portal_active", 0, 0);
        self:CreateAnim(OBJ_TYPE.NO_ACTIVE, "portal_anim", "graphics/screens/08_portal/location/portal_anim", 533, 379);
        AnimationPlay("portal_anim", "main")
    
        --HUDScreen:ShowTips({path = self.textures_path.."text_08c", x = 425, y = 1131})
        --TODO: анимация портала
        SetState("screen_chapters_screen", "current_chapter", "3")
        self:CreateLocationPortal("portal_hand", "chapters_screen", "hand");
        self:CreateLocationPortal("generator_hand", "009_generator", "hand");
    end
    
    if(GetState(CURRENT_SCREEN, "state") == "museum") then
        MusicPlay({"sound/ambient/portal_active.ogg"}, 8.1)
        if(GetState(CURRENT_SCREEN, "crystal") ~= "found") then
            self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "crystal_08_part_dark", "graphics/screens/08_portal/location/crystal_08_part_dark", 325, 352);
            self:AddHiddenCrystal("crystal_08_part_dark")
        end
        
        if(GetState(CURRENT_SCREEN, "dlg2") ~= "true") then
            SetState(CURRENT_SCREEN, "dlg2", "true")
            local comics_finish = function()
                HUDScreen:SetInvntoryOnlyMode()
            end
            
            comics = clScreenManager:add_screen("008_portal_dlg2") 	
            comics.on_finish = comics_finish;
         else
            HUDScreen:SetInvntoryOnlyMode()
         end
    else
        MusicPlay({"sound/ambient/08_ambient.ogg", "sound/music/basement2.ogg"}, 8)
        if(GetState(CURRENT_SCREEN, "crystal") ~= "found") then
            self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "crystal_08_part", "graphics/screens/08_portal/location/crystal_08_part", 325, 352);
            self:AddHiddenCrystal("crystal_08_part")
        end
    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()

    EffectorPlay(eye_eff, "eye", true)
    
    self:RestoreScene();
    
    
    
    self:CreateLocationPortal("basement_hand", "006_basement", "hand")
    
    self:SetItemSound("cupboard_glass", "sound/glass_door.ogg");
    self:SetItemSound("xrays", "sound/xray_01.ogg");
    self:SetItemSound("talking_device", "sound/walkie_03.ogg");
    self:SetItemSound("eye_bottle", "sound/eye.ogg");
    self:SetItemSound("lamp_03", "sound/lamp_hit_03.ogg");
    self:SetItemSound("lamp_02", "sound/lamp_hit_02.ogg");
    self:SetItemSound("lamp_01", "sound/lamp_hit_01.ogg");
    self:SetItemSound("hand_boo", "sound/hand_boo.ogg");
end    
------------------------------------------------------------------------------------------------------
function GameScreen:onObjectClick(item_name)
    if(not self.tip_lock and item_name == "pc_hand" and GetState(CURRENT_SCREEN, "state") == "microscope_finish") then
        self.tip_lock = true
        DelayedExec(2.0, function()
            self.tip_lock = false;
        end, 113)
        HUDScreen:ShowTips({path = self.textures_path.."text_08a", x = 460, y = 1130})
    end
    
    if(item_name == "generator_hand" and GetState(CURRENT_SCREEN, "state") == "generator") then
        SoundPlay("sound/chief_door_open.ogg")
    end
    
    if(item_name == "generator_hand" and GetState(CURRENT_SCREEN, "state") ~= "generator" and GetState(CURRENT_SCREEN, "state") ~= "museum") then
        HUDScreen:ShowTips({path = self.textures_path.."Text_08d", x = 783, y = 1129})
    end
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------

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

    self:CreateObj(OBJ_TYPE.TRIGGER, "hand_boo", "graphics/screens/08_portal/location/hand_boo", 1507, 185);
    self:CreateObj(OBJ_TYPE.TRIGGER, "monitor_eye", "graphics/screens/08_portal/location/monitor_eye", 1281, 561);
    self:CreateObj(OBJ_TYPE.TRIGGER, "pc_hand", "graphics/screens/08_portal/location/pc_hand", 1279, 808);
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "cupboard_glass", "graphics/screens/08_portal/location/cupboard_glass", 20, 405);    
    self:CreateObj(OBJ_TYPE.TRIGGER, "xrays", "graphics/screens/08_portal/location/xrays", 1146, 388);
    self:CreateObj(OBJ_TYPE.TRIGGER, "talking_device", "graphics/screens/08_portal/location/talking_device", 1038, 497);
    self:CreateObj(OBJ_TYPE.TRIGGER, "eye_bottle", "graphics/screens/08_portal/location/eye_bottle", 107, 185);
    self:CreateObj(OBJ_TYPE.TRIGGER, "lamp_03", "graphics/screens/08_portal/location/lamp_03", 1214, 178);
    self:CreateObj(OBJ_TYPE.TRIGGER, "lamp_02", "graphics/screens/08_portal/location/lamp_02", 826, 345);
    self:CreateObj(OBJ_TYPE.TRIGGER, "lamp_01", "graphics/screens/08_portal/location/lamp_01", 462, 377);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "eye", "graphics/screens/08_portal/location/eye", 135, 278);
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "portal_hand", "graphics/screens/08_portal/location/portal_hand", 533, 379);
    self:CreateObj(OBJ_TYPE.TRIGGER, "generator_hand", "graphics/screens/08_portal/location/generator_hand", 875, 494);
    self:CreateObj(OBJ_TYPE.TRIGGER, "cupboard_eye", "graphics/screens/08_portal/location/cupboard_eye", 166, 374);

    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "cupboard_open", "graphics/screens/08_portal/location/cupboard_open", 170, 375);
    
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "activate_on", "graphics/screens/08_portal/location/activate_on", 1299, 578);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "ok", "graphics/screens/08_portal/location/ok", 1298, 577);
    ItemSetOpacity("activate_on", 0.0)
    ItemSetOpacity("ok", 0.0)
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "basement_hand", "graphics/screens/08_portal/location/basement_hand", 1511, 400);  
    
    clScreenManager:add_screen("hud") 
end