dofile('Data/Scripts/Common/Screens/InputNameScreenBase.lua')

class 'InputNameScreen' (InputNameScreenBase)

function InputNameScreen:__init(name)
    InputNameScreenBase.__init(self, name)

    self.inputNameTextField:attachTo( self:getMainEntity() )
	self.inputNameTextField:setOrder(500)
	self.inputNameTextField:setXY(self:getEntityNamed('player_name'):getX(), self:getEntityNamed('player_name'):getY())
	self.inputNameTextField:setText('')
	self.inputNameTextField:setMaxLength(8)
	self.inputNameTextField:setFontColor( Colorf(1, 1, 1, 1) )
	self.inputNameTextField:getTextView():setFont( 'Data/Fonts/tool_tip.fnt' ) 
	self.inputNameTextField:setFieldHeight(27)
	
    self:getWindow():getHotkeySignal(ERSK_ENTER):add( bind(self.onEnterPress, self) )
	
end

---------------------------------------------------------------------------------
function InputNameScreen:onEnterPress()
    if self.inputNameTextField:getTextView():getText():len() > 0 then
        self:onOkButtonClick()
    else
        self.inputNameTextField = createTextFieldEntity()
        self:getWindow():switchControlFocus(self.inputNameTextField, nil)
        self.inputNameTextFieldPosition = self:getEntityNamed('player_name'):getPosition() 
        self.inputNameTextField:setPosition(self.inputNameTextFieldPosition)
        self.inputNameTextField:attachTo( self:getMainEntity() )
        self.inputNameTextField:setOrder(500)
        self.inputNameTextField:setXY(self:getEntityNamed('player_name'):getX(), self:getEntityNamed('player_name'):getY())
        self.inputNameTextField:setText('')
        self.inputNameTextField:setMaxLength(8)
        self.inputNameTextField:setFontColor( Colorf(1, 1, 1, 1) )
        self.inputNameTextField:getTextView():setFont( 'Data/Fonts/tool_tip.fnt' ) 
        self.inputNameTextField:setFieldHeight(27)
    end
end
---------------------------------------------------------------------------------
function InputNameScreen:onOkButtonClick()
    local playerName = self.inputNameTextField:getTextView():getText()
    local playerAlreadyExist = ProfilesManager:getProfile(playerName)
    if playerAlreadyExist then
        createOkScreen(StringManager:getString('PROFILE_ALREADY_EXISTS'), nil, true)
    else        
        current_player = ProfilesManager:addProfile(playerName)
        ProfilesManager:setCurrentProfile(current_player)
        ProfilesManager:save()
        local vecProfiles = ProfilesManager:getProfiles()
        if vecProfiles:size() == 1 then
            AddGameState('INTRO_VIDEO_SHOWED', 'true')                
        end        
        self:onScreenDestroy()        
        
        if self.isOpenProfilesScreen then
            -- createProfilesScreen()
        end
    end
end

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

function InputNameScreen:CreateLoaderTable()
	local objects =
	{
		{ name = 'EnterName',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/WindowsBack/Window_small.png',  width = 743,  height = 575,  },
		{ name = 'name_text',  entityType = 'TextEntity',  parentName = 'EnterName',  x = -5,  y = -152,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'INPUT_NAME_TITLE',  font = 'Data/Fonts/tool_found.fnt',  height = 34,  },
		{ name = 'bttn_ok',  entityType = 'ButtonEntity',  parentName = 'EnterName',  x = -144,  y = 128,  order = 2,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Buttons/bttn_ok.png',  activeRectX = 150,  activeRectY = 50,  },
		{ name = 'bttn_cancel',  entityType = 'ButtonEntity',  parentName = 'EnterName',  x = 82,  y = 128,  order = 2,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Buttons/bttn_cancel.png',  activeRectX = 120,  activeRectY = 50,  },
		{ name = 'player_name',  entityType = 'SpriteEntity',  x = 500,  y = 355,  order = 1,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 100,  height = 100,  },
	}
	return objects
end
