local xSize, ySize = term.getSize()
local comp = false
os.loadAPI('/System/API/windows')

local usualText = function(string,xPos,yPos)
	term.setCursorPos(xPos,yPos)
	term.write(string)
end

function draw(color,textColor,noty,inapp,settings,timer)
	local time = ''
	if timer ~= false then
		time = textutils.formatTime(os.time(),true)
	end
		
	local obj = {}
	if xSize > 30 then comp = true end
	
	if comp == false then
		term.setTextColor(textColor)
		if inapp == false then
			paintutils.drawLine(0,1,xSize,1,color)
			usualText(time,xSize-#time,1)
			if noty~=nil then
				for i=1,#noty do
					if noty[i]["ico"] ~= nil then
						usualText(noty[i]["ico"],i,1)
					else
						usualText("+",i,1)
					end
				end
			end	
		end
		paintutils.drawLine(0,ySize,xSize,ySize,color)
		usualText("()",xSize/2,ySize)
		usualText("<<",xSize/2-4,ySize)
		usualText("[]",xSize/2+4,ySize)
		if settings == true then
			usualText("|",xSize,ySize)
		end
	elseif comp == true then
		paintutils.drawLine(0,ySize,xSize,ySize,color)
		term.setTextColor(textColor)
		usualText(time,xSize-#time-1,ySize)
		usualText("()",5,ySize)
		usualText("<<",2,ySize)
		usualText("[]",8,ySize)
		if settings == true then
			usualText("|",xSize,ySize)
		end
		if noty~=nil then
			for i=1,#noty do
				if noty[i]["ico"] ~= nil then
					usualText(noty[i]["ico"],xSize-2-i-#time,ySize)
				else
					usualText("+",xSize-2-i-#time,ySize)
				end
			end
		end
	end
end

function back(x,y)
	if xSize > 30 then comp = true end
	if comp == false then
		if (x >= math.floor(xSize/2)-4 and x <= math.floor(xSize/2)-3) and y==ySize then
			term.setBackgroundColor(colors.lightGray)
			term.setTextColor(colors.white)
			usualText(" << ",xSize/2-5,ySize)
			sleep(0.1)
			return true
		else
			return false
		end
	elseif comp == true then
		if (x == 2 or x == 3) and y==ySize then
			term.setBackgroundColor(colors.lightGray)
			term.setTextColor(colors.white)
			usualText(" << ",1,ySize)
			sleep(0.1)
			return true
		else
			return false
		end
	end
end

function home(x,y)
	if xSize > 30 then comp = true end
	if comp == false then
		if (x >= math.floor(xSize/2) and x <= math.floor(xSize/2)+1) and y==ySize then
			term.setBackgroundColor(colors.lightGray)
			term.setTextColor(colors.white)
			usualText(" () ",xSize/2-1,ySize)
			sleep(0.1)
			home = true
			return true
		else
			return false
		end
	elseif comp == true then
		if (x == 5 or x == 6) and y==ySize then
			term.setBackgroundColor(colors.lightGray)
			term.setTextColor(colors.white)
			usualText(" () ",4,ySize)
			sleep(0.1)
			home = true
			return true
		else
			return false
		end
	end
end

function recents(x,y)
	if xSize > 30 then comp = true end
	if comp == false then
		if (x >= math.floor(xSize/2)+4 and x <= math.floor(xSize/2)+5) and y==ySize then
			term.setBackgroundColor(colors.lightGray)
			term.setTextColor(colors.white)
			usualText(" [] ",xSize/2+3,ySize)
			sleep(0.1)
			return true
		else
			return false
		end
	elseif comp == true then
		if (x == 8 or x == 9) and y==ySize then
			term.setBackgroundColor(colors.lightGray)
			term.setTextColor(colors.white)
			usualText(" [] ",7,ySize)
			sleep(0.1)
			return true
		else
			return false
		end
	end
end

function notifyCheck(x,y)
	local time = textutils.formatTime(os.time(),true)
	if xSize > 30 then comp = true end
	if comp == false then
		if y==1 then
			return true
		else
			return false
		end
	elseif comp == true then
		if (x > 9 and x < xSize-#time-1) and y==ySize then
			return true
		else
			return false
		end
	end
end

function header(text,textColor,backColor)
	paintutils.drawLine(1,1,xSize,1,backColor)
	paintutils.drawLine(1,2,xSize,2,backColor)
	term.setTextColor(textColor)
	usualText(text,1,1)
end

function switch(y, pos, color)
	if pos == nil then
		pos = false
	end
	if color == nil then
		color = colors.lightBlue
	end
	paintutils.drawLine(xSize-7,y,xSize-2,y,colors.gray)
	if pos==true or pos==1 then
		paintutils.drawLine(xSize-4,y,xSize-2,y,color)
	else
		paintutils.drawLine(xSize-7,y,xSize-5,y,colors.lightGray)
	end
end

function scrollBar(count, size, pos, sizeStep, color2, color1)
	local lenthScroll = 0
	local posScroll = 0
	local lenthPage = 0
	local step = 0
	if color1 == nil then
		color1 = colors.white
	end
	if color2 == nil then
		color2 = colors.lightGray
	end
	paintutils.drawLine(xSize,3,xSize,ySize-1,color2)
	if count>0 then
		if count*size > ySize-3 then
			lenthPage = count*size
			lenthScroll = math.floor((ySize-3)/(lenthPage/(ySize-3)))
			step = ((ySize-4)-lenthScroll)/math.floor((lenthPage-(ySize-4))/sizeStep)
			
		else
			step = 0
			lenthScroll = ySize-3
		end
	else 
		lenthScroll = ySize-3
	end
	paintutils.drawLine(xSize,3-pos,xSize,3+lenthScroll-pos,color1)
	return step
end

function search(path, expand, input)
	if path == nil then path = "" end
	local waitFor = true
	local output = nil
	term.setTextColor(colors.black)
	if xSize > 30 then comp = 1 else comp = 0 end
	paintutils.drawLine(3,2-comp,xSize-2,2-comp,colors.white)
	paintutils.drawLine(3,3-comp,xSize-2,3-comp,colors.white)
	paintutils.drawLine(3,4-comp,xSize-2,4-comp,colors.white)
	term.setCursorPos(4,3-comp)
	local keyWord = read()
	if input == false or input == nil then
		local result = {}
		if keyWord ~= "" then
			result = fs.find(path..keyWord.."*")
		end
		if expand == true then
			if result ~= nil then
				local i = 1
				while result[i] ~= nil do
					if fs.isDir(result[i]) == true then
						for ii=i,#result-1 do
							result[ii] = result[ii+1]
						end
						result[#result] = nil
					end
					i = i +1
				end
				term.setTextColor(colors.white)
				paintutils.drawLine(3,5-comp,xSize-2,5-comp,colors.lightGray)
				for i=1,#result do
					paintutils.drawLine(3,5-comp+i,xSize-2,5-comp+i,colors.lightGray)
					usualText(result[i],5,5-comp+i)
					paintutils.drawLine(3,6-comp+i,xSize-2,6-comp+i,colors.lightGray)
				end
			end
			while waitFor==true and keyWord~="" do
				local event, side, xp, yp = os.pullEvent()
				if event == 'mouse_click' then 
					for i=1,#result do
						if xp >= 3 and xp <= xSize-2 and yp==i+5-comp then
							output = result[i]
						end
					end
					waitFor = false
					comp = false
					return output
				end
			end
		else
			comp = false
			return result
		end
	else
		comp = false
		return keyWord
	end
end