--OSTextView--

	__index = OSControl
	type = "OSTextView"

	text = ""
	Selection = {0,0} --start character, end character
	CursorPosition = 0
	Blink = false  --whether the blinking character should be shown
	BlinkCharacter = "|"
	MoveDirection = 0
	submit = nil

	TextColour = colours.black
	SelectedBackgroundColour = colours.lightBlue
	SelectedTextColour = colours.white
	BackgroundColour = colours.white

	new = function(self, _x, _y, _width, _height, _text)
		local new = {}    -- the new instance
		setmetatable( new, {__index = OSTextView} )
		new.width = _width
		new.height = _height
		new.x = _x
		new.y = _y
		new.text = _text
		new:calculateWrapping()
		return new
	end


	--Thanks to Kingdaro for this snippet :D
	calculateWrapping = function(self)

		local lines = {''}
        for word, space in self.text:gmatch('(%S+)(%s*)') do
                local temp = lines[#lines] .. word .. space:gsub('\n','')
                if #temp > self.width then
                        table.insert(lines, '')
                end
                if space:find('\n') then
                        lines[#lines] = lines[#lines] .. word
                        
                        space = space:gsub('\n', function()
                                table.insert(lines, '')
                                return ''
                        end)
                else
                        lines[#lines] = lines[#lines] .. word .. space
                end
        end
		self.lines = lines
		OSUpdate()
	end

	Draw = function(self)
		OSDrawing.DrawBlankArea(self.x, self.y, self.width, self.height, self.BackgroundColour)

		for _,line in ipairs(self.lines) do
			if _ <= self.height then
			local startChar = (_-1) * self.width
			local endChar   = startChar + #line

			OSDrawing.DrawCharacters(self.x, self.y+_-1, line, self.TextColour, self.BackgroundColour)
			if startChar < self.Selection[1] then
				local selStr = string.sub(line,self.Selection[1],self.Selection[2])
				OSDrawing.DrawCharacters(self.x+self.Selection[1]-1, self.y+_-1, selStr, self.SelectedTextColour, self.SelectedBackgroundColour)
			else
				OSDrawing.DrawCharacters(self.x, self.y+_-1, line, self.TextColour, self.BackgroundColour)
			end			
			end
		end

		if self.isSelected then
			if self.Blink then
				local cX, cY = (self.CursorPosition % self.width) + self.x, math.floor(self.CursorPosition/self.width) + self.y 
				OSDrawing.DrawCharacters(cX, cY, self.BlinkCharacter, self.TextColour, self.BackgroundColour)
			end
			self.Blink = not self.Blink
		end
	end

	insertCharacter = function(self, char)
		self.text = string.sub(self.text, 1, self.CursorPosition) .. char .. string.sub(self.text, self.CursorPosition + 1)
		self.CursorPosition = self.CursorPosition + 1
		self.Blink = true
		self:calculateWrapping()
	end

	removeCharacter = function(self, direction)
		if direction == 0 or self.CursorPosition > 0 then
			self.text = string.sub(self.text, 1, self.CursorPosition - 1 + direction) .. string.sub(self.text, self.CursorPosition + 1 + direction)
			self:moveCursor(direction)
		end
		self.Blink = true
		self:calculateWrapping()
	end

	moveCursor = function(self, direction)
		local c = self.CursorPosition + direction
		if c < 0 then --make sure cursor is greater than 0
			c = 0
		end

		if c > #self.text + 1 then --make sure cursor is the character after the last at maximum
			c = #self.text + 1
		end
		self.CursorPosition = c
		self.Blink = true
		OSUpdate()
	end

	action = function(self, x, y)
		local pos  = 0
		if y > #self.lines then
			pos = #self.text
		else
			pos = x + (y *  self.width)
		end
		self.CursorPosition = pos
		self.Blink = true
		--self.Selection = {pos, pos}
		OSSelectedEntity = self --gain focus
	end