os.pullEvent = os.pullEventRaw
local verze = "v1.9.8 EN"
local w,h = term.getSize()
local fgc = term.setTextColor
local bgc = term.setBackgroundColor
local user
local userDir
local sDrive = nil
local tArgs = { ... }

local function prc(text, y)
  local w = term.getSize()
  local _, cy = term.getCursorPos()
  term.setCursorPos(math.ceil((w-#text)/2), y or cy)
  write(text)
end

function boot ()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
shell.run ("list")
local function openRednet()
  for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
    if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
      rednet.open(side)
	  local modem = peripheral.wrap(side)
	  modem.open(5)
      return side
    end
  end
  term.setTextColor(colors.red)
  print("no wifi present")
end

modemSide = openRednet()
if modemSide == nil then
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.red)
  print("No modem connected")
  sleep (1)
else
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.lime)
  print("Opened modem on "..modemSide)
  sleep (1)
end

local function openRednet()
  for _,side in ipairs({"bottom", "front", "left", "right", "back"}) do
    if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
      rednet.open(side) 
	  local modem = peripheral.wrap(side)
	  modem.open(5)
      return side
    end
  end
  term.setTextColor(colors.red)
  print("no wired present")
end

modemSide = openRednet()
if modemSide == nil then
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.red)
  print("2nd No modem connected")
  sleep (1)
else
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.lime)
  print("2nd Opened modem on "..modemSide)
  sleep (1)
end
sleep (1)
spousteni1 ()
end

function spousteni1 ()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.blue)
term.setCursorPos(20,18)
print"  Pepek"
term.setCursorPos(28,18)
print"Soft  "
term.setCursorPos(27,18)
term.setTextColor(colors.lime)
print"@"
term.setCursorPos(5, 14)
print ("Verze "..verze)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(16,7)
prc "Pepdroll 6 Profesional"
term.setCursorPos(15,9)
term.setBackgroundColor(colors.lightBlue)
textutils.slowPrint "                        "
term.setCursorPos(15,9)
term.setBackgroundColor(colors.yellow)
textutils.slowPrint "                        "
term.setCursorPos(15,9)
term.setBackgroundColor(colors.lightBlue)
textutils.slowPrint "          "
heslo ()
end

function kontrola ()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(14, 7)
print "   try upgrade? y/n"
term.setCursorPos(5, 12)
print "Enter -> back"
term.setCursorPos(5, 14)
print ("Version "..verze)
term.setCursorPos(18, 8)
print " if help /help "
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(23, 10)
print "       "
term.setCursorPos(24, 10)
opak = io.read ()

if opak == "y" then
aktiv ()
elseif opak == "/help" then
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(5, 7)
term.setBackgroundColor(colors.yellow)
term.setTextColor(colors.white)
print "                         "
term.setCursorPos(5, 8)
print " y -> upgrade            "
term.setCursorPos(5, 9)
print " n -> back               "
term.setCursorPos(5, 10)
print " /help -> help           "
term.setCursorPos(5, 11)
print " Enter -> back           "
term.setCursorPos(5, 12)
print "                         "
term.setCursorPos(5, 13)
a = io.read ()

if a == " " then
kontrola ()
else
kontrola ()
end
elseif opak == "n" then
aktualizace ()
else
aktualizace ()
end
end

function aktiv ()
shell.run ("pastebin get BvW0repA aktiv")
shell.run ("aktiv")
end

function deaktiv ()
shell.run ("delete aktiv")
os.reboot ()
heslo ()
end

function createAccount()
  local next = false
  local user
  local pass
  clear ()
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.clear()
  term.setCursorPos(5,7)
  print("Register new user")
  term.setCursorPos(5,9)
  print("User name: ")
  term.setCursorPos(5,11)
  print(" Password: ")
  term.setBackgroundColor(colors.lightBlue)
  term.setCursorPos(18,9)
  print "         "
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,9)
  user = read()
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,11)
  pass = read("@")
  if not fs.isDir("/.ucet/"..user) then
    fs.makeDir("/.ucet/"..user)
    local file = fs.open(".ucet/"..user.."/.data","w")
    file.writeLine(user)
    file.writeLine(pass)
    file.close()
    if not fs.exists("/.core/ucet") then
      f=fs.open("/.core/ucet","w")
      f.close ()
    end
  else
    term.setBackgroundColor(colors.white)
    fgc(colors.red)
	term.setCursorPos(5,13)
    print("Name is exist")
  end
end

function heslo()
  if not fs.exists("/.core/ucet") then
    createAccount()
    local ucet = fs.open("/.core/ucet","w")
    ucet.close()
  end
  local userI
  local passI
  local userO
  local passO
  local data
  local success = false

  while not success do
  clear ()
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.clear()
  term.clear()
  term.setCursorPos(5,7)
  print("Login")
  term.setCursorPos(5,9)
  print("User name: ")
  term.setCursorPos(5,11)
  print(" Password: ")
  term.setBackgroundColor(colors.lightBlue)
  term.setCursorPos(18,9)
  print "         "
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,9)
  userI = read()
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,11)
  passI = read("@")
  
    if fs.isDir("/.ucet/"..userI) then
      if fs.exists("/.ucet/"..userI.."/.data") then
        data = fs.open("/.ucet/"..userI.."/.data","r")
		userO = data.readLine()
        passO = data.readLine()
	    data.close ()
        if userI == userO and passI == passO then
		  term.setBackgroundColor(colors.white)
		  term.setCursorPos(5,13)
		  fgc(colors.lime)
          print("Login successful!")
          user = userI
          userDir = "/.ucet/"..userI
          success = true
		  sleep (1)
		  vitejte ()
        else
		  term.setBackgroundColor(colors.white)
          fgc(colors.red)
		  term.setCursorPos(5,13)
          print("Login failed: Incorrect Username or Password!")
        end
        data.close()
      else
	    term.setBackgroundColor(colors.white)
        fgc(colors.red)
		term.setCursorPos(5,13)
        print("Missing account data")
      end
    else
	  term.setBackgroundColor(colors.white)
      fgc(colors.red)
	  term.setCursorPos(5,13)
      print("Login failed: Incorrect Username or Password!")
    end
    sleep(1)
  end
end

function clear()
  term.clear()
  term.setBackgroundColor(colors.white)
  for i=1,w do
    for j=1,h do
      term.setCursorPos(i,j)
      write(" ")
    end
  end
end

function vitejte ()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(23, 7)
prc ("Welcome")
term.setBackgroundColor(colors.white)
term.setTextColor(colors.blue)
term.setCursorPos(20,18)
print"  Pepek"
term.setCursorPos(28,18)
print"Soft  "
term.setCursorPos(27,18)
term.setTextColor(colors.lime)
print"@"
sleep (1)
plocha ()
end

function plocha ()
local f=fs.open("//.core/nastaveni.cfg","r")
local cnt=f.readAll()
f.close()
local nastaveni=textutils.unserialize(cnt)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(nastaveni.barva)
local image = paintutils.loadImage("bar.nfp")
paintutils.drawImage(image, 3, 2)
term.setCursorPos(1,1)
term.setBackgroundColor(nastaveni.barva)
term.setTextColor(colors.black)
term.setCursorPos(45,17)
if #tArgs > 0 then
	sDrive = tostring( tArgs[1] )
end

if sDrive == nil then
	print( "ID "..os.getComputerID() )
	
	local label = os.getComputerLabel()
	if label then
		print( "Label \""..label.."\"" )
	end

else
	local bData = disk.hasData( sDrive )
	if not bData then
		print( "No disk in drive "..sDrive )
		return
	end
	
	print( "The disk is #"..disk.getID( sDrive ) )

	local label = disk.getLabel( sDrive )
	if label then
		print( "The disk is labelled \""..label.."\"" )
	end
end
term.setTextColor(colors.white)
term.setCursorPos(1,1)
print "                                                   "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
print"  "
paintutils.drawPixel(1, 19, nastaveni.barva)
paintutils.drawPixel(2, 19, nastaveni.barva)
term.setCursorPos(15,1)
prc ("Pepdroll 6 Professional "..verze)
term.setCursorPos(14,2)
print"                         "
paintutils.drawPixel(51, 1, colors.red)
term.setCursorPos(51, 1)
print "Q"
term.setCursorPos(1,1)
term.setBackgroundColor(colors.orange)
term.setTextColor(colors.black)
print "Menu"
term.setBackgroundColor(colors.orange)
local time = os.time()
local formattedTime = textutils.formatTime(time, true)
term.setCursorPos(1,18)
print("" .. formattedTime)


while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if xy == "51,1" and button == 1 then
vypnout ()
break
end

if xy == "51,1" and button == 2 then
restart ()
break
end

if x >= 1 and x <= 4 and y == 1 and button == 1 then
menu ()
break
end
end
end

function menu ()
term.setBackgroundColor(colors.white)
term.setCursorPos(5,1)
print "       "
term.setCursorPos(1,2)
print "Dynet Brow."
term.setCursorPos(1,3)
print "File mana. "
term.setCursorPos(1,4)
print "Media play "
term.setCursorPos(1,5)
print "Paint NP   "
term.setCursorPos(1,6)
print "Text editor"
term.setCursorPos(1,7)
print "Options    "
term.setCursorPos(1,8)
print "Pastebin   "
term.setBackgroundColor(colors.orange)
term.setCursorPos(1,9)
print "           "
paintutils.drawPixel(12, 2, colors.orange)	
paintutils.drawPixel(12, 3, colors.orange)
paintutils.drawPixel(12, 1, colors.orange)
paintutils.drawPixel(12, 4, colors.orange)
paintutils.drawPixel(12, 5, colors.orange)
paintutils.drawPixel(12, 6, colors.orange)
paintutils.drawPixel(12, 7, colors.orange)
paintutils.drawPixel(12, 8, colors.orange)
paintutils.drawPixel(12, 9, colors.red)
term.setCursorPos(12, 9)
print "Q"
term.setBackgroundColor(colors.orange)
local time = os.time()
local formattedTime = textutils.formatTime(time, true)
term.setCursorPos(1,18)
print("" .. formattedTime)
while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if x >= 1 and x <= 11 and y == 2 and button == 1 then
web ()
elseif x >= 1 and x <= 8 and y == 8 and button == 1 then
pastebin ()
elseif x >= 1 and x <= 10 and y == 7 and button == 1 then
nastaveni ()
elseif x >= 1 and x <= 11 and y == 6 and button == 1 then
textovani ()
elseif x >= 1 and x <= 11 and y == 5 and button == 1 then
malovani ()
elseif x >= 1 and x <= 10 and y == 4 and button == 1 then
prehravac ()
elseif x >= 1 and x <= 10 and y == 3 and button == 1 then
prohlizec ()
elseif xy == "12,9" and button == 1 then
rezimy ()
elseif xy == "12,9" and button == 2 then
plocha ()
elseif xy == "51,1" and button == 1 then
vypnout ()
elseif xy == "51,1" and button == 2 then
restart ()
else
plocha ()
break
end
end
end

function rezimy ()
term.setBackgroundColor(colors.orange)
term.setTextColor(colors.black)
term.setCursorPos(13, 8)
print "          "
term.setCursorPos(13, 9)
print "          "
term.setCursorPos(12, 10)
print "           "
term.setCursorPos(12, 11)
print "           "
term.setCursorPos(12, 12)
print "           "
term.setCursorPos(12, 13)
print "           "
term.setCursorPos(12, 14)
print "           "
term.setCursorPos(12, 15)
print "           "
term.setBackgroundColor(colors.white)
term.setCursorPos(13, 9)
print " Modes   "
term.setCursorPos(13, 10)
term.setBackgroundColor(colors.lightBlue)
print " Log Out "
term.setCursorPos(13, 11)
term.setBackgroundColor(colors.lightGray)
term.setTextColor(colors.white)
print " Sleep   "
term.setTextColor(colors.black)
term.setCursorPos(13, 12)
term.setBackgroundColor(colors.white)
print "         "
term.setCursorPos(13, 13)
term.setBackgroundColor(colors.yellow)
print " Restart "
term.setCursorPos(13, 14)
term.setBackgroundColor(colors.red)
print " Shutdown"
paintutils.drawPixel(12, 9, colors.gray)
term.setCursorPos(12, 9)
print "Q"
term.setBackgroundColor(colors.orange)
local time = os.time()
local formattedTime = textutils.formatTime(time, true)
term.setCursorPos(1,18)
print("" .. formattedTime)

while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if xy == "12,9" and button == 1 then
plocha ()
elseif x >= 14 and x <= 20 and y == 10 and button == 1 then
odhlasit ()
elseif x >= 14 and x <= 20 and y == 11 and button == 1 then
spanek ()
elseif x >= 14 and x <= 20 and y == 13 and button == 1 then
restart ()
elseif x >= 14 and x <= 20 and y == 14 and button == 1 then
vypnout ()
else
plocha ()
break
end
end
end

function spanek ()
term.setBackgroundColor(colors.lightGray)
term.clear()
sleep (0.5)
term.setBackgroundColor(colors.gray)
term.clear()
sleep (0.5)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.black)
term.clear()
while true do
  local event, button, xPos, yPos = os.pullEvent("mouse_click")
  heslo ()
end
end

function vypnout ()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(23, 7)
prc "Bye Bye"
term.setBackgroundColor(colors.white)
term.setTextColor(colors.blue)
term.setCursorPos(20,18)
print"  Pepek"
term.setCursorPos(28,18)
print"Soft  "
term.setCursorPos(27,18)
term.setTextColor(colors.lime)
print"@"
sleep (2)
os.shutdown ()
end

function odhlasit ()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(23, 7)
prc "Log Out"
term.setBackgroundColor(colors.white)
term.setTextColor(colors.blue)
term.setCursorPos(20,18)
print"  Pepek"
term.setCursorPos(28,18)
print"Soft  "
term.setCursorPos(27,18)
term.setTextColor(colors.lime)
print"@"
sleep (2)
heslo ()
end

function restart ()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(23, 7)
prc "Log Out"
term.setBackgroundColor(colors.white)
term.setTextColor(colors.blue)
term.setCursorPos(20,18)
print"  Pepek"
term.setCursorPos(28,18)
print"Soft  "
term.setCursorPos(27,18)
term.setTextColor(colors.lime)
print"@"
sleep (1)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(23, 7)
prc "Bye Bye"
term.setBackgroundColor(colors.white)
term.setTextColor(colors.blue)
term.setCursorPos(20,18)
print"  Pepek"
term.setCursorPos(28,18)
print"Soft  "
term.setCursorPos(27,18)
term.setTextColor(colors.lime)
print"@"
sleep (1)
os.reboot ()
end

function prohlizec ()
local f=fs.open("//.core/nastaveni.cfg","r")
local cnt=f.readAll()
f.close()
local nastaveni=textutils.unserialize(cnt)
local version = "1.2"
w,h = term.getSize()
local dir = ""
shell.setDir(dir)
local tProgram = shell.getRunningProgram()
local buttonAPI = "button"
local selected = 0
local menuS = false
local input
local path
local ypos
local tempvar

filePos = {
  [1] = {3,3},
  [2] = {15,3},
  [3] = {27,3},
  [4] = {39,3},
  [5] = {3,7},
  [6] = {15,7},
  [7] = {27,7},
  [8] = {39,7},
  [9] = {3,11},
  [10] = {15,11},
  [11]= {27,11},
  [12]= {39,11},
  [13] = {3,15},
  [14]= {15,15},
  [15]= {27,15},
  [16]= {39,15}
}
local cPage = 1
local nPage = 1
local run = true

--Checks
if not term.isColor() then
  print("This program can only run on an advanced computer")
  return
--[[elseif os.version() ~= "CraftOS 1.5" then
  print("This program is designed for CraftOS 1.5")
  print("Your version is "..os.version())
  print("Newer or older version may not work")
  print("Run anyways?")
  print("y/n")
  while true do
    event,par1,par2,par3 = os.pullEvent()
    if par1 == keys.y then run = true break end
    if par1 == keys.n then run = false break end
  end]]
end

if not run then os.pullEvent() return end


--Functions--
function clear(color)
  if color then
    term.setBackgroundColor(color)
  end
  term.clear()
  term.setCursorPos(1,1)
end

function sColor(color)
  term.setBackgroundColor(color)
end

function drawS(x,y,state)
  if state then sColor(colors.cyan) else sColor(colors.white) end
  term.setCursorPos(x-1,y-1)
  print(string.rep(" ",13))
  term.setCursorPos(x-1,y)
  term.write(" ")
  term.setCursorPos(x-1,y+1)
  term.write(" ")
  term.setCursorPos(x-1,y+2)
  term.write(" ")
  term.setCursorPos(x-1,y+3)
  print(string.rep(" ",13))
  term.setCursorPos(x+11,y)
  term.write(" ")
  term.setCursorPos(x+11,y+1)
  term.write(" ")
  term.setCursorPos(x+11,y+2)
  term.write(" ")
  term.setCursorPos(x+11,y+3)
  term.write(" ")
end

function printC(text,y)
  if not y then
    error("printC:No Y value specified")
  end
  term.setCursorPos(math.ceil(w/2-#tostring(text)/2),y)
  term.write(text)
end

function window(_w,_h,head,ypos)
  if not _w or not _h then
    error("Window:Missing width (w) or height (h)")
  end
  if not ypos or type(ypos) ~= "number" then ypos = 5 end
  sColor(colors.white)
  term.setCursorPos(1,h)
  term.write(string.rep(" ",w))
  sColor(nastaveni.barva)
  term.setCursorPos(w,1)
  term.write(" ")
  printC(string.rep(" ",_w),ypos)
  term.setTextColor(colors.white)
  if head then
    printC(head,ypos)
  end
  sColor(colors.gray)
  for i = 1, _h do
    printC(string.rep(" ",_w),i+ypos)
  end
end

function testSpot(x,y)
  if y > 1 then
    if #cDir < 17 then
      _rep = #cDir
    else
      _rep = 16
    end
    for _c = 1, _rep do
      if x >= filePos[_c][1] and x <= filePos[_c][1]+10 and y >= filePos[_c][2] and y <= filePos[_c][2]+2 then
        return _c
      end
    end
  end
  return false
end

function bar()
  sColor(nastaveni.barva)
  term.setCursorPos(1,1)
  term.write(string.rep(" ",w-1))
  sColor(colors.red)
  term.setTextColor(colors.white)
  term.write("Q")
end

function drawDir()
  clear(colors.white)
  bar ()
  sColor(nastaveni.barva)
  cDir = fs.list(shell.dir())
  if shell.dir() == "" then
    printC("C: - File Manager "..version,1)
  else
    printC("/"..shell.dir(),1)
  end
  for i = 1, #cDir do
    local num = i
    local a = i+16*(cPage-1)
    local name = tostring(cDir[a])
    if name == "nil" then break end
    if #name > 9 then
      text = string.sub(name,1,6).."..."
    else
      text = name
    end
    if fs.isDir(shell.dir().."/"..name) then
      sColor(colors.orange)
    else
      sColor(colors.lightGray)
    end 
    if shell.dir() == "" then
      location = "C:/"..name
    else
      location = tostring("C:/"..shell.dir().."/"..name)
    end
    if i > 16 then break end
    x = filePos[i][1]
    y = filePos[i][2]
    term.setCursorPos(x,y)
    term.write(string.rep(" ",11))
    term.setCursorPos(x,y+1)
    term.write(string.rep(" ",math.floor(5.5-#tostring(text)/2)))
    term.write(text)
    term.write(string.rep(" ",math.ceil(5.5-#tostring(text)/2)))
    term.setCursorPos(x,y+2)
    term.write(string.rep(" ",11))
  end
  if #cDir > 16 then
    nPage = math.ceil(#cDir/16)
    term.setTextColor(colors.black)
    sColor(colors.white)
    printC("Page "..cPage.." of "..nPage,2)
    nSelect = cDir[selected+16*(cPage-1)]
  else
    nSelect = cDir[selected]
  end
  if selected > 0 then
    drawS(filePos[selected][1],filePos[selected][2],true)
    sType = fs.isDir(shell.resolve(nSelect))
    menuS = true
  else
    menuS = false
  end
end

function confirm(text)
  if not text or #text <= 8 then
    window(8,3,text)
  else
    window(#text+2,3,text)
  end    
  term.setCursorPos(23,7)
  sColor(colors.lime)
  term.write("Yes")
  term.setCursorPos(27,7)
  sColor(colors.red)
  term.write("No")
  while true do
    event,par1,par2,par3 = os.pullEvent("mouse_click")
    if par3 == 7 then
      if par2 >= 23 and par2 <=25 then
        return true
      elseif par2 >= 27 and par2 <= 28 then
        return false
      end
    end
  end
end

function changeDir()
  window(26,4,"Change Directory")
  printC("Use '/' for root",8)
  sColor(colors.black)
  repeat    
    printC(string.rep(" ",18),7)  
    term.setCursorPos(17,7)
    input = read() 
  until input ~= ""
  path = shell.resolve(input)
  if fs.exists(path) and fs.isDir(path) then
    shell.setDir(path)
  else
    sColor(colors.gray)
    term.setTextColor(colors.red)
    printC("Doesn't Exist!",9)
    sleep(1)
  end
end

function info(_path)
  if not _path then
    error("No path")
  end
  name = cDir[_path]
  clear(colors.white)
  term.setTextColor(colors.white)
  sColor(nastaveni.barva)
  term.write(string.rep(" ",w))
  printC(name,1)
  term.setCursorPos(w,1)
  sColor(colors.red)
  print("X")
  term.setCursorPos(1,2)
  term.setTextColor(colors.black)
  sColor(colors.white)
  print(" ")
  term.write("File Path:")
  path = shell.resolve(name)
  print("/"..path)
  term.write("Type:")
  if fs.isDir(path) then
    print("Folder")
  else
    print("File")
    print("Size:"..fs.getSize(path))
  end
  if fs.isReadOnly(shell.resolve(nSelect)) then
    print("This is a read-only item")
  end
  if shell.resolve(nSelect) == tProgram then
    term.setTextColor(colors.orange)
    print("This program is currently running")
  end
  while true do
    event,par1,par2,par3 = os.pullEvent()
    if event == "mouse_click" then
      if par2 == w and par3 == 1 then
        break
      end
    end
  end
end

function addItem()
  window(8,7,"Add...")
  term.setTextColor(colors.white)
  sColor(colors.lime)
  printC("Folder",7)
  printC("File",9)
  sColor(colors.red)
  printC("Cancel",11)
  local choice = nil
  while true do
    event,par1,par2,par3 = os.pullEvent("mouse_click")
    if par3 == 7 and par2 >= 23 and par2 <= 28 then
      choice = 1
      break
    elseif par3 == 9 and par2 >= 24 and par2 <= 27 then
      choice = 2
      break
    elseif par3 == 11 and par2 >= 23 and par2 <=28 then
      choice = 3
      break
    end
  end
  if choice ~= 3 then
    window(26,3,"Enter name")
    sColor(colors.black)
    repeat
      printC(string.rep(" ",22),7)
      term.setCursorPos(15,7)
      input = read()
    until input ~= ""
    if fs.exists(shell.resolve(input)) then
      sColor(colors.gray)
      term.setTextColor(colors.red)
      printC("Already Exists!",8)
      sleep(2)
    elseif choice == 1 then
      fs.makeDir(shell.dir().."/"..input)
    elseif choice == 2 then
      f = fs.open(shell.dir().."/"..input,"w")
      f.close()
    end
  end
end

function move(item)
  path = shell.resolve(item)
  if #path <= 26 then
    window(26,5,"Move item")
  else
    window(#path+2,5,"Move item")
  end
  printC("Current path:",6)
  printC(path,7)
  printC("New path:",8)
  printC("Use 'cancel' to cancel",10)
  sColor(colors.black)
  repeat
    printC(string.rep(" ",22),9)
    term.setCursorPos(15,9)
    input = read()
  until input ~= ""
  sColor(colors.gray)
  printC(string.rep(" ",26),10)
  term.setTextColor(colors.red)
  if fs.exists(input) then
    printC("Already Exists!",10)
    sleep(2)
  elseif input == "cancel" then
    printC("Canceled",10)
    sleep(2)
  else
    fs.move(path,input)
    term.setTextColor(colors.lime)
    printC("Success",10)
    sleep(2)
  end    
end

function cRename(item)
  path = shell.resolve(item)
  if #path <= 26 then
    window(26,5,"Rename item")
  else
    window(#path+2,5,"Rename item")
  end
  printC("Current name:",6)
  printC(item,7)
  printC("New name:",8)
  printC("Use 'cancel' to cancel",10)
  sColor(colors.black)
  repeat
    printC(string.rep(" ",22),9)
    term.setCursorPos(15,9)
    input = read()
  until input ~= ""
  sColor(colors.gray)
  printC(string.rep(" ",26),10)
  term.setTextColor(colors.red)
  if fs.exists(shell.resolve(input)) then
    printC("Already Exists!",10)
    sleep(2)
  elseif string.find(input,"/") then 
    printC("Cannot contain '/'",10)
    sleep(2)
  elseif input == "cancel" then
    printC("Canceled",10)
    sleep(2)
  else
    fs.move(path,input)
    term.setTextColor(colors.lime)
    printC("Success",10)
    sleep(2)
  end
end

--Main loop program--
function main()
  while true do
    drawDir()
    if menuS then
      term.setTextColor(colors.black)
      sColor(colors.white)
      printC(nSelect,h-1)
      if sType then  --If the item is a folder
        if fs.isReadOnly(shell.resolve(nSelect)) then  --If the folder is read-only
          printC("This is a read-only folder  O:Open Tab:Info",h)
        else  --Normal folder
          printC("O:Open  D:Delete  M:Move  N:Rename  Tab:Info",h)
        end
      else  --If the item is a file
        if shell.resolve(nSelect) == tProgram then  --If the file is this program
          printC("This program is currently running  Tab:Info",h)
        elseif fs.isReadOnly(shell.resolve(nSelect)) then  --If the file is read-only
          printC("This is a read-only file  R:Run P:Preview Tab:Info",h)
        else  --Normal file
          printC("R:Run E:Edit D:Delete M:Move N:Rename Tab:Info",h)
        end
      end
      term.setTextColor(colors.white)
    else
      term.setTextColor(colors.black)
      sColor(colors.white)
      if fs.isReadOnly(shell.dir()) then --If folder is read--only
        if #cDir > 16 then
          printC("B:Back  Tab:Enter Path  Page:PgUp/Down",h)
        else
          printC("B:Back  Tab:Enter Path",h)
          cPage = 1
        end
      else  --Normal Folder
        if #cDir > 16 then
          printC("C:Create... B:Back Tab:Enter path Page:PgUp/Down",h)
        else
          printC("C:Create...  B:Back  Tab:Enter path",h)
          cPage = 1
        end
      end
      term.setTextColor(colors.white)
    end
    event,par1,par2,par3,par4 = os.pullEvent()  --Main pullEvent--
    if event == "mouse_click" then
      if par2 == w and par3 == 1 then
        break
      else
        click = testSpot(par2,par3)
        if click then
          selected = click
        else
          selected = 0
        end
      end
    elseif event == "key" then
      if selected > 0 then  --If an item is selected
        if par1 == keys.tab then 
          info(selected)
        end
        if shell.resolve(nSelect) ~= tProgram then
          if par1 == keys.d and not fs.isReadOnly(shell.resolve(nSelect)) then
            if confirm(" Delete? ") then
              fs.delete(shell.resolve(nSelect))
              selected = 0
            end          
          elseif par1 == keys.m and not fs.isReadOnly(shell.resolve(nSelect)) then
            os.pullEvent()
            move(nSelect)
            selected = 0
          elseif par1 == keys.n and not fs.isReadOnly(shell.resolve(nSelect)) then
            os.pullEvent()
            cRename(nSelect)
            selected = 0
          end
          if sType then --If the item is a directory
            if par1 == keys.o then
              shell.setDir(shell.resolve(nSelect))
              selected = 0          
            end
          else  --If the item is a file
            if par1 == keys.r then
              clear(colors.black)
              print("Begin program:")
              state,err = pcall(function() shell.run(nSelect) end )
              if err then
                clear(colors.white)
                bar()
                printC("Program Error Report",1)
                term.setCursorPos(1,3)
                term.setTextColor(colors.black)
                print("An error occured while running "..nSelect)
                print(err)
                while true do
                  event,par1,par2,par3 = os.pullEvent("mouse_click")
                  if par2 == w and par3 == 1 then
                    break
                  end
                end
              else
                term.setTextColor(colors.white)
                print("End of program")
                print("Press any key...")
                term.setTextColor(colors.black)
                print("End of program")
                print("Press any key...")
                os.pullEvent("key")
              end
              selected = 0
            elseif par1 == keys.e or par1 == keys.p then
              os.pullEvent()
              shell.run("edit "..nSelect)
              selected = 0
            end
          end
        end
      else  --If nothing is selected
        if par1 == keys.tab then 
          changeDir() 
        elseif par1 == keys.c and not fs.isReadOnly(shell.resolve(shell.dir())) then
          addItem()
        elseif par1 == keys.b then
          shell.run("cd ..")
        elseif par1 == 209 then
          if cPage > 1 then cPage = cPage-1 end
        elseif par1 == 201 then
          if cPage < nPage then cPage = cPage+1 end
        end 
      end
    end
  end
end



--Program--
state,err = pcall(function() main() end)

if err then
  if not string.find("Terminated",err) then
    term.setTextColor(colors.white)
    sColor(colors.black)
    print("CRITICAL ERROR:")
    print(err)
    print("Press any key to continue")
    os.pullEvent("key")
  end
end

--after program ends
plocha ()

end

function prehravac ()
local f=fs.open("//.core/nastaveni.cfg","r")
local cnt=f.readAll()
f.close()
local nastaveni=textutils.unserialize(cnt)

term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(nastaveni.barva)
term.setTextColor(colors.white)
print "                                                   "
term.setCursorPos(19,1)
print" Media player "
term.setTextColor(colors.black)

term.setCursorPos(5,8)
term.setBackgroundColor(colors.yellow)
term.setTextColor(colors.black)
print" Play "
term.setCursorPos(41,8)
print" Stop "
paintutils.drawPixel(51, 1, colors.red)
term.setTextColor(colors.white)
term.setCursorPos(51, 1)
print "Q"



while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if xy == "51,1" and button == 1 then
plocha ()
break
end

if xy == "6,8" and button == 1 then
disk.playAudio("left")
disk.playAudio("right")
disk.playAudio("bottom")
prehravac ()
break
end

if xy == "7,8" and button == 1 then
disk.playAudio("left")
disk.playAudio("right")
disk.playAudio("bottom")
prehravac ()
break
end

if xy == "8,8" and button == 1 then
disk.playAudio("left")
disk.playAudio("right")
disk.playAudio("bottom")
prehravac ()
break
end

if xy == "9,8" and button == 1 then
disk.playAudio("left")
disk.playAudio("right")
disk.playAudio("bottom")
prehravac ()
break
end

if xy == "10,8" and button == 1 then
disk.playAudio("left")
disk.playAudio("right")
disk.playAudio("bottom")
prehravac ()
break
end

if xy == "11,8" and button == 1 then
disk.playAudio("left")
disk.playAudio("right")
disk.playAudio("bottom")
prehravac ()
break
end

if xy == "12,8" and button == 1 then
disk.playAudio("left")
disk.playAudio("right")
disk.playAudio("bottom")
prehravac ()
break
end

if xy == "45,8" and button == 1 then
print("Stop")
disk.stopAudio("left")
disk.stopAudio("right")
disk.stopAudio("bottom")
prehravac ()
break
end

if xy == "42,8" and button == 1 then
print("Stop")
disk.stopAudio("left")
disk.stopAudio("right")
disk.stopAudio("bottom")
prehravac ()
break
end

if xy == "43,8" and button == 1 then
print("Stop")
disk.stopAudio("left")
disk.stopAudio("right")
disk.stopAudio("bottom")
prehravac ()
break
end

if xy == "44,8" and button == 1 then
print("Stop")
disk.stopAudio("left")
disk.stopAudio("right")
disk.stopAudio("bottom")
prehravac ()
break
end
end
end

function malovani ()
shell.run ("np")
end

function textovani ()
shell.run ("ink")
end

function nastaveni ()
local f=fs.open("//.core/nastaveni.cfg","r")
local cnt=f.readAll()
f.close()
local nastaveni=textutils.unserialize(cnt)
term.setBackgroundColor(colors.orange)
term.setTextColor(colors.black)
term.setCursorPos(13, 8)
print "          "
term.setCursorPos(13, 9)
print "          "
term.setCursorPos(12, 10)
print "           "
term.setCursorPos(12, 11)
print "           "
term.setCursorPos(12, 12)
print "           "
term.setCursorPos(12, 13)
print "           "
term.setCursorPos(12, 14)
print "           "
term.setCursorPos(12, 15)
print "           "
term.setBackgroundColor(colors.white)
term.setCursorPos(13, 9)
print " Options "
term.setCursorPos(13, 10)
term.setBackgroundColor(colors.lime)
print " Credits "
term.setCursorPos(13, 11)
term.setBackgroundColor(colors.magenta)
print " Users   "
term.setCursorPos(13, 12)
term.setBackgroundColor(colors.white)
print "         "
term.setCursorPos(13, 13)
term.setBackgroundColor(colors.lightBlue)
print " Backgro."
term.setCursorPos(13, 14)
term.setBackgroundColor(colors.yellow)
print " Upgrade "
paintutils.drawPixel(12, 9, colors.gray)
term.setCursorPos(12, 9)
print "Q"
term.setBackgroundColor(colors.orange)
local time = os.time()
local formattedTime = textutils.formatTime(time, true)
term.setCursorPos(1,18)
print("" .. formattedTime)

while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if xy == "12,9" and button == 1 then
plocha ()
elseif x >= 14 and x <= 20 and y == 10 and button == 1 then
credits ()
elseif x >= 14 and x <= 20 and y == 11 and button == 1 then
uzivatele ()
elseif x >= 14 and x <= 20 and y == 13 and button == 1 then
pozadi ()
elseif x >= 14 and x <= 20 and y == 14 and button == 1 then
aktualizace ()
else
plocha ()
break
end
end
end

function credits ()
local f=fs.open("//.core/nastaveni.cfg","r")
local cnt=f.readAll()
f.close()
local nastaveni=textutils.unserialize(cnt)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(nastaveni.barva)
term.setTextColor(colors.white)
print "                                                   "
term.setCursorPos(23,1)
prc" Credits "
paintutils.drawPixel(51, 1, colors.red)
term.setTextColor(colors.white)
term.setCursorPos(51, 1)
print "Q"
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(5, 5)
print (" - NPaint")
term.setCursorPos(5, 7)
print (" - File Manager v1.2")
term.setCursorPos(5, 9)
print (" - Ink by oeed")
term.setCursorPos(5, 11)
print (" - Pepdroll addons")
term.setCursorPos(5, 13)
print (" - Dynet client by Laurens Weyn")
term.setCursorPos(5, 15)


while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if xy == "51,1" and button == 1 then
plocha ()
break
end
end
end

function pastebin ()
local f=fs.open("//.core/nastaveni.cfg","r")
local cnt=f.readAll()
f.close()
local nastaveni=textutils.unserialize(cnt)
term.setBackgroundColor(colors.white)
term.clear ()
term.setBackgroundColor(nastaveni.barva)
term.setCursorPos(1,1)
print "                                                   "
term.setTextColor(colors.white)
prc ("Pastebin Manager", 1)
term.setTextColor(colors.black)
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(5,4)
print "- Run "
term.setCursorPos(5,6)
print "- Get "
term.setCursorPos(5,8)
print "- Put "
term.setBackgroundColor(colors.red)
term.setCursorPos(51, 1)
print " "
term.setTextColor(colors.white)
term.setCursorPos(51, 1)
print "Q"
term.setTextColor(colors.black)
term.setBackgroundColor(colors.white)

while true do
local event, button, x, y = os.pullEventRaw()
if event == "mouse_click" then
if x >= 5 and x <= 10 and y == 4 and button == 1 then
spustit ()
elseif x >= 5 and x <= 10 and y == 6 and button == 1 then
ulozit ()
elseif x >= 5 and x <= 10 and y == 8 and button == 1 then
nahrat ()
elseif x == 51 and y == 1 and button == 1 then
plocha ()
end
end
end
end

function spustit ()
term.setCursorPos(4,12)
print "enter the code"
term.setCursorPos(4,13)
term.setBackgroundColor(colors.lightBlue)
print "          "
term.setCursorPos(4,13)
kod1 = io.read ()
if kod1 == "" then
pastebin ()
else
shell.run ("pastebin run",kod1)
plocha ()
end

while true do
if x == 51 and y == 1 and button == 1 then
plocha ()
end
end
end

function ulozit ()
term.setCursorPos(4,12)
print "enter the code"
term.setCursorPos(4,13)
term.setBackgroundColor(colors.lightBlue)
print "          "
term.setCursorPos(4,13)
kod1 = io.read ()
if kod1 == "" then
pastebin ()
else
term.setBackgroundColor(colors.white)
term.setCursorPos(4,12)
print "enter the path"
print "                                     "
end
term.setCursorPos(4,13)
term.setBackgroundColor(colors.lightBlue)
print "          "
term.setCursorPos(4,13)
kod2 = io.read ()
if kod2 == "" then
pastebin ()
else
shell.run ("pastebin get",kod1,kod2)
prohlizec ()
end
while true do
if x == 51 and y == 1 and button == 1 then
plocha ()
end
end
end

function nahrat ()
term.setCursorPos(4,12)
print "enter the path"
term.setCursorPos(4,13)
term.setBackgroundColor(colors.lightBlue)
print "          "
term.setCursorPos(4,13)
kod1 = io.read ()
if kod1 == "" then
pastebin ()
else
shell.run ("pastebin put",kod1)
end
pokr = io.read ()
if pokr == "" then
plocha ()
else
plocha ()
end
while true do
if x == 51 and y == 1 and button == 1 then
plocha ()
end
end
end

function web ()
term.clear ()
shell.run ("web")
end

function uzivatele ()
local f=fs.open("//.core/nastaveni.cfg","r")
local cnt=f.readAll()
f.close()
local nastaveni=textutils.unserialize(cnt)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(nastaveni.barva)
term.setTextColor(colors.white)
print "                                                   "
term.setCursorPos(23,1)
prc"  Users "
paintutils.drawPixel(51, 1, colors.red)
term.setTextColor(colors.white)
term.setCursorPos(51, 1)
print "Q"
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setBackgroundColor(colors.pink)
term.setCursorPos(5, 7)
print "- Create new"

while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if xy == "51,1" and button == 1 then
plocha ()
break
end

if x >= 5 and x <= 16 and y == 7 and button == 1 then
registrace()
break
end
end
end

function registrace()
  local next = false
  local user
  local pass
  clear ()
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.clear()
  term.setCursorPos(5,7)
  print("Register new user")
  term.setCursorPos(5,9)
  print("User name: ")
  term.setCursorPos(5,11)
  print(" Password: ")
  term.setBackgroundColor(colors.lightBlue)
  term.setCursorPos(18,9)
  print "         "
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,9)
  user = read()
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,11)
  pass = read("@")
  if not fs.isDir("/.ucet/"..user) then
    fs.makeDir("/.ucet/"..user)
    local file = fs.open(".ucet/"..user.."/.data","w")
    file.writeLine(user)
    file.writeLine(pass)
    file.close()
    if not fs.exists("/.core/ucet") then
      fs.open("/.core/ucet","w")
    end
	term.setBackgroundColor(colors.white)
    fgc(colors.lime)
	term.setCursorPos(5,13)
    print("successful")
	sleep (1)
	uzivatele ()
  else
    term.setBackgroundColor(colors.white)
    fgc(colors.red)
	term.setCursorPos(5,13)
    print("Name is exist")
	sleep (1)
	uzivatele ()
  end
end

function pozadi ()
local f=fs.open("//.core/nastaveni.cfg","r")
local cnt=f.readAll()
f.close()
local nastaveni=textutils.unserialize(cnt)
term.setCursorPos(1,1)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(nastaveni.barva)
term.setTextColor(colors.white)
print "                                                   "
term.setBackgroundColor(nastaveni.barva)
term.setTextColor(colors.white)
term.setCursorPos(19, 1)
prc("Background")
paintutils.drawPixel(51, 1, colors.red)
term.setTextColor(colors.white)
term.setCursorPos(51, 1)
print "Q"
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(5, 7)
print "- Custom"
term.setCursorPos(5, 9)
print "- Farm"
term.setCursorPos(5, 11)
print "- Island"
term.setCursorPos(5, 13)
print "- Win XP "
term.setCursorPos(5, 5)
print "- Color  "

while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if x >= 5 and x <= 12 and y == 5 and button == 1 then
barvapozadi ()
break
end

if xy == "51,1" and button == 1 then
plocha ()
break
end

if x >= 5 and x <= 12 and y == 7 and button == 1 then
namalovat ()
break
end

if x >= 5 and x <= 12 and y == 9 and button == 1 then
statek ()
break
end

if x >= 5 and x <= 12 and y ==11 and button == 1 then
ostrov ()
end

if x >= 5 and x <= 12 and y ==13 and button == 1 then
xp ()
end
end
end

function xp ()
shell.run ("delete bar.nfp")
shell.run ("pastebin get iy3EcA4Z bar.nfp")
plocha ()
end

function ostrov ()
shell.run ("delete bar.nfp")
shell.run ("pastebin get vm1p8DXs bar.nfp")
plocha ()
end

function statek ()
shell.run ("delete bar.nfp")
shell.run ("pastebin get K0zXPiKn bar.nfp")
plocha ()
end

function namalovat ()
shell.run (".core/paint bar.nfp")
end

function aktualizace ()
local f=fs.open("//.core/nastaveni.cfg","r")
local cnt=f.readAll()
f.close()
local nastaveni=textutils.unserialize(cnt)
term.setCursorPos(1,1)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(nastaveni.barva)
term.setTextColor(colors.white)
print "                                                   "
term.setBackgroundColor(nastaveni.barva)
term.setTextColor(colors.white)
term.setCursorPos(20, 1)
prc("Upgrade")
paintutils.drawPixel(51, 1, colors.red)
term.setTextColor(colors.white)
term.setCursorPos(51, 1)
print "Q"
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(5, 14)
print ("Current version "..verze)
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(5, 7)
print "- Upgrade"
term.setCursorPos(5, 9)
print "- upgrade to Pepdroll 6.2"

while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if x >= 5 and x <= 18 and y ==9 and button == 1 then
fs.delete ("/.core/ucet")
fs.delete ("/.ucet")
fs.delete ("/startup")
shell.run ("pastebin get 3R3M6XeK startup")
os.reboot ()
break
end

if xy == "51,1" and button == 1 then
plocha ()
break
end

if x >= 5 and x <= 18 and y ==7 and button == 1 then
kontrola ()
break
end
end
end

function barvapozadi ()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
local nastaveni={barva=colors.magenta}
term.setCursorPos (5, 17)
print"Paste color name:"
term.setCursorPos (5, 18)
local novaBarva=read() --#precist hodnotu od uzivatele
if colors[novaBarva:lower()] then
  nastaveni.barva=colors[novaBarva:lower()]
else
  print"Barva nenalezena, ponechano puvodni nastaveni"
end
--#ulozime
local f=fs.open("//.core/nastaveni.cfg","w")
f.write(textutils.serialize(nastaveni))
f.close()
pozadi ()
end
boot ()