class 'InputNameScreenBase' (BaseScreen)

local ALPHA_CHARACTERS = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
}

local NUMERICAL_CHARACTERS = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
}

local function charIsOneOfThe(ch, data)
    for _, v in pairs(data) do
        if ch == v then
            return true
        end
    end

    return false
end

function InputNameScreenBase:__init(name)
    BaseScreen.__init(self, name)
    
    self.bttn_ok = self:getMainEntity():getEntityNamed('bttn_ok')
    self.bttn_cancel = self:getMainEntity():getEntityNamed('bttn_cancel')
    
    self.bttn_ok.onPress:add(bind(self.onOkButtonClick, self))
	self.bttn_cancel.onPress:add(bind(self.onCancelButtonClick, self))
	
    self.bttn_ok:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
    self.bttn_cancel:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )

	if not current_player then
	    self.bttn_cancel:setActive(false)
	    self.bttn_cancel:setRespondable(false)
	end
	
	self.inputNameTextField = createTextFieldEntity()
	self:getWindow():switchControlFocus(self.inputNameTextField, nil)

    self.inputNameTextFieldPosition = self:getEntityNamed('player_name'):getPosition() 
    self.inputNameTextField:setPosition(self.inputNameTextFieldPosition)
	
	-- self:addHotKey(ERSK_ENTER, bind(self.onOkButtonClick, self) )
	-- self:addHotKey(ERSK_ESCAPE, bind(self.onCancelButtonClick, self) )	
end

function InputNameScreenBase:__finalize()
end

function InputNameScreenBase:onUpdate(dt)
    -- Check: is valid Symbol ?
    do
        local str = self.inputNameTextField:getTextView():getText()
        local result = ""
        
        for i = 0, str:len() do
            local ch = str:sub(i, i)
            if ch and self:isValidCharacter(ch) then
                result = result..ch
            end
        end
        
        self.inputNameTextField:setText(result)
    end


    if self.inputNameTextField:getTextView():getText():len() > 0 then
        self.bttn_ok:setActive(true)
        self.bttn_ok:setRespondable(true)
        local width = self.inputNameTextField:getTextView():getBoundingBox():getWidth()
        self.inputNameTextField:setX(self.inputNameTextFieldPosition.x - width/2)
    else
        self.bttn_ok:setActive(false)
        self.bttn_ok:setRespondable(false)
    end
end

function InputNameScreenBase:isValidCharacter(ch)
    if charIsOneOfThe(ch, ALPHA_CHARACTERS) or charIsOneOfThe(ch, NUMERICAL_CHARACTERS) then
        return true
    end
    
    return false
end

function InputNameScreenBase: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()
        
        self:onScreenDestroy()
        
        if self.isOpenProfilesScreen then
            -- createProfilesScreen()
        end
    end
end

function InputNameScreenBase:setOpenProfilesScreen(isOpenProfilesScreen)
    self.isOpenProfilesScreen = isOpenProfilesScreen
end

function InputNameScreenBase:onCancelButtonClick()
    self:onScreenDestroy()
end

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