_w, _h = term.getSize()

function GitGet(Author, Project, Branch, Path, FileName)
	http.request("https://www.github.com/" .. Author .. "/" .. Project .. "/blob/" .. Branch .. "/" .. Path)
	local requesting = true
	while requesting do
		local event, url, sourceText = os.pullEventRaw()
		if event == "http_success" then
			localrespondedText = sourceText.readAll()
			temp = io.open(FileName, "w")
			temp:write(respondedText)
			temp:close()
			requesting = false
			return true
		end
	end
end
--[[ADD FANCY FUNCTIONS TO TERM]]--
term.colorPrint = function(_string, tColor, bgColor)
	term.setBackgroundColor(bgColor)
	term.setTextColor(tColor)
	print(_string)
end
term.colorWrite = function(_string, tColor, bgColor)
	term.setBackgroundColor(bgColor)
	term.setTextColor(tColor)
	write(_string)
end
term.cPrint = function(_string)
	x = _w / 2 - (#_string / 2)
	y = _h / 2 - 1
	term.setCursorPos(x, y)
	print(_string)
end
term.cWrite = function(_string)
	x = _w / 2 - (#_string / 2)
	y = _h / 2 - 1
	term.setCursorPos(x, y)
	write(_string)
end
term.cColorPrint = function(_string, tColor, bgColor)
	x = _w / 2 - (#_string / 2)
	y = _h / 2 - 1
	term.setCursorPos(x, y)
	term.setBackgroundColor(bgColor)
	term.setTextColor(tColor)
	print(_string)
end
term.cColorWrite = function(_string, tColor, bgColor)
	x = _w / 2 - (#_string / 2)
	y = _h / 2 - 1
	term.setCursorPos(x, y)
	term.setBackgroundColor(bgColor)
	term.setTextColor(tColor)
	write(_string)
end
term.cColourPrint = term.cColorPrint
term.cColourWrite = tern.cColorWrite
term.colourPrint = term.colorPrint
term.colourWrite = term.colorWrite
--[[Override os.version]]--
os.oldVersion = os.version
os.version = function()
	return os.oldVersion .. " + SAPI 0.0.1"
end

EventTable = {
	--[[] EVENT TABLE FORMAT
	["EVENTNAMEHERE AS GIVEN BY os.pullEvent"] = {["P1NAME", "P2NAME", "P3NAME", etc. OR FALSE IF NO PARAMATERS RETURNED]]--
	["unknown"] = {"p1", "p2", "p3", "p4", "p5"},
	["char"] = {"letter"},
	["key"] = {"keyCode"},
	["timer"] = {"timerVal"},
	["alarm"] = {"alarmVal"},
	["redstone"] = false,
	["terminate"] = false,
	["disk"] = {"side"},
	["disk_eject"] = {"side"},
	["peripheral"] = {"side"},
	["peripheral_detach"] = {"side"},
	["rednet_message"] = {"senderID", "msg", "distance"},
	["modem_message"] = {"side", "freq", "replyFreq", "msg", "distance"},
	["http_success"] = {"url", "text"},
	["http_failure"] = {"url"},
	["mouse_click"] = {"button", "x" , "y"},
	["mouse_scroll"] = {"direction", "x", "y"},
	["mouse_drag"] = {"button", "x", "y"},
	["monitor_touch"] = {"side", "x", "y"},
	["monitor_resize"] = {"side"},
	["turtle_inventory"] = false,
	
}

CCObject = {
	Type = "CCObject",
	ID = 0
}
CCEntity = {
	__index = CCObject,
	X = 0,
	Y = 0,
	Width = 0,
	Height = 0,
	Title = "",
	Visible = true,
	ToggleVisible = function(self)
		self.Visible = not self.Visible
	end
}
CCControl = {
	__index = CCEntity,
	Action = nil,
	CheckClick = nil,
	Enabled = true
}

--[[USE THESE ONLY!!!]]--
CCInterfaceEntities = {
	ButtonList = {},
	LoadingBarList = {},
	LabelList = {},
	SliderList = {},
	SwitchList = {},
	New = function(self)
		o = {}
		setmetatable(o, {__index = self})
	end,
	AddButton = function(self, _entity)
		table.insert(self.ButtonList,_entity)
	end,
	AddLoadingBar = function(self, _entity)
		table.insert(self.LoadingBarList, _entity)
	end,
	AddLabel = function(self, _entity)
		table.insert(self.LabelList, _entity)
	end,
	AddScrollBar = function(self, _entity)
		table.insert(self.SliderList, _entity)
	end,
	AddSwitch = function(self, _entity)
		table.insert(self.SwitchList, _entity)
	end,
	ToggleAll = function(self)
		for _, entity in pairs(self.ButtonList) do
			entity:ToggleVisible()
		end
		for _, entity in pairs(self.LoadingBarList) do
			entity:ToggleVisible()
		end
		for _, entity in pairs(self.LabelList) do
			entity:ToggleVisible()
		end
		for _, entity in pairs(self.SliderList) do
			entity:ToggleVisible()
		end
		for _, entity in pairs(self.SwitchList) do
			entity:ToggleVisible()
		end
	end
}
CCDrawing = {
	Screen = {
		Width = _w,
		Height = _h,
		BackgroundColor = colors.lightGray,
		TextColor = colors.white
	},
	DrawCharacters = function(_x, _y, _string, _textColor, _backgroundColor, mon)
		x = mon or term
		x.setBackgroundColor(_backgroundColor)
		x.setTextColor(_textColor)
		x.setCursorPos(_x, _y)
		x.write(_string)
	end,
	DrawArea = function(_x, _y, _w, _h, _character, _textColor, _bgColor, mon)
		if _w < 0 then
			_w = _w * -1
		elseif _w == 0 then
			_w = 1
		end
		if _h < 0 then
			_h = _h * -1
		elseif _h == 0 then
			_h = 1
		end
		
		local sRow = ""
		for ix = 1, _w do
			sRow = sRow .. _character
		end
		for iy = 1, _h do
			local currY = _y + iy - 1
			x = mon or term
			CCDrawing.DrawCharacters(_x, currY, sRow, _textColor, _bgColor, x)
		end
	end
}
CCButton = {
	__index = CCControl,
	Type = "CCButton",
	TextColor = colors.lime,
	BackgroundColor = colors.gray,
	ClickedColor = colors.green,
	Height = 1,
	Border = true,
	New = function(self, _x, _y, _title, _border, _action, _textColor, _bgColor, _clkColor)
		local new = {}
		setmetatable(new, {__index = self})
		new.TextColor = _textColor
		new.BackgroundColor = _bgColor
		new.ClickedColor = _clkColor
		new.Width = string.len(_title) + 2
		new.X = _x
		new.Y = _y
		new.Title = _title
		new.Action = _action
		new.Enabled = false
		new.Border = _border
		return new
	end,
	CheckClick = function(self, x, y)
		if x >= self.X - 1 and x <= self.X + self.Width + 1 and y >= self.Y - 1 and y <= self.Y + self.Height + 1 then
			self:Action()
		else
			return
		end
	end,
	Toggle = function(self)
		self.Enabled = not self.Enabled
		self:Draw()
	end,
	Draw = function(self, mon)
		local BGColor = self.BackgroundColor
		if self.Enabled then
			BGColor = self.ClickedColor
		end
		x = mon or term
		if self.Border then
			CCDrawing.DrawArea(self.X - 1, self.Y - 1, self.Width + 2, self.Height + 2, " ", self.TextColor, BGColor, x)
		end
		CCDrawing.DrawCharacters(self.X, self.Y, " " .. self.Title .. " ", self.TextColor, BGColor, x)
	end
}
CCLoadingBar = {
	__index = CCControl,
	Type = "CCLoadingBar",
	Text = " ",
	BackgroundColor = colors.gray,
	BarColor = colors.lime,
	X = 0,
	Y = 0,
	Width = 0,
	Height = 0,
	Progress = 0,
	New = function(self, _x, _y, _w, _bgColor, _barColor)
		o = {}
		setmetatable(o, {__index = self})
		o.X = _x
		o.Y = _y
		o.Width = _w
		o.BackgroundColor = _bgColor or self.BackgroundColor
		o.BarColor = _barColor or self.BarColor
		return o
	end,
	Draw = function(self, mon)
		x = mon or term
		CCDrawing.DrawArea(self.X, self.Y, self.Width, self.Height, self.Text, self.BackgroundColor, self.BackgroundColor, x)
		CCDrawing.DrawArea(self.X, self.Y, self.Progress, self.Height, self.Text, self.BarColor, self.BarColor, x)
	end,
	IncrementBar = function(self)
		self.Progress = self.Progress + 1
	end
}
CCEventBuffer = {
	__index = CCObject,
	Type = "",
	Buffer = true,
	_Buffer = {},
	_BufferFilter = {},
	New = function(self, _IsBuffer, _BF)
		o = {}
		setmetatable(o, {__index = CCEventBuffer})
		if _IsBuffer then
			o:ToggleBuffer()
			o._BufferFilter = _BF
		end
		return o
	end,
	FormatEvent = function(self)
		_eventData = {}
		local event={os.pullEventRaw()}
		_eventData.name = event[1]
		if EventTable[event[1]] then
			for i, label in ipairs(EventTable[event[1]]) do
				_eventData[label] = event[i+1] or nil
			end
		end
		if event[1] == "key" then
			_eventData.keyName = keys.getName(event[2])
		end
		return _eventData
	end,
	PullEvent = function(self)
		EventData = self:FormatEvent()
		if self.Buffer then
			if #self._BufferFilter > 0 then
				for _, eventName in pairs(self._BufferFilter) do
					if eventNAme == tEventData.name then
						table.insert(self._Buffer, EventData)
					end
				end
			else
				table.insert(self._Buffer, EventData)
			end
		end
		return EventData
	end,
	IsBuffer = function(self)
		return self.Buffer
	end,
	ToggleBuffer = function(self)
		self.Buffer = not self.Buffer
	end,
	GetLast = function(self)
		if _Buffer > 0 then
			return table.remove(self._Buffer)
		end
	end,
	GetFirst = function(self)
		if _Buffer > 0 then
			return table.remove(self._Buffer, 1)
		end
	end,
	Clear = function(self)
		self._Buffer = {}
	end
}
CCLabel = {
	__index = CCEntity,
	Type = "CCLabel",
	Height = 1,
	TextColor = colors.white,
	BackgroundColor = colors.black,
	New = function(self, _x, _y, _str, _txtColor, _bgColor)
		o = {}
		setmetatable(o, {__index = self})
		o.X = _x
		o.Y = _y
		o.Width = string.len(_str) + 2
		o.Title = _str
		o.TextColor = _txtColor or self.TextColor
		o.BackgroundColor = _bgColor or self.BackgroundColor
		return o
	end,
	Draw = function(self, mon)
		x = mon or term
		CCDrawing.DrawCharacters(self.X, self.Y, self.TextColor, self.BackgroundColor, x)
	end
}
CCHorizontalScrollBar = {
	__index = CCControl,
	Type = "CCHorizontalScrollBar",
	BarColor = colors.lime,
	BackgroundColor = colors.gray,
	LabelColor = colors.lime,
	Min = 0,
	Max = 0,
	Value = 0,
	Name = "",
	New = function(self, x1, y1, height, smin, smax, name, barColor, bgColor, labelColor)
		o = {}
		setmetatable(o, {__index = self})
		o.X = x1
		o.Y = y1
		o.Height = height
		o.Width = smax - smin
		o.Min = smin
		o.Max = smax
		o.Value = smin
		o.Name = name
		o.BarColor = barColor or self.BarColor
		o.BackgroundColor = bgColor or self.BackgroundColor
		o.LabelColor = labelColor or self.LabelColor
		return o
	end,
	Draw = function(self, mon)
		x = mon or term
		CCDrawing.DrawArea(self.X, self.Y, self.Width, self.Height, " ", self.BackgroundColor, self.BackgroundColor, x)
		CCDrawing.DrawArea(self.X, self.Y, (self.X + (self.Value - self.Min)), self.Height, " ", self.BarColor, self.BarColor, x)
		if self.Name == "" then
			CCDrawing.DrawCharacters(self.X, self.Y - 2, self.Value .. "/" .. self.Max, self.LabelColor, CCDrawing.Screen.BackgroundColor, x)
		else
			CCDrawing.DrawCharacters(self.X, self.Y - 2, self.Name, self.LabelColor, CCDrawing.Screen.BackgroundColor, x)
		end
	end,
	CheckClick = function(self, x, y)
		if x >= self.X and x <= self.X + self.Width and y >= self.Y and y <= self.Y + self.Height then
			self.Value = x - self.Min
			if self.Value < self.Min then
				self.Value = self.Min
			elseif self.Value > self.Max then
				self.Value = self.Max
			end
		end
	end
}
CCVerticalScrollBar = {
	__index = CCControl,
	Type = "CCHorizontalScrollBar",
	BarColor = colors.lime,
	BackgroundColor = colors.gray,
	LabelColor = colors.lime,
	Min = 0,
	Max = 0,
	Value = 0,
	Name = "",
	New = function(self, x1, y1, height, smin, smax, name, barColor, bgColor, labelColor)
		o = {}
		setmetatable(o, {__index = self})
		o.X = x1
		o.Y = y1
		o.Height = smax - smin
		o.Width = height
		o.Min = smin
		o.Max = smax
		o.Value = smin
		o.Name = name
		o.BarColor = barColor or self.BarColor
		o.BackgroundColor = bgColor or self.BackgroundColor
		o.LabelColor = labelColor or self.LabelColor
		return o
	end,
	Draw = function(self, mon)
		x = mon or term
		CCDrawing.DrawArea(self.X, self.Y, self.Width, self.Height, " ", self.BackgroundColor, self.BackgroundColor, x)
		CCDrawing.DrawArea(self.X, self.Y, self.Width, (self.Y + (self.Value - self.Min)) , " ", self.BarColor, self.BarColor, x)
		if self.Name == "" then
			CCDrawing.DrawCharacters(self.X, self.Y - 2, self.Value .. "/" .. self.Max, self.LabelColor, CCDrawing.Screen.BackgroundColor, x)
		else
			CCDrawing.DrawCharacters(self.X, self.Y - 2, self.Name, self.LabelColor, CCDrawing.Screen.BackgroundColor, x)
		end
	end,
	CheckClick = function(self, x, y)
		if x >= self.X and x <= self.X + self.Width and y >= self.Y and y <= self.Y + self.Height then
			self.Value = y - self.Min
			if self.Value < self.Min then
				self.Value = self.Min
			elseif self.Value > self.Max then
				self.Value = self.Max
			end
		end
	end
}
CCHorizontalSwitch = {
	__index = CCControl,
	Type = "CCHorizontalSwitch",
	SwitchColor = colors.lime,
	BackgroundColor = colors.gray,
	Value = false,
	Name = "",
	New = function(self, _x, _y, _name, switchColor, bgColor)
		o = {}
		setmetatable(o, {__index = self})
		o.X = _x
		o.Y = _y
		o.Name = _name
		o.SwitchColor = switchColor or self.SwitchColor
		o.BackgroundColor = bgColor or self.BackgroundColor
	end,
	Draw = function(self, mon)
		x = mon or term
		CCDrawing.DrawArea(self.X, self.Y, self.X + 2, self.Y + 1, " ", self.BackgroundColor, self.BackgroundColor, x)
		if self.Value then
			CCDrawing.DrawArea(self.X + 1, self.Y, self.X + 2, self.Y + 1, " ", self.SwitchColor, self.SwitchColor, x)
		else
			CCDrawing.DrawArea(self.X, self.Y, self.X + 1, self.Y + 1, " ", self.SwitchColor, self.SwitchColor, x)
		end
		CCDrawing.DrawCharacters(self.X - 1, self.Y, "0", self.SwitchColor, CCDrawing.Screen.BackgroundColor, x)
		CCDrawing.DrawCharacters(self.X + 2, self.Y, "1", self.SwitchColor, CCDrawing.Screen.BackgroundColor, x)
		CCDrawing.DrawCharacters(self.X, self.Y - 1, self.Name, self.SwitchColor, CCDrawing.Screen.BackgroundColor, x)
	end,
	CheckClick = function(self, x, y)
		if self.Value then
			if x >= self.X and x <= self.X + 1 and y >= self.Y and y <= self.Y + 1 then
				self.Value = not self.Value
			end
		else
			if x >= self.X + 1 and x <= self.X + 2 and y >= self.Y and y <= self.Y + 1 then
				self.Value = not self.Value
			end
		end
	end
}
CCVerticalSwitch = {
	__index = CCControl,
	Type = "CCVerticalSwitch",
	SwitchColor = colors.lime,
	BackgroundColor = colors.gray,
	Value = false,
	Name = "",
	New = function(self, _x, _y, _name, switchColor, bgColor)
		o = {}
		setmetatable(o, {__index = self})
		o.X = _x
		o.Y = _y
		o.Name = _name
		o.SwitchColor = switchColor or self.SwitchColor
		o.BackgroundColor = bgColor or self.BackgroundColo
	end,
	Draw = function(self, mon)
		x = mon or term
		CCDrawing.DrawArea(self.X, self.Y, self.X + 1, self.Y + 2, " ", self.BackgroundColor, self.BackgroundColor, x)
		if self.Value then
			CCDrawing.DrawArea(self.X, self.Y + 1, self.X + 1, self.Y + 2, " ", self.SwitchColor, self.SwitchColor, x)
		else
			CCDrawing.DrawArea(self.X, self.Y, self.X + 1, self.Y + 1, " ", self.SwitchColor, self.SwitchColor, x)
		end
		CCDrawing.DrawCharacters(self.X, self.Y - 1, "0", self.SwitchColor, CCDrawing.Screen.BackgroundColor, x)
		CCDrawing.DrawCharacters(self.X, self.Y + 2, "1", self.SwitchColor, CCDrawing.Screen.BackgroundColor, x)
		CCDrawing.DrawCharacters(self.X, self.Y - 2, self.Name, self.SwitchColor, CCDrawing.Screen.BackgroundColor, x)
	end,
	CheckClick = function(self, x, y)
		if self.Value then
			if x >= self.X and x <= self.X + 1 and y >= self.Y and y <= self.Y + 1 then
				self.Value = not self.Value
			end
		else
			if x >= self.X and x <= self.X + 1 and y >= self.Y + 1 and y <= self.Y + 2 then
				self.Value = not self.Value
			end
		end
	end
}



function init()
	setmetatable(CCEntity, CCEntity)
	setmetatable(CCEventBuffer, CCEventBuffer)
	setmetatable(CCControl, CCControl)
	setmetatable(CCLabel, CCLabel)
	setmetatable(CCButton, CCButton)
	setmetatable(CCLoadingBar, CCLoadingBar)
	setmetatable(CCHorizontalScrollBar, CCHorizontalScrollBar)
	setmetatable(CCVerticalScrollBar, CCVertiacalScrollBar)
	setmetatable(CCHorizontalSwitch, CCHorizontalSwitch)
	setmetatable(CCVerticalSwitch, CCVerticalSwitch)
end

init()
