dofile('Data/Scripts/Common/Screens/NotesScreenBase.lua')
dofile('Data/Scripts/Map/MapScreen.lua')
dofile('Data/Scripts/PhotoGallery/PhotoGallery.lua')

class 'PhoneScreen' (NotesScreenBase)

local TASK_DESCRIPTION_PREFIX = 'GAME_TASK_DESCRIPTION_'

local EPhoneMode =
{
	TASKS	 	= 'TASKS',
	NOTES 		= 'NOTES',
	MAP	 		= 'MAP',
	GALLERY	 	= 'GALLERY'
}
-------------------------------------------------------------------------------
local CENTER_SCREEN = 512
local DTIME 		= 0.3
local DY 			= 30
local PATH_TEXTURE	= 'Data/Textures/Screens/Phone/'
-------------------------------------------------------------------------------

function PhoneScreen:__init(name)
	BaseScreen.__init(self, name)		
	
	self.pageSlotState 		= 'PageSlot'
	self.activePageState	= 'NotebookActivePage'
	self.activeModeState	= 'PhoneActiveMode'
		
	self.notes              = {}
	self.pages              = nil

	self.map 			= MapScreen(self)
	self.gallery 			= PhotoGallery(self)
	self.mouseBlocker	= MouseBlocker:new( self:getMainEntity() )

	self:getEntityNamed('anchor_content'):setScissorBox(80, -640, 960, -140)
	self:getEntityNamed('anchor_content'):setRespondable(true)
	
	self:restorePages()
	self:setupButtons()
	
	if not HasGameState(HAS_NOT_SHOWN_TASKS) then
	    self:entityHide('tasks_updated')
	end
	
	if not HasGameState(HAS_NOT_SHOWN_NOTES) then
	    self:entityHide('notes_updated')
	end
	
	self.timer:add(	function()
						local fade_in_on_load = self:getEntityNamed('fade_in_on_load')
						fade_in_on_load.onClickBegin:add(bind(self.onCloseFadeClick, self))

						fade_in_on_load:setRespondable(true)
					end, 0.6)
	
    self:entityHide('notebook_bg_notes', 'notebook_bg_tasks')
    self:entityHide('tasks_description_txt')
    
    if HasGameState(self.activeModeState) then 
        self:setMode(GetGameState(self.activeModeState):get(0))
    else
        self:setMode(EPhoneMode.NOTES)
    end
    
    self:getEntityNamed('anchor_content'):ChangeScale(0,1,0.2)
end

function PhoneScreen:setupButtons()
	self.buttons = {}
	
	self.buttons.button_tasks = self:getEntityNamed('bttn_tasks')				
    self.buttons.button_tasks.onPress:add(bind(self.onTasksButtonClick, self))
	self.buttons.button_tasks:setSFXTrackOnPress( getCommonSoundEntity('PhoneBttnClick') )
	
	self.buttons.button_notes = self:getEntityNamed('bttn_notes')				
    self.buttons.button_notes.onPress:add(bind(self.onNotesButtonClick, self))
	self.buttons.button_notes:setSFXTrackOnPress( getCommonSoundEntity('PhoneBttnClick') )
	
	self.buttons.button_map = self:getEntityNamed('bttn_map')				
    self.buttons.button_map.onPress:add(bind(self.onMapButtonClick, self))
	self.buttons.button_map:setSFXTrackOnPress( getCommonSoundEntity('PhoneBttnClick') )

	self.buttons.button_gallery = self:getEntityNamed('bttn_gallery')				
    self.buttons.button_gallery.onPress:add(bind(self.onGalleryButtonClick, self))
	self.buttons.button_gallery:setSFXTrackOnPress( getCommonSoundEntity('PhoneBttnClick') )

	self.buttons.button_close = self:getEntityNamed('bttn_close')	
	self.buttons.button_close.onPress:add(bind(self.onCloseButtonClick, self))
	self.buttons.button_close:setSFXTrackOnPress( getCommonSoundEntity('PhoneBttnClick') )

	self.buttons.button_prev = self:getEntityNamed('bttn_prev')
    self.buttons.button_prev.onPress:add(bind(self.onPrevButtonClick, self))
    self.buttons.button_prev:setSFXTrackOnPress( getCommonSoundEntity('PhoneBttnClick') )
    self.buttons.button_prev:hide()
    
	self.buttons.button_next = self:getEntityNamed('bttn_next')
	self.buttons.button_next.onPress:add(bind(self.onNextButtonClick, self))
	self.buttons.button_next:setSFXTrackOnPress( getCommonSoundEntity('PhoneBttnClick') )
	self.buttons.button_next:hide()			
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhoneScreen:onPrevButtonClick()
	local id   = self:getActivePageId()
	local slot = self:getPageSlot(id)
	
	playCommonSound('PhoneBttnClick')

	if slot and slot > 1 then
		local newId = self.pages[slot - 1]
		self:showPage(newId)

		local obj = self.notes[newId].main_entity


		obj:setXY(-CENTER_SCREEN - 400, 0)		

		self.notes[id].main_entity:Move(CENTER_SCREEN+400, 0, DTIME)
		obj:Move(CENTER_SCREEN + 400, 0, DTIME)	

		self.mouseBlocker:push()
		self.timer:add( function()
							self.mouseBlocker:pop()
							self:hidePage(id)
						end, DTIME)

		self:setButtonsEnabled()
	end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhoneScreen:onNextButtonClick()
	local id   = self:getActivePageId()
	local slot = self:getPageSlot(id)
	local size = #self.pages

	playCommonSound('PhoneBttnClick')
		
	if slot and slot < size then
		local newId = self.pages[slot + 1]	
		self:showPage(newId)

		local obj = self.notes[newId].main_entity

		obj:setXY(CENTER_SCREEN + 400, 0)

		self.notes[id].main_entity:Move(-(CENTER_SCREEN + 400), 0, DTIME)
		obj:Move(-(CENTER_SCREEN + 400), 0, DTIME)

		self.mouseBlocker:push()
		self.timer:add( function()
							self.mouseBlocker:pop()
							self:hidePage(id)
						end, DTIME)

		self:setButtonsEnabled()
	end
end
------------------------------------------------------------------------------- ''

function PhoneScreen:addNotePage(id, anchor_name)
	if not self:isPageCreated(id) then
		local filePath = NOTES_FOLDER..id..'.lua'
		if IsLevelCollectorsEdition(current_scene:getPlayField().name) then
		    filePath    =   NOTES_FOLDER_CE..id..'.lua'
		end
		assert(resourceExists(filePath))
		dofile(filePath)
		self.notes[id] = Helpers.CallFunctionByName(id, self, id)
		self.notes[id]:loadPage()
		self.notes[id]:getMainEntity():attachTo(self:getEntityNamed('anchor_note_2'))
	end
end

function PhoneScreen:getCurrentMapPage()
	self.map:getMapPage(0)
end

function PhoneScreen:setMode(mode)
    if mode == self.current_mode then
        return
    end
    
    SetGameState(self.activeModeState, mode)
    
    local instant_change = false
    if not self.current_mode then
        instant_change = true
    end
    
    local activePageId = self:getActivePageId()

    -- hide map screen ---------------------------------------
    self.map:hideScreen()
    self.gallery:hideScreen()
    ----------------------------------------------------------
    
    if mode == EPhoneMode.NOTES then
        if HasGameState(HAS_NOT_SHOWN_NOTES) then
            RemoveGameState(HAS_NOT_SHOWN_NOTES)
	        self:entityHide('notes_updated')	        
	    end

	    self:entityShow('notebook_bg_notes')
	    self:entityHide('notebook_bg_tasks')
    
        self:setButtonsEnabled()
        self:entityShow('number_page_back')    
        self:entityHide('tasks_description_txt')        
        
        self.buttons.button_tasks:setPressed(false)
        self.buttons.button_tasks:setActive(true)

        self.buttons.button_map:setPressed(false)
        self.buttons.button_map:setActive(true)

        self.buttons.button_notes:setPressed(true)
		self.buttons.button_notes:setActive(false)

		self.buttons.button_gallery:setPressed(false)
		self.buttons.button_gallery:setActive(true)
    
	    if activePageId == nil and #self.pages > 0 then
		    activePageId = self.pages[#self.pages]
	    elseif (#self.pages == 0 ) then
	    	self:entityHide('number_page_back') 
	    end

	    self:showPage(activePageId)
    else
        if activePageId and self.current_mode == EPhoneMode.NOTES then
            self:hidePage(activePageId)
        end       

        self:setNotesButtonsDisabled()
        self:entityHide('number_page_back')
    
        if mode == EPhoneMode.TASKS then
            if HasGameState(HAS_NOT_SHOWN_TASKS) then
                RemoveGameState(HAS_NOT_SHOWN_TASKS)
	            self:entityHide('tasks_updated')
	        end
        
            self:entityHide('notebook_bg_notes')
	    	self:entityShow('notebook_bg_tasks')

            self:entityShow('tasks_description_txt')
            
            self.buttons.button_map:setPressed(false)
            self.buttons.button_map:setActive(true)

            self.buttons.button_notes:setPressed(false)
            self.buttons.button_notes:setActive(true)

            self.buttons.button_tasks:setPressed(true)
		    self.buttons.button_tasks:setActive(false)

		    self.buttons.button_gallery:setPressed(false)
		    self.buttons.button_gallery:setActive(true)
            
            local activatedTasks
            local percentCompleted
            activatedTasks, percentCompleted = getActivatedGameTasksDescriptions()
            
            local description = StringManager:getString('TASK_PROCENT_COMPLETED_START') .. percentCompleted .. StringManager:getString('TASK_PROCENT_COMPLETED_END')
            self:getEntityNamed('tasks_description_txt'):setText(description)    

            local index = 1
            for i = 1,#activatedTasks do   
            	if not activatedTasks[i].finished or (activatedTasks[i].finished and i > 10) then        
	                local task_description = '-' .. StringManager:getString(string.upper(TASK_DESCRIPTION_PREFIX..activatedTasks[i].id))
	                local text, task_box = self:createTextTask(index, task_description)

	                if activatedTasks[i].finished then	                	
	                	task_box:setVisible(true)
	                end	    

	                index = index + 1
                end       
            end            
        elseif mode == EPhoneMode.GALLERY then
        	self:entityHide('notebook_bg_notes', 'notebook_bg_tasks')
            self:entityHide('tasks_description_txt')
            
            self.buttons.button_tasks:setPressed(false)
            self.buttons.button_tasks:setActive(true)

            self.buttons.button_notes:setPressed(false)
            self.buttons.button_notes:setActive(true)

            self.buttons.button_map:setPressed(false)
		    self.buttons.button_map:setActive(true)

		    self.buttons.button_gallery:setPressed(true)
		    self.buttons.button_gallery:setActive(false)

		    self.gallery:showScreen()
        else
            self:entityHide('notebook_bg_notes', 'notebook_bg_tasks')
            self:entityHide('tasks_description_txt')
            
            self.buttons.button_tasks:setPressed(false)
            self.buttons.button_tasks:setActive(true)

            self.buttons.button_notes:setPressed(false)
            self.buttons.button_notes:setActive(true)

            self.buttons.button_map:setPressed(true)
		    self.buttons.button_map:setActive(false)

		    self.buttons.button_gallery:setPressed(false)
		    self.buttons.button_gallery:setActive(true)

		    self.map:showScreen()		    	
        end
    end
    
    self.current_mode = mode
end

function PhoneScreen:createTextTask(index, description)
	local textEntity = nil
	local object_box = nil

	if not self:hasEntityNamed( index .. 'TASK_ADDED' ) then
		local parent	= self:getEntityNamed('anchor_tast_text')
		textEntity 		= CreateTextEntity()

		textEntity:attachTo		 ( parent )
		textEntity:setName		 ( index .. 'TASK_ADDED' )
		textEntity:setText		 ( description )
		textEntity:setFont 		 ( 'Data/Fonts/diary_white.fnt' )
		textEntity:setHotSpotType( TextEntity.ETextHotSpotTypeLeftTop )
		textEntity:setVisible	 ( true )
		textEntity:setRespondable( false )
		textEntity:setOrder		 ( 2 )
		textEntity:setHeight	 ( 28 )

		--local prev_y = self:getEntityNamed( index .. 'TASK_ADDED' )
		textEntity:setY 		 ( index * DY + 40)
		textEntity:setColor		 ( Colorf(0, 0, 0, 1) )

		local object	= CreateSpriteEntity( PATH_TEXTURE .. 'task_checkbox_bg.png')

		object:attachTo		 	( textEntity )
		object:setName		 	( index .. 'TASK_ADDED_CB_BG' )
		object:setAnimationSize	( 40, 42 )
		object:setHotSpot 		( 20, 21 )
		object:setVisible	 	( true )
		object:setRespondable	( false )
		object:setOrder		 	( 2 )
		object:setScale		 	( 0.7 )
		object:setXY 		 	( 600, 14 )

		object_box		= CreateSpriteEntity( PATH_TEXTURE .. 'task_checkbox.png')

		object_box:attachTo		 	( textEntity )
		object_box:setName		 	( index .. 'TASK_ADDED_CB' )
		object_box:setAnimationSize	( 56, 52 )
		object_box:setHotSpot 		( 28, 26 )
		object_box:setVisible	 	( false )
		object_box:setRespondable	( false )
		object_box:setOrder		 	( 4 )
		object_box:setScale		 	( 0.7 )
		object_box:setXY 		 	( 608, 8 )		
	else
		textEntity = self:getEntityNamed( index .. 'TASK_ADDED' )
		object_box = self:getEntityNamed( index .. 'TASK_ADDED_CB' )
	end

	return textEntity, object_box
end

function PhoneScreen:onTasksButtonClick()
    self:setMode(EPhoneMode.TASKS)
end

function PhoneScreen:onNotesButtonClick()
    self:setMode(EPhoneMode.NOTES)
end

function PhoneScreen:onMapButtonClick()
    self:setMode(EPhoneMode.MAP)
end

function PhoneScreen:onGalleryButtonClick()
    self:setMode(EPhoneMode.GALLERY)
end

function PhoneScreen:setNotesButtonsDisabled()
	self.buttons.button_prev:setRespondable(false)
	self.buttons.button_prev:setVisible(false)
	
	self.buttons.button_next:setRespondable(false)
	self.buttons.button_next:setVisible(false)
end

function PhoneScreen:showPage(id, time)
    NotesScreenBase.showPage(self, id, time)

	local page_num = self:getPageSlot(id) 
	page_num = page_num and ''..page_num or ''
    self:getEntityNamed('note_current_page_txt'):setText(page_num)
end

function PhoneScreen:setCloseCallback(close_callback)
    self.close_callback = close_callback
end

function PhoneScreen:onCloseButtonClick()
    NotesScreenBase.onCloseButtonClick(self)

    playCommonSound('PhoneBttnClose')
	
    if self.close_callback and type(self.close_callback) == 'function' then
        self.close_callback()
    end
end

function PhoneScreen:onCloseFadeClick()
    NotesScreenBase.onCloseButtonClick(self)
	
    if self.close_callback and type(self.close_callback) == 'function' then
        self.close_callback()
    end
end

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

function PhoneScreen:CreateLoaderTable()
	local objects =
	{
		{ name = 'anchor_content',  entityType = 'SpriteEntity',  x = 0,  y = 770,  order = 60,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 1, y = 1},  width = 2,  height = 2,  },
		{ name = 'notebook_bg_tasks',  entityType = 'SpriteEntity',  parentName = 'anchor_content',  x = 512,  y = -386,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/notebook_bg_tasks.png',  width = 848,  height = 446,  },
		{ name = 'blackout_1',  entityType = 'SpriteEntity',  parentName = 'notebook_bg_tasks',  x = 0,  y = 0,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.498039},  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Screens/Phone/blackout.png',  width = 848,  height = 446,  },
		{ name = 'notebook_bg_notes',  entityType = 'SpriteEntity',  parentName = 'anchor_content',  x = 512,  y = -386,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/notebook_bg_notes.png',  width = 848,  height = 446,  },
		{ name = 'blackout_2',  entityType = 'SpriteEntity',  parentName = 'notebook_bg_notes',  x = 0,  y = 0,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.498039},  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Screens/Phone/blackout.png',  width = 848,  height = 446,  },
		{ name = 'anchor_note_2',  entityType = 'SpriteEntity',  parentName = 'notebook_bg_notes',  x = -451,  y = -384,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 5, y = 5},  width = 10,  height = 10,  },
		{ name = 'tasks_description_txt',  entityType = 'TextEntity',  parentName = 'anchor_content',  x = 273,  y = -574,  order = 0,  isRespondable = false,  color = {r = 0, g = 0, b = 0, a = 1},  editingLocked = false,  treeItemIsExpanded = true,  text = '',  font = 'Data/Fonts/diary_white.fnt',  height = 28,  boundsX = 600,  boundsY = 0,  alignment = TextEntity.ETextHorizontalAlignmentLeft,  hotSpotType = TextEntity.ETextHotSpotTypeLeftTop,  },
		{ name = 'anchor_tast_text',  entityType = 'SpriteEntity',  parentName = 'tasks_description_txt',  x = 0,  y = 0,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0},  editingLocked = false,  treeItemIsExpanded = false,  width = 2,  height = 2,  },
		{ name = 'bttn_tasks',  entityType = 'ButtonEntity',  parentName = 'anchor_content',  x = 162,  y = -557,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/tasks_button.png',  text = '',  activeRectX = 108,  activeRectY = 100,  },
		{ name = 'tasks_txt',  entityType = 'TextEntity',  parentName = 'bttn_tasks',  x = 0,  y = 50,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  localizationKey = 'BTTN_TASKS_TEXT',  height = 20,  },
		{ name = 'tasks_updated',  entityType = 'SpriteEntity',  parentName = 'bttn_tasks',  x = 33,  y = 19,  order = 3,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Screens/Phone/exclamation_mark.png',  width = 24,  height = 48,  },
		{ name = 'bttn_notes',  entityType = 'ButtonEntity',  parentName = 'anchor_content',  x = 162,  y = -448,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/notes_button.png',  text = '',  activeRectX = 110,  activeRectY = 100,  },
		{ name = 'notes_txt',  entityType = 'TextEntity',  parentName = 'bttn_notes',  x = 0,  y = 50,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  localizationKey = 'BTTN_NOTES_TEXT',  height = 20,  },
		{ name = 'notes_updated',  entityType = 'SpriteEntity',  parentName = 'bttn_notes',  x = 33,  y = 19,  order = 3,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/exclamation_mark.png',  width = 24,  height = 48,  },
		{ name = 'bttn_map',  entityType = 'ButtonEntity',  parentName = 'anchor_content',  x = 159,  y = -338,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/map_button.png',  text = '',  activeRectX = 116,  activeRectY = 100,  },
		{ name = 'map_txt',  entityType = 'TextEntity',  parentName = 'bttn_map',  x = 3,  y = 50,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  localizationKey = 'BTTN_MAP_TEXT',  height = 20,  },
		{ name = 'bttn_gallery',  entityType = 'ButtonEntity',  parentName = 'anchor_content',  x = 159,  y = -227,  order = 2,  angle = -2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/gallery_button.png',  text = '',  activeRectX = 116,  activeRectY = 100,  },
		{ name = 'gallery_txt',  entityType = 'TextEntity',  parentName = 'bttn_gallery',  x = 3,  y = 50,  order = 5,  angle = 2,  editingLocked = false,  treeItemIsExpanded = false,  localizationKey = 'BTTN_GALLERY_TEXT',  height = 20,  },
		{ name = 'number_page_back',  entityType = 'SpriteEntity',  parentName = 'anchor_content',  x = 842,  y = -214,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/number_page_back.png',  width = 60,  height = 46,  },
		{ name = 'note_current_page_txt',  entityType = 'TextEntity',  parentName = 'number_page_back',  x = -1,  y = 0,  order = 11,  editingLocked = false,  treeItemIsExpanded = true,  text = '00',  height = 32,  },
		{ name = 'bttn_prev',  entityType = 'ButtonEntity',  parentName = 'note_current_page_txt',  x = -44,  y = -1,  order = 2,  scaleX = -1,  scaleY = 1,  color = {r = 1, g = 1, b = 1, a = 0.498039},  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/phone_arrow_bttn.png',  text = '',  activeRectX = 60,  activeRectY = 50,  },
		{ name = 'bttn_next',  entityType = 'ButtonEntity',  parentName = 'note_current_page_txt',  x = 44,  y = -1,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/phone_arrow_bttn.png',  text = '',  activeRectX = 60,  activeRectY = 50,  },
		{ name = 'layer_left_but',  entityType = 'SpriteEntity',  parentName = 'anchor_content',  x = 160,  y = -388,  order = 1,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/layer_left_but.png',  width = 114,  height = 446,  },
		{ name = 'phone_cover',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 100,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/phone_cover.png',  width = 1036,  height = 568,  },
		{ name = 'bttn_close',  entityType = 'ButtonEntity',  parentName = 'phone_cover',  x = 450,  y = -207,  order = 2,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Screens/Phone/bttn_close_phone.png',  text = '',  },
	}
	return objects
end
