class 'ClockScreen' (BaseScreen)

local BOTTOM_BLACK_BAR_SHOW_OPACITY      =  0.5
local BOTTOM_BLACK_BAR_HIDE_OPACITY      =  0

function ClockScreen:__init(name, gameSceneName)
	BaseScreen.__init(self, name)

	self.previous_scene_name = nil
	if gameSceneName then
		self.previous_scene_name = gameSceneName
	end

	-- tutorial shit
	self.tutorial = Tutorial(self, Tutorial.tips)

	local function g8y3eCreateTutorialScreen(tutorial, id)
		local tip = tutorial.tips[ id ]
		local bGameSceneOnTop = PopUpScreensMgr:GetTop():getName() == tutorial.scene:getName()

		local screenFile = tip.screenFile or 'Data/Scripts/Screens/TutorialScreen.lua'
		local className	 = ExtractFileName(screenFile)
		local screen	 = CreatedScreen(screenFile, className, tutorial)
		screen:setId(id)
		
	    if tip.constructor then
	    	tip.constructor(screen, self)
	    end
	    
	    PopUpScreensMgr:push(screen, false)

	    if bGameSceneOnTop then
	    	tutorial.scene:go()
	    end

	    return screen
	end

	local function g8y3eShowTip(tutorial, id, delay, bHardShow, bMultyShow)
		local tip = tutorial.tips[id]
		assert(tip)

		delay = delay or tutorial.showTipDelay

		local bActive	 = bHardShow or tutorial:isActive()
		local bCompleted = not bMultyShow and tutorial:isTipCompleted(id)

		if bActive and not bCompleted then		
			tutorial:createTutorialScreen(id)			
		end
	end

	self.tutorial.showTip = g8y3eShowTip
	self.tutorial.createTutorialScreen = g8y3eCreateTutorialScreen  

	-- fackin patlatiy
	local clock_screen = self
	
	local function barMoveIn(self, time)
		self:moveOut(0)
		self:show()
		self.bar:destroyEffectors()
		self.textField:destroyEffectors()

		if self.time_in then
			self.time_in:kill()
		end

		if time == 0 then
			self.bar:setOpacity(BOTTOM_BLACK_BAR_SHOW_OPACITY)
			self.textField:setOpacity(1)
		else
			self.bar:ChangeOpacity(self.bar:getOpacity(), BOTTOM_BLACK_BAR_SHOW_OPACITY, time)
			self.textField:ChangeOpacity(self.textField:getOpacity(), 1, time)
		end

		self.time_in = clock_screen.timer:add( function()
													self:moveOut(time)
											   end, 2.0)
		--clock_screen:setBlackBarVisible(true, time)		
	end

	local function barMoveOut(self, time)
		self.bar:destroyEffectors()
		self.textField:destroyEffectors()

		if self.time_in then
			self.time_in:kill()
		end

		if time == 0 then
			self.bar:setOpacity(0)
			self.textField:setOpacity(0)
			self:hide()
		else
			local eff = self.bar:ChangeOpacity(self.bar:getOpacity(), 0, time)
			self.textField:ChangeOpacity(self.textField:getOpacity(), 0, time)
			
			eff.onFinish:add(function()
								self:hide()
							 end)
		end
			
		--clock_screen:setBlackBarVisible(false, time)
	end

	self.blackBar = BlackBar(self)
	self:getEntityNamed('black_bar'):setColor( Colorf(0, 0, 0, 0.5) )
	self:getEntityNamed('black_bar'):attachTo( self:getMainEntity() )
	self:getEntityNamed('text_field'):attachTo( self:getMainEntity() )
	self:getEntityNamed('black_bar'):setOrder( 100 )
	self:getEntityNamed('text_field'):setOrder( 100 )
	self:getEntityNamed('text_field'):setXY( 512, 90 )
	self:getEntityNamed('black_bar'):setSize(1366, 80)
	self:getEntityNamed('black_bar'):setXY(512, 180)

	self.blackBar.moveIn = barMoveIn
	self.blackBar.moveOut = barMoveOut

	self.blackBar:hideText(0)
	----------------------------------------------------------

	self:initSound()

	self.chains_anim = self:getEntityNamed('compositionBG'):getElementByName('chains_idle')

	self:getEntityNamed('bttn_home').onPress:add(function() self:onScreenDestroy() end)

	self:registerBBT('area_click_portal', 'ROOM_PORTAL_BBT')

	local function on_click_clock(event) 
		local index = string.sub(event.target, -1)
		local dx = self:getEntityNamed('effect_' .. index):getX()
		local dy = self:getEntityNamed('effect_' .. index):getY()

		if not event.save then
			self:getEntityNamed('layer_clock_' .. index):MoveTo(dx, dy, 1.0)

			playCommonSound('ClockDestroyLocks')

			self.timer:add( function()							
								self:getEntityNamed('layer_clock_' .. index):ChangeOpacity(1, 0, 0.5)
								self.chains_anim:getElementByName('lock_' .. index):ChangeOpacity(1, 0, 0.5)
								self:getEntityNamed('effect_' .. index):show()
								self:getEntityNamed('effect_' .. index):play()
							end, 1.0)
		else
			self:getEntityNamed('layer_clock_' .. index):hide()		
			self.chains_anim:getElementByName('lock_' .. index):setOpacity(0)		
		end
	end

	local clock_name = ''
	if self:isClockFinded('clock_1') then		
		self:registerClockClickArea('area_click_clock_1', on_click_clock)	
		clock_name = 'area_click_clock_1'
	else	
		self:entityHide('layer_clock_1','area_click_clock_1') 	
	end		

	if self:isClockFinded('clock_2') then	
		self:registerClockClickArea('area_click_clock_2', on_click_clock)
		clock_name = 'area_click_clock_2'
	else	
		self:entityHide('layer_clock_2','area_click_clock_2') 	
	end		

	if self:isClockFinded('clock_3') then
		self:registerClockClickArea('area_click_clock_3', on_click_clock)
		clock_name = 'area_click_clock_3'
	else	
		self:entityHide('layer_clock_3','area_click_clock_3') 	
	end		

	if self:isClockFinded('clock_4') then
		self:registerClockClickArea('area_click_clock_4', on_click_clock)
		clock_name = 'area_click_clock_4'
	else	
		self:entityHide('layer_clock_4','area_click_clock_4') 	
	end		

	if self:isClockFinded('clock_5') then
		self:registerClockClickArea('area_click_clock_5', on_click_clock)
		clock_name = 'area_click_clock_5'
	else	
		self:entityHide('layer_clock_5','area_click_clock_5') 	
	end		

	if self:isClockFinded('clock_6') then
		self:registerClockClickArea('area_click_clock_6', on_click_clock)
		clock_name = 'area_click_clock_6'
	else	
		self:entityHide('layer_clock_6','area_click_clock_6') 	
	end		
	
	if self:isClockFinded('clock_7') then
		self:registerClockClickArea('area_click_clock_7', on_click_clock)
		clock_name = 'area_click_clock_7'
	else	
		self:entityHide('layer_clock_7','area_click_clock_7') 	
	end		

	if (clock_name ~= '') then
		self.clock_name = clock_name
		self.timer:add( function()
							self.tutorial:showTip('CLOCK_SCREEN_CLICK', 0, true)
						end, 1.0)
	end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function ClockScreen:setBlackBarVisible(visible, time, key)
    local targetOpacity = (visible) and BOTTOM_BLACK_BAR_SHOW_OPACITY or BOTTOM_BLACK_BAR_HIDE_OPACITY  
    local obj     = self:getEntityNamed('black_bar')

    if self.blackBarEff then
        self.blackBarEff:destroy()
        self.blackBarEff = nil
    end

    if time == 0 then
        obj:setOpacity(targetOpacity)
    else
        local requiredTime = math.abs(targetOpacity - obj:getOpacity())/(BOTTOM_BLACK_BAR_SHOW_OPACITY - BOTTOM_BLACK_BAR_HIDE_OPACITY) * time
    
        local eff = CreateAnimationEffector()
        eff:attachTo(obj)
        eff:setKey(_TRACK_O, 0,            obj:getOpacity())
        eff:setKey(_TRACK_O, requiredTime,         targetOpacity)
        eff:play()
    
        self.blackBarEff = eff
    end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function ClockScreen:initSound()
	local function createClockSoundEntity()
	    local main_sound = CreateAmbientTrack('Data/Sounds/GUI/ClockScreen/ClockScreen_Internoise.ogg') -- CreateSoundTrack
	    main_sound:setName('ClockInternoise')
	    main_sound:setRepeated(true)
	    main_sound:setFadeDuration(0)
	    main_sound:setFading(0)
	    main_sound:play()

	    return main_sound
	end

	local root = getGameRoot()
	local sound_entity = root:getEntityNamed('ClockInternoise')
	if sound_entity then
		sound_entity:attachTo( self:getMainEntity() )
	else
		local sound_entity = createClockSoundEntity()
		sound_entity:attachTo( self:getMainEntity() )
	end
end

-------------------------------------------------------------------------------
function ClockScreen:registerClockClickArea(area_object, callback, cursor_name)
	local event = {}
	event.target = area_object

	cursor_name = cursor_name or 'CursorActive'

	if not HasGameState('CLOCK_SCREEN_CLICKED', area_object, GENERAL_TAG_NAME) then

		self:getEntityNamed(area_object):setCursorOverName(cursor_name)

		local function on_click_clock()
			self.blackBar:moveOut(0)
			self:getEntityNamed(area_object):hide()
			AddUniqueGameState('CLOCK_SCREEN_CLICKED', area_object, GENERAL_TAG_NAME)

			event.save = false
			callback(event)
		end

		self:getEntityNamed(area_object).onPress:add( function() on_click_clock() end)
	else	
		self:getEntityNamed(area_object):hide()

		event.save = true
		callback(event)
	end
end
-------------------------------------------------------------------------------

function ClockScreen:registerBBT(obj_name, text_alias, alias_order, ignore_highlight_on_click, do_not_show_in_hard_mode)
	alias_order = alias_order or EBBTAliasOrder.SIMPLE
		
	local function on_click(event)	
		if not event.save then
			if do_not_show_in_hard_mode and (not getPlayModeParameters().blackbarEnabled) then -- isHardPlayMode() 
				return
			end
			local text_alias_type = type(text_alias)
			 
			if text_alias_type == 'table' then
				local aliasCount = #text_alias
				local textKey    = nil
				
				if alias_order == EBBTAliasOrder.RANDOM then
					local index = math.random(aliasCount)
					textKey     = text_alias[index]
					
				elseif alias_order == EBBTAliasOrder.LOOP then
					local index = 0;
					local state = string.upper('MONOLOG_LOOP_STEP_'..self.name..'_'..obj_name)
					
					if HasGameState(state) then
						index = tonumber( GetGameState(state):get(0) )
					end
					
					index   = (index < aliasCount) and (index + 1) or 1
					textKey = text_alias[index]
					
					SetGameState(state, tostring(index))
					
				elseif alias_order == EBBTAliasOrder.SIMPLE then
					for _, _textKey in ipairs(text_alias) do
						textKey = _textKey
						if not HasGameState(BLACK_BAR_SHOWN, textKey) then
							break;
						end
					end
				end
				
				if textKey then
					self.blackBar:showText(textKey)
				end
				
			elseif text_alias_type == 'string' then				
				self.blackBar:showText(text_alias)
			else
				assert(false and 'text_alias must be a string or table')
			end
		end		
	end

	self:getEntityNamed(obj_name):setCursorOverName('CursorQuestion')
	self:getEntityNamed(obj_name).onClickBegin:add(on_click)
	self:getEntityNamed(obj_name):setSendEventsToParent(false)
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function ClockScreen:getClockClickedState(objectName)
	return CLOCK_ROOM..'('..objectName..')'
end
-------------------------------------------------------------------------------

function ClockScreen:isClockFinded(objectName)
	local state = self:getClockClickedState(objectName)
	return HasGameState(CLOCK_CLICKED, state, GENERAL_TAG_NAME)
end


-------------------------------------------------------------------------------
function ClockScreen:onScreenDestroy()
	PopUpScreensMgr:pop()
	if (not self.previous_scene_name) then
		createMainMenu()
	else
		current_scene = GameScene(self.previous_scene_name)
        PopUpScreensMgr:push(current_scene, false, nil)
	end
end
-------------------------------------------------------------------------------

--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function ClockScreen:CreateLoaderTable()
	local objects =
	{
		{ name = 'bg',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Scenes/Clock/Layers/Bg.jpg',  width = 1366,  height = 768,  },
		{ name = 'anchor_areas',  entityType = 'SpriteEntity',  x = 0,  y = 0,  order = 20,  color = {r = 1, g = 1, b = 1, a = 0},  editingLocked = false,  treeItemIsExpanded = true,  width = 2,  height = 2,  },
		{ name = 'area_click_clock_1',  entityType = 'SpriteEntity',  parentName = 'anchor_areas',  x = 260,  y = 689,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 104,  height = 90,  },
		{ name = 'area_click_clock_2',  entityType = 'SpriteEntity',  parentName = 'anchor_areas',  x = 151,  y = 576,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 108,  height = 150,  },
		{ name = 'area_click_clock_3',  entityType = 'SpriteEntity',  parentName = 'anchor_areas',  x = 61,  y = 272,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 119,  height = 113,  },
		{ name = 'area_click_clock_4',  entityType = 'SpriteEntity',  parentName = 'anchor_areas',  x = 771,  y = 637,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 150,  height = 150,  },
		{ name = 'area_click_clock_5',  entityType = 'SpriteEntity',  parentName = 'anchor_areas',  x = 950,  y = 157,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 150,  height = 150,  },
		{ name = 'area_click_clock_6',  entityType = 'SpriteEntity',  parentName = 'anchor_areas',  x = 939,  y = 405,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 150,  height = 150,  },
		{ name = 'area_click_clock_7',  entityType = 'SpriteEntity',  parentName = 'anchor_areas',  x = 942,  y = 679,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 150,  height = 150,  },
		{ name = 'area_click_portal',  entityType = 'SpriteEntity',  parentName = 'anchor_areas',  x = 513,  y = 321,  order = 6,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 453,  height = 504,  },
		{ name = 'anchor_sprites',  entityType = 'SpriteEntity',  x = 0,  y = 0,  order = 10,  color = {r = 1, g = 1, b = 1, a = 0},  editingLocked = false,  treeItemIsExpanded = true,  width = 2,  height = 2,  },
		{ name = 'layer_clock_1',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 259,  y = 689,  order = 10,  scale = 0.4,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Layers/clock1.png',  width = 250,  height = 250,  },
		{ name = 'layer_clock_2',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 152,  y = 588,  order = 10,  angle = -12,  scale = 0.5,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Layers/clock2.png',  width = 250,  height = 250,  },
		{ name = 'layer_clock_3',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 51,  y = 284,  order = 10,  scale = 0.34,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Layers/clock3.png',  width = 250,  height = 250,  },
		{ name = 'layer_clock_4',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 769,  y = 645,  order = 40,  angle = 20,  scaleX = 0.48,  scaleY = 0.4,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Layers/clock4.png',  width = 250,  height = 250,  },
		{ name = 'layer_clock_5',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 964,  y = 152,  order = 10,  scale = 0.54,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Layers/clock5.png',  width = 250,  height = 250,  },
		{ name = 'layer_clock_6',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 937,  y = 407,  order = 10,  angle = 18,  scale = 0.5,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Layers/clock6.png',  width = 250,  height = 250,  },
		{ name = 'layer_clock_7',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 951,  y = 693,  order = 10,  angle = 62,  scaleX = 0.45,  scaleY = 0.61,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Layers/clock7.png',  width = 250,  height = 250,  },
		{ name = 'ClockScreen_Anim_Bg',  entityType = 'CompositionContainer',  parentName = 'anchor_sprites',  x = -171,  y = 1,  order = 18,  scaleX = 1.07,  scaleY = 1.03,  editingLocked = false,  treeItemIsExpanded = true,  path = 'Data/Scripts/Screens/Anim/ClockScreen_Anim_Bg.lua',  },
		{ name = 'compositionBG',  entityType = 'CompositionSequence',  parentName = 'ClockScreen_Anim_Bg',  x = 0,  y = 0,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  isCyclic = true,  speed = 1,  },
		{ name = 'effect_1',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 700,  y = 299,  order = 100,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Animations/lock_effect_[].png',  width = 164,  height = 150,  fps = 20,  framesCount = 24,  },
		{ name = 'effect_2',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 560,  y = 439,  order = 100,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Animations/lock_effect_[].png',  width = 164,  height = 150,  fps = 20,  framesCount = 24,  },
		{ name = 'effect_3',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 180,  y = 55,  order = 100,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Animations/lock_effect_[].png',  width = 164,  height = 150,  fps = 20,  framesCount = 24,  },
		{ name = 'effect_4',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 458,  y = 504,  order = 100,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Animations/lock_effect_[].png',  width = 164,  height = 150,  fps = 20,  framesCount = 24,  },
		{ name = 'effect_5',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 783,  y = 151,  order = 100,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Animations/lock_effect_[].png',  width = 164,  height = 150,  fps = 20,  framesCount = 24,  },
		{ name = 'effect_6',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 376,  y = 367,  order = 100,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Animations/lock_effect_[].png',  width = 164,  height = 150,  fps = 20,  framesCount = 24,  },
		{ name = 'effect_7',  entityType = 'SpriteEntity',  parentName = 'anchor_sprites',  x = 259,  y = 224,  order = 100,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Animations/lock_effect_[].png',  width = 164,  height = 150,  fps = 20,  framesCount = 24,  },
		{ name = 'ClockScreen_Anim_Portal',  entityType = 'CompositionContainer',  parentName = 'anchor_sprites',  x = -120,  y = -48,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  path = 'Data/Scripts/Screens/Anim/ClockScreen_Anim_Portal.lua',  },
		{ name = 'compositionPortal1',  entityType = 'CompositionSequence',  parentName = 'ClockScreen_Anim_Portal',  x = 0,  y = 0,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  isCyclic = true,  speed = 1,  },
		{ name = 'bttn_home',  entityType = 'ButtonEntity',  x = 512,  y = 724,  order = 30,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Scenes/Clock/Layers/bttn_home.png',  text = '',  activeRectX = 208,  activeRectY = 50,  },
		{ name = 'Layer_title',  entityType = 'SpriteEntity',  x = 512,  y = 36,  order = 600,  scale = 0.95,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/Clock/Layers/Layer_title.PNG',  width = 316,  height = 62,  },
	}
	return objects
end
