--retrieve perph IDs
shell.run("label set ToDo")
local periList = peripheral.getNames()
mon = term.native()
for i = 1, #periList do
        if peripheral.getType(periList[i]) == "monitor" then
          mon = peripheral.wrap(periList[i])
          print("Monitor wrapped as... "..periList[i])
		end
end

term.redirect(mon)

local currentPage = 1 --start on page 1!
local todo = {} --begin with empty todo list
local width,height = mon.getSize() --get size of screen
local line = height - 3 --1 border on top, 1 on bottom, 1 row of buttons

function textCol(color) --useful function, only set color if a color monitor/computer
  if mon.isColor() then
    term.setTextColor(color)
  end
end


function openTODO() 
  local file = fs.open("todolist","r") --file should be called todolist
  if file ~= nil then  --if file exists
    local data = file.readAll()  --read entire file
    file.close() --close to ensure writing later
    if (textutils.unserialize(data)==nil) then --if file is invalid or blank data
      todo[1]="make todo list" --setup dummy table
    else
      todo = textutils.unserialize(data) --if its full, use data
     end
  else
   todo[1]="make todo list" --if file doesn't exist, setup dummy table
  end
end

function saveTODO() --reverse of previous function
  local file = fs.open("todolist","w")
  file.write(textutils.serialize(todo))
  file.close()
end

function displayPage(p) --change table display
 textCol(colors.white) 
  term.setCursorPos(1,2) --start on line 2
  for i = (1+((p-1)*line)),(p*line) do --page 1 starts at 1, if 6 lines, ends on 6. Improve?
    if todo[i] ~= nil then  --if line isn't blank
    print(" "..todo[i]) --print line!
	end
  end
end

function printButtons() --basic add/cycle buttons
  term.setCursorPos(2,height) 
  textCol(colors.green)
  term.write("Add item")
  term.setCursorPos(width-11,height)
  textCol(colors.yellow)
  term.write("Cycle page")
end

function itemRemove(itm) --doesn't work if in a function, investigage why...
term.clear()
term.setCursorPos(2,2)
term.write("Really remove item "..itm.."?")
term.setCursorPos(2,4)
term.write(todo[itm])
term.setCursorPos(2,6)
textCol(colors.green)
term.write("YES")
textCol(colors.red)
term.setCursorPos(2,8)
term.write("NO")
--term.write("Y for yes, N for no")
--os.pullEvent("char")
y=1
while (y~=6 and y~=8) do
if mon == term.native then
 event,but,x,y = os.pullEvent("mouse_click")
else
 event,side,x,y = os.pullEvent("monitor_touch")
end
end
if y == 6 then 
table.remove(todo,tonumber(itm)) 
end
end
--end broke function -------------------------------------

function addItem() --pretty simple
  term.clear() --clear terminal
  term.setCursorPos(2,2) --indent
  print("please see computer for input") --display on monitor
  term.redirect(term.native()) --reset to terminal
  term.clear() --clear terminal
  print("Please enter new item:")
  local input = read() --get item

for i=#todo+1,2,-1 do --for every item in the table, 
 todo[i] = todo[i-1] --shift 1 position over
 end
todo[1] = input --add to TOP of list. This is optional and can be easily undone to add to the bottom instead
print("thanks!") --output on terminal
term.redirect(mon) --redirect to monitor (if applicable)
end



--initial printing
openTODO()
term.clear()
printButtons()
displayPage(1)

while (true) do --forever loop!

if mon == term.native() then --if get where the user clicks, monitor or computer)
 event,but,x,y = os.pullEvent("mouse_click")
else
 event,side,x,y = os.pullEvent("monitor_touch")
end

if (y == height and x <= 9) then -- user clicks "add item
  addItem()
end

if (y == height and x >= width - 11) then -- user clicks cycle page
  if (currentPage >= #todo/(line)) then --#todo/line = max pages
   currentPage = 1
  else
   currentPage = currentPage +1
  end
end

if y >= 2 and y <= height-3 then --user clicks a todo item
  itm = ((currentPage-1)*line)+y-1
  if todo[itm]~=nil then --item actually exists
    --itemRemove(itm)
	term.clear()
    term.setCursorPos(2,2)
    term.write("Really remove item "..itm.."?")
    term.setCursorPos(2,4)
    term.write(todo[itm])
    term.setCursorPos(2,6)
    textCol(colors.green)
    term.write("YES")
    textCol(colors.red)
    term.setCursorPos(2,8)
    term.write("NO")
    y=1
    while (y~=6 and y~=8) do
     if mon == term.native() then
      event,but,x,y = os.pullEvent("mouse_click")
     else
      event,side,x,y = os.pullEvent("monitor_touch")
     end
    end
    if y == 6 then 
     table.remove(todo,tonumber(itm)) 
    end
  end
--ends remove code, MUCH MUCH neater in a function, but returns NIL errors if it is in one...
end

term.clear() --reset terminal
printButtons() --redraw everything
term.setCursorPos(1,1) --not needed, but keeps terminal tidy
displayPage(currentPage)

saveTODO() --save list so any additions/removals are preserved in case of crash.
end
