class 'ProfilesScreenBase' (BaseScreen)

function ProfilesScreenBase:__init(name)
	BaseScreen.__init(self, name)

	self:setupButtons()    
	self:createListBox()
end

function ProfilesScreenBase:setupButtons()
	self:getEntityNamed('bttn_ok').onPress:add(bind(self.onOkButtonClick, self))
	self:getEntityNamed('bttn_new').onPress:add(bind(self.onNewButtonClick, self))
	self:getEntityNamed('bttn_delete').onPress:add(bind(self.onDeleteButtonClick, self))
	
	-- add sfx-sound on click
	do
		self:getEntityNamed('bttn_ok'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
		self:getEntityNamed('bttn_new'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
		self:getEntityNamed('bttn_delete'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
	end
end

function ProfilesScreenBase:__finalize()
end

function ProfilesScreenBase:onUpdate(dt)
end

function ProfilesScreenBase:createListBox()
	local vecProfiles = ProfilesManager:getProfiles()
	local position  = Vector2f(512, 240)
	local offset    = Vector2f(0, 40)
	
	-- Удаление всех игроков, если они были созданы до этого.
	if self.players then
		for _, bttn in pairs(self.players) do
			bttn:destroy()    
		end
	end
	self.players = {}

	for i = 0, vecProfiles:size() - 1 do
		local profile = vecProfiles:get(i)
		
		local bttn = self:createButtonName(profile.name, position)
		table.insert(self.players, bttn)
		position = position + offset
	end
	
	self:updateActivePlayers()
end

function ProfilesScreenBase:updateActivePlayers()
	for i, bttn in pairs(self.players) do
		if ProfilesManager:getCurrentProfile() then
			if ProfilesManager:getCurrentProfile().name == bttn:getName() then
				self:getMainEntity():getEntityNamed('select'):setXY(bttn:getX(), bttn:getY())
				break
			end
		elseif i == 1 then
			self:getMainEntity():getEntityNamed('select'):setXY(bttn:getX(), bttn:getY())
			current_player = ProfilesManager:getProfile(bttn:getName())
			ProfilesManager:setCurrentProfile( current_player )
			ProfilesManager:save()
		end
	end
	
	current_player = ProfilesManager:getCurrentProfile()
end

function ProfilesScreenBase:createButtonName(name, position)
	local button = CreateButtonEntity('')
	button:attachTo( self:getMainEntity() )
	button:setName(name)
	button:setXY(position.x, position.y)
	button:setOrder(50)
	button:getOverlayTextEntity():setText(name)
	button:getOverlayTextEntity():setFont( 'Data/Fonts/comics.fnt' ) 
	button:setActiveRectSize(350, 40)
	button.onPress:add(bind(self.onActiveButtonClick, self))
	
	return button
end

function ProfilesScreenBase:onActiveButtonClick(bttn, r)
	local activePlayerName = bttn:getName()
	
	current_player = ProfilesManager:getProfile(activePlayerName)
	ProfilesManager:setCurrentProfile( current_player )
	ProfilesManager:save()
	
	local timeMove = 0.25
	self:getMainEntity():getEntityNamed('select'):MoveTo(bttn:getX(), bttn:getY(), timeMove)
	
	for _, bttn in pairs(self.players) do
		bttn:setActive(false)    
	end
	
	self.timer:add(function()
						for _, bttn in pairs(self.players) do
							bttn:setActive(true)    
						end
				   end, timeMove)
end

function ProfilesScreenBase:onOkButtonClick()
	self:onScreenDestroy()
end

function ProfilesScreenBase:onNewButtonClick()
	if ProfilesManager:getProfiles():size() >= MAX_PROFILES then
		createOkScreen(StringManager:getString('MAX_PROFILES'), nil, true)
		return
	end

	self:onScreenDestroy()
	
	local screen = createInputNameScreen()
	screen.isOpenProfilesScreen = true
end

function ProfilesScreenBase:onDeleteButtonClick()
	self:onScreenDestroy()

	local screen = createYesNoScreen('QUESTION_DELETE_PROFILE', playerDeleteConfirm, playerDeleteDiscard, false, 0)
end

function playerDeleteConfirm()          -- Делается как свободная функция, потому что нужно удалить ProfilesScreenBase
	local activeProfile = ProfilesManager:getCurrentProfile()
	ProfilesManager:deleteProfile(activeProfile.name)
	ProfilesManager:save()
	
	if ProfilesManager:getProfiles():size() == 0 then
		current_player = ProfilesManager:getCurrentProfile()
		createInputNameScreen()
	else
		createProfilesScreen()
	end
end

function playerDeleteDiscard()          -- Делается как свободная функция, потому что нужно удалить ProfilesScreenBase
	createProfilesScreen()
end

function ProfilesScreenBase:onScreenDestroy()
	 PopUpScreensMgr:pop()
end