--Made by kijzeer
--Version 1.2

term.setCursorBlink(true)
local sNew = "|   [Make new file]    Open existing file   |"
local sOpen = "|    Make new file    [Open existing file]  |"
local sYes = "|               [Yes]    No                 |"
local sNo = "|                Yes    [No]                |"
local sSave = "|      [Save]    Quit     Save & Quit       |"
local sQuit = "|       Save    [Quit]    Save & Quit       |"
local sSaveAndQuit = "|       Save     Quit    [Save & Quit]      |"
local iMenuXCor = 0
local sFile = ""
local sLine = ""
local tLines = {}
local iScrollX, iScrollY = 0,0
local x, y = 1,1
local w, h = term.getSize()

local function fMenuXCor()
	iMenuXCor = (w/2)-21
end

local function fCheckDir()
	if not fs.isDir("/files") then
		fs.makeDir("/files")
	end
	if shell.dir() ~=  "/files" then
		shell.setDir("/files")
	end
end

local function fExitDir()
	term.clear()
	shell.setDir("")
end

local function fOpenNew()
	tLines = {}
	table.insert(tLines, "")
end

local function fMenuNotExist()
    term.clear()
	term.setCursorBlink(false)
	local sYesOrNo = sYes
    term.setCursorPos(iMenuXCor,6)
	write(" ___________________________________________")
	term.setCursorPos(iMenuXCor,7)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,8)
	write("|         Error: File doesn't exist.        |")
	term.setCursorPos(iMenuXCor,9)
	write("|          Do you want to make it?          |")
	term.setCursorPos(iMenuXCor,10)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,11)
	write(sYesOrNo)
	term.setCursorPos(iMenuXCor,12)
	write("|___________________________________________|")
	repeat
		local sEvent, param = os.pullEvent()
		if sEvent == "key" then
			param = tostring(param)
			if param == "203" or param == "205" then
				if sYesOrNo == sYes then
					sYesOrNo = sNo
					term.setCursorPos(iMenuXCor,11)
					write(sYesOrNo)
				elseif sYesOrNo == sNo then
					sYesOrNo = sYes
					term.setCursorPos(iMenuXCor,11)
					write(sYesOrNo)
				end
			end
		end
	until param == "28"
	if sYesOrNo == sYes then
		bExit = false
		fOpenNew()
	elseif sYesOrNo == sNo then
		bExit = true
	end
end

local function fLoad()
	sPath = shell.resolve(sFile)
	if fs.exists( sPath ) then
		local file = io.open( sPath , "r")
		local sLine = file:read()
		while sLine do
			table.insert(tLines, sLine)
			sLine = file:read()
		end
		file:close()
	else
		fMenuNotExist()
	end
end

local function fMenuNew()
	term.clear()
	term.setCursorBlink(false)
	term.setCursorPos(iMenuXCor,1)
	write(" ___________________________________________")
	term.setCursorPos(iMenuXCor,2)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,3)
	write("|     How do you want to call the file?     |")
	term.setCursorPos(iMenuXCor,4)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,5)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,6)
	write("|___________________________________________|")
	term.setCursorPos(iMenuXCor+6,5)
	sFile = io.read()
	sFileExt = string.sub(sFile, -3)
	if sFileExt ~= ".txt" then
		sFile = sFile..".txt"
	end
	sPath = shell.resolve(sFile)
	if fs.exists(sPath) then
		term.clear()
		local sYesOrNo = sYes
		term.setCursorPos(iMenuXCor,6)
		write(" ___________________________________________")
		term.setCursorPos(iMenuXCor,7)
		write("|                                           |")
		term.setCursorPos(iMenuXCor,8)
		write("|         Error: File already exist.        |")
		term.setCursorPos(iMenuXCor,9)
		write("|          Do you want to edit it?          |")
		term.setCursorPos(iMenuXCor,10)
		write("|                                           |")
		term.setCursorPos(iMenuXCor,11)
		write(sYesOrNo)
		term.setCursorPos(iMenuXCor,12)
		write("|___________________________________________|")
		repeat
			local sEvent, param = os.pullEvent()
			if sEvent == "key" then
				param = tostring(param)
				if param == "203" or param == "205" then
					if sYesOrNo == sYes then
						sYesOrNo = sNo
						term.setCursorPos(iMenuXCor,11)
						write(sYesOrNo)
					elseif sYesOrNo == sNo then
						sYesOrNo = sYes
						term.setCursorPos(iMenuXCor,11)
						write(sYesOrNo)
					end
				end
			end
		until param == "28"
		if sYesOrNo == sYes then
			bExit = false
			fLoad()
		elseif sYesOrNo == sNo then
			bExit = true
		end
	else
		fOpenNew()
	end
	term.setCursorBlink(true)
end

local function fMenuOpen()
	term.clear()
	term.setCursorBlink(false)
	term.setCursorPos(1,8)
	write("Files:")
	term.setCursorPos(iMenuXCor,10)
	shell.run("ls")
	term.setCursorPos(iMenuXCor,1)
	write(" ___________________________________________")
	term.setCursorPos(iMenuXCor,2)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,3)
	write("|      Which file do you want to open?      |")
	term.setCursorPos(iMenuXCor,4)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,5)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,6)
	write("|___________________________________________|")
	term.setCursorPos(iMenuXCor+7,5)
	term.setCursorBlink(true)
	sFile = read()
	sFileExt = string.sub(sFile, -3)
	if sFileExt ~= ".txt" then
		sFile = sFile..".txt"
	end
	sPath = shell.resolve(sFile)
    local file = io.open( sPath , "r")
		local sLine = file:read()
		while sLine do
			table.insert(tLines, sLine)
			sLine = file:read()
		end
		file:close()
    bExit = false
end

local function fMenuStart()
	local sChoose = sNew
	fMenuXCor()
	term.clear()
	term.setCursorBlink(false)
	term.setCursorPos(iMenuXCor,6)
	write(" ___________________________________________")
	term.setCursorPos(iMenuXCor,7)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,8)
	write("| Welcome to the RedworksOS Word Processor, |")
	term.setCursorPos(iMenuXCor,9)
	write("|          what do you want to do?          |")
	term.setCursorPos(iMenuXCor,10)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,11)
	write(sChoose)
	term.setCursorPos(iMenuXCor,12)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,13)
	write("|         To access menu press Ctrl.        |")
	term.setCursorPos(iMenuXCor,14)
	write("|___________________________________________|")
	repeat
	local sEvent, param = os.pullEvent()
	if sEvent == "key" then
		param = tostring(param)
		if param == "203" or param == "205" then
			if sChoose == sNew then
				sChoose = sOpen
				term.setCursorPos(iMenuXCor,11)
				write(sChoose)
			elseif sChoose == sOpen then
				sChoose = sNew
				term.setCursorPos(iMenuXCor,11)
				write(sChoose)
			end
		end
	end
	until param == "28"
	if sChoose == sOpen then
		term.clear()
		fMenuOpen()
	elseif sChoose == sNew then
		term.clear()
		fMenuNew()
	end
	term.setCursorBlink(true)
end

local function fMenuExit()
	term.clear()
	term.setCursorBlink(false)
	local sSaveOrQuit = sSave
	term.setCursorPos(iMenuXCor,6)
	write(" ___________________________________________")
	term.setCursorPos(iMenuXCor,7)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,8)
	write("|          What do you want to do?          |")
	term.setCursorPos(iMenuXCor,9)
	write("|                                           |")
	term.setCursorPos(iMenuXCor,10)
	write(sSaveOrQuit)
	term.setCursorPos(iMenuXCor,11)
	write("|___________________________________________|")
	repeat
		local sEvent, param = os.pullEvent()
		if sEvent == "key" then
			if param == 203 then
				if sSaveOrQuit == sSave then
					sSaveOrQuit = sSaveAndQuit
					term.setCursorPos(iMenuXCor,10)
					write(sSaveOrQuit)
				elseif sSaveOrQuit == sQuit then
					sSaveOrQuit = sSave
					term.setCursorPos(iMenuXCor,10)
					write(sSaveOrQuit)
				elseif sSaveOrQuit == sSaveAndQuit then
					sSaveOrQuit = sQuit
					term.setCursorPos(iMenuXCor,10)
					write(sSaveOrQuit)
				end
			elseif param == 205 then
				if sSaveOrQuit == sSave then
					sSaveOrQuit = sQuit
					term.setCursorPos(iMenuXCor,10)
					write(sSaveOrQuit)
				elseif sSaveOrQuit == sQuit then
					sSaveOrQuit = sSaveAndQuit
					term.setCursorPos(iMenuXCor,10)
					write(sSaveOrQuit)
				elseif sSaveOrQuit == sSaveAndQuit then
					sSaveOrQuit = sSave
					term.setCursorPos(iMenuXCor,10)
					write(sSaveOrQuit)
				elseif param == 29 or param == 157 then
					
				end
			end
		end
	until param == 28 or param == 29 or param == 157
	while true do
		if param == 29 or param == 157 then
			break
		elseif sSaveOrQuit == sSave then
			fSave()
			term.setCursorBlink(true)
			bExit = false
			break
		elseif sSaveOrQuit == sQuit then
			bExit = true
			break
		elseif sSaveOrQuit == sSaveAndQuit then
			bExit = true
			fSave()
			break
		end
	end
end

function fSave()
	local file = io.open( sPath, "w" )
	if file then
		for n, sLine in ipairs( tLines ) do
			file:write( sLine .. "\n" )
		end
		file:close()
	end
end

local function fPrintText()
	for y=1,h do
		term.setCursorPos( 1 - iScrollX, y )
		term.clearLine()

		local sLine = tLines[ y + iScrollY ]
		if sLine ~= nil then
			write( sLine )
		end
	end
	term.setCursorPos( x - iScrollX, y - iScrollY )
end

local function fPrintLine()
	local sLine = tLines[y]
	term.setCursorPos( 1 - iScrollX, y - iScrollY )
	term.clearLine()
	term.write( sLine )
	term.setCursorPos( x - iScrollX, y - iScrollY )
end

local function fSetCursor(x, y)
	local screenX = x - iScrollX
	local screenY = y - iScrollY
	
	if screenX < 1 then
		iScrollX = x - 1
		screenX = 1
		fPrintText()
	elseif screenX > w then
		iScrollX = x - w
		screenX = w
		fPrintText()
	end
	
	if screenY < 1 then
		iScrollY = y - 1
		screenY = 1
		fPrintText()
	elseif screenY > h then
		iScrollY = y - (h)
		screenY = h
		fPrintText()
	end
	
	term.setCursorPos( screenX, screenY )
end

fCheckDir()
fMenuStart()
term.clear()
term.setCursorPos(x,y)
term.setCursorBlink(true)
fPrintText()
while true do
	if not bExit then
		local sEvent, param = os.pullEvent()
		if sEvent == "key" then
			if param == 200 then
				if y > 1 then
					y = y - 1
					x = math.min( x, string.len( tLines[y] ) + 1 )
					fSetCursor( x, y )
				end
			
			elseif param == 208 then
				if y < #tLines then
						y = y + 1
						x = math.min( x, string.len( tLines[y] ) + 1 )
						fSetCursor( x, y )
				end
			
			elseif param == 203 then
				if x > 1 then
					x = x - 1
					fSetCursor( x, y )
				end
			
			elseif param == 205 then
				if x < string.len( tLines[y] ) + 1 then
					x = x + 1
					fSetCursor( x, y )
				end
				
			elseif param == 14 then
				 if x > 1 then
					local sLine = tLines[y]
					tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
					fPrintLine()
				
					x = x - 1
					fSetCursor( x, y )
					
				elseif y > 1 then
					local sPrevLen = string.len( tLines[y-1] )
					tLines[y-1] = tLines[y-1] .. tLines[y]
					table.remove( tLines, y )
					fPrintText()
					
					x = sPrevLen + 1
					y = y - 1
					fSetCursor( x, y )
				end
				
			elseif param == 28 then
				local sLine = tLines[y]
				tLines[y] = string.sub(sLine,1,x-1)
				table.insert( tLines, y+1, string.sub(sLine,x) )
				fPrintText()
				
				x = 1
				y = y + 1
				fSetCursor( x, y )		
				
				
			elseif param == 29 or param == 157 then
				fMenuExit()
				if bExit then
					break
				else
					fPrintText()
				end
			
			elseif param == 199 then
				y = 1
				x = string.len( tLines[y] ) + 1
				fSetCursor(x, y)
			
			elseif param == 207 then
				y = # tLines
				x = string.len( tLines[y] ) + 1
				fSetCursor(x, y)
			
			elseif param == 201 then
				if y > h then
					y = y - h
				else
					y = 1
				end
				
				if y < 1 then
					y = 1
				end
				
				x = string.len( tLines[y] ) + 1
				fSetCursor(x, y)
			
			elseif param == 209 then
				if # tLines > h then
					y = y + h
				else
					y = #tLines
				end
				
				if y > # tLines then
					y = #tLines
				end
				
				x = string.len( tLines[y] ) + 1
				fSetCursor(x, y)
			
			elseif param == 15 then
				local sLine = tLines[y]
				tLines[y] = string.sub(sLine,1,x-1) .. "    " .. string.sub(sLine,x)
				fPrintLine()
			
				x = x + 4
				fSetCursor(x, y)
                
            elseif param == 211 then
                if x < string.len(tLines[y]) + 1 then
					local sLine = tLines[y]
					tLines[y] = string.sub(sLine,1,x-1) .. string.sub(sLine,x+1)
					fPrintLine()
					fSetCursor( x, y )
                
                elseif x == string.len(tLines[y]) + 1 then
					if y < #tLines then
                        tLines[y] = tLines[y] .. tLines[y+1]
                        table.remove( tLines, y + 1 )
                        fPrintText()
                        
                        fSetCursor( x, y )
                    end
				end
			
			end

		elseif sEvent == "char" then
			local sLine = tLines[y]
			tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
			fPrintLine()
		
			x = x + string.len( param )
			fSetCursor(x, y)
		end
	else
		break
	end
end

fExitDir()
term.clear()
term.setCursorBlink(false)
term.setCursorPos(1,1)