class 'ToolFoundUnique' (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 ToolFoundUnique:__init(name)
    BaseScreen.__init(self, name)
    self.bttn_ok = self:getMainEntity():getEntityNamed('OKButton')	
    self.bttn_ok:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
    
    self:lockScreen(true)	
	self.inputNameTextField = createTextFieldEntity()
	self:getWindow():switchControlFocus(self.inputNameTextField, nil)

    self.inputNameTextFieldPosition = self:getEntityNamed('tool_name'):getPosition() 
    self.inputNameTextField:setPosition(self.inputNameTextFieldPosition)

    self.inputNameTextField:attachTo( self:getMainEntity() )
	self.inputNameTextField:setOrder(500)
	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(28)  
end

function ToolFoundUnique:lockScreen(lock)
	local active = not lock
	self:getEntityNamed('fade_in_on_load'):setActive(active)
	self:getEntityNamed('fade_in_on_load'):setRespondable(active)

	self.bttn_ok:setActive(active)
    self.bttn_ok:setRespondable(active)
end

function ToolFoundUnique:isLocked()
    return not self.bttn_ok:isActive()
end

function ToolFoundUnique: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        
        local width = self.inputNameTextField:getTextView():getBoundingBox():getWidth()
        self.inputNameTextField:setX(self.inputNameTextFieldPosition.x - width/2)

        self:lockScreen(false)

    else
      	self:lockScreen(true)
    end
end

function ToolFoundUnique:isValidCharacter(ch)
   return charIsOneOfThe(ch, ALPHA_CHARACTERS) or charIsOneOfThe(ch, NUMERICAL_CHARACTERS)    
end

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

function ToolFoundUnique:CreateLoaderTable()
	local objects =
	{
		{ name = 'tool_name',  entityType = 'SpriteEntity',  x = 516,  y = 519,  order = 1,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 100,  height = 30,  },
		{ name = 'ToolFoundWindow',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 20,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 206, y = 236},  texturePath = 'Data/Textures/GUI/ToolFound/ToolFoundWindow.png',  width = 412,  height = 472,  },
		{ name = 'new_item_txt',  entityType = 'SpriteEntity',  parentName = 'ToolFoundWindow',  x = 0,  y = -208,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/ToolFound/new_item_txt.png',  width = 84,  height = 16,  },
		{ name = 'MessageTxt',  entityType = 'TextEntity',  parentName = 'ToolFoundWindow',  x = 7,  y = 70,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  text = ' ',  font = 'Data/Fonts/Options.fnt',  height = 32,  boundsX = 300,  boundsY = 0,  },
		{ name = 'ToolAnchor',  entityType = 'SpriteEntity',  parentName = 'ToolFoundWindow',  x = 0,  y = -70,  order = 0,  isRespondable = false,  color = {r = 1, g = 1, b = 1, a = 0.01},  editingLocked = false,  treeItemIsExpanded = false,  hotSpot = {x = 125, y = 125},  width = 250,  height = 250,  },
		{ name = 'OKButton',  entityType = 'ButtonEntity',  parentName = 'ToolFoundWindow',  x = 0,  y = 177,  order = 5,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Buttons/bttn_ok.png',  text = '',  },
	}
	return objects
end
