--get arguments

local tArgs = { ... }
musicplaylist = false
selectedsong = nil
displaynotes = ""
mute = {{ },{ },{ },{ },{ }}
i = 1
j = 1
selectedi = 1
selectedj = 1
ptoggle = 0
debug = false
--attaching ironblocks
ironblock1 = peripheral.wrap("left")
ironblock2 = peripheral.wrap("right")
ironblock3 = peripheral.wrap("back")
ironblock4 = peripheral.wrap("top")
ironblock5 = peripheral.wrap("bottom")

function round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

input = function(evt, arg1, arg2)
	if debug and evt == "key" and arg1 == 41 then
		debug = false
	elseif evt == "key" and arg1 == 41 then
		debug = true
	end
	
	if debug then
		if evt == "key" and arg1 == 17 and selectedi < 5 then
			selectedi = selectedi + 1
		elseif evt == "key" and arg1 == 31 and selectedi > 1  then
			selectedi = selectedi - 1
		elseif evt == "key" and arg1 == 32 and selectedj < 5 then
			selectedj = selectedj + 1
		elseif evt == "key" and arg1 == 30 and selectedj > 1 then
			selectedj = selectedj - 1
		end
		
		if evt == "key" and arg1 == 57 and mute[selectedi][selectedj] ~= 0 then
			mute[selectedi][selectedj] = 0
		elseif evt == "key" and arg1 == 57 and mute[selectedi][selectedj] == 0 then
			mute[selectedi][selectedj] = 1
		end
	end

	if evt == "key" and arg1 == 15 and ptoggle ~= 0 then
		ptoggle = 0
	elseif evt == "key" and arg1 == 15 and ptoggle == 0 then
		ptoggle = 1
	end

	if evt == "key" and arg1 >= 2 and arg1 <= 12 then
		factor = allNotes / 10
		i =  round(factor * (arg1 - 2), 0)
	end
end

playnotes = function(noteblockside, channle, int)
displaynotes = displaynotes .. "("
if song[i][channle] then

if song[i][channle][1] and mute[channle][1] ~= 0 then
note = song[i][channle][1]
noteblockside.playNote(int,note)
displaynotes = displaynotes .. note .. ","
end
if song[i][channle][2] and mute[channle][2] ~= 0 then
note = song[i][channle][2]
noteblockside.playNote(int,note)
displaynotes = displaynotes .. note .. ","
end
if song[i][channle][3] and mute[channle][3] ~= 0 then
note = song[i][channle][3]
noteblockside.playNote(int,note)
displaynotes = displaynotes .. note .. ","
end
if song[i][channle][4] and mute[channle][4] ~= 0 then
note = song[i][channle][4]
noteblockside.playNote(int,note)
displaynotes = displaynotes .. note .. ","
end
if song[i][channle][5] and mute[channle][5] ~= 0 then
note = song[i][channle][5]
noteblockside.playNote(int,note)
displaynotes = displaynotes .. note .. ","
end
end
displaynotes = displaynotes .. ")"
end

-- Song selection stuff
if fs.exists("playlist") and #tArgs < 1 then
  local file = fs.open("playlist", "r")
   playlist = {}
                   toltal = 0
                  while true do
                 local songque = file.readLine()
                 if songque == nil then --if reached end of file
   break end
                   table.insert(playlist, songque)
                   toltal = toltal + 1
                   musicplaylist = true
  end
  selectedsong = playlist[1]
elseif #tArgs < 1 then
   shell.run("clear")
   print( "Use the arrows and enter to chose.  " )
   songlist = fs.list("Music")
   i = 0
   max = -1
   while true do
           i = i + 1
           max = max + 1
                   if songlist[i] == nil then break end
   end
                   pointer = 1
                   term.setCursorPos(1, 10)
  term.clearLine(none)
                   print( "->" .. pointer .. ": " .. songlist[pointer])
          while true do
          
                                        list = 1
                                        while list < 9 do
                                         term.setCursorPos(1, 10 - list)
                                         term.clearLine(none)
                                         if songlist[pointer - (list)] then
                                         write( "  " .. pointer - (list) .. ": " .. songlist[pointer - (list)])  
                                         end
                                         list = list + 1
                                        end                       
                                        term.setCursorPos(1, 10)
                                        term.clearLine(none)
                                        print( "->" .. pointer .. ": " .. songlist[pointer])
list = 1
while list < 10 do
                                         term.setCursorPos(1, 10 + list)
                                         term.clearLine(none)
                                         if songlist[pointer + (list)] then
                                         write( "  " .. pointer + (list) .. ": " .. songlist[pointer + (list)])  
                                         end
                                         list = list + 1
                                        end
local evt, arg1, arg2 = os.pullEvent()
                                        if arg1 == 28 then break end
                                        if arg1 == 200 and pointer > 1 then
                                                        pointer = pointer - 1
                                        elseif arg1 == 208 and pointer < max then
                                                        pointer = pointer + 1
                                        end
                                        
         end
          
selectedsong = songlist[pointer]
else
selectedsong = tArgs[1]
end

que = 0
while true do
if musicplaylist then
if que >= toltal then
que = 1
else
que = que + 1
end
selectedsong = playlist[que]
end

-- Playing The Music

--opening file
shell.run( "clear" )
if fs.exists("Instrument/" .. selectedsong) then
shell.run( "Instrument/" .. selectedsong )
else
int1 = 0
int2 = 0
int3 = 0
int4 = 0
int5 = 0
end
file = io.open( "Music/" .. selectedsong )
filedata = file:read()
file:close()
--converting file to table
song = textutils.unserialize(filedata)
allNotes = song["lenght"] / song["delay"]
-- Song info
term.setCursorPos(1,1)
print("Song:" .. song["name"] )
print("Author:" .. song["author"] )
print("Original Author:" .. song["original_author"] )
-- Start playing
i = 0
line1 = ""
line2 = ""
line3 = ""
line4 = ""
line5 = ""
line6 = ""
line7 = ""
line8 = ""
line9 = ""
line10 = ""
while i * song["delay"] < song["lenght"] + 1 do
term.setCursorPos(1,4)
if ptoggle ~= 1 then
	term.clearLine()
	print(i * song["delay"]  .."/" .. song["lenght"] .. " Seconds" )
else
	term.clearLine()
	print(i .."/" .. allNotes .. " Notes" )
end
if debug then
	print("[" .. selectedi .. "," .. selectedj .. "]")
else
	term.clearLine()
end
--clear all notes
displaynotes = "["

-- read notes for up to 5 parts
i = i + 1
if song[i] then
if ironblock1 then playnotes(ironblock1, 1, int1) end
if ironblock2 then playnotes(ironblock2, 2, int2) end
if ironblock3 then playnotes(ironblock3, 3, int3) end
if ironblock4 then playnotes(ironblock4, 4, int4) end
if ironblock5 then playnotes(ironblock5, 5, int5) end
end
displaynotes = displaynotes .. "]                                  "
line1 = line2
line2 = line3
line3 = line4
line4 = line5
line5 = line6
line6 = line7
line7 = line8
line8 = line9
line9 = line10
line10 = displaynotes
term.setCursorPos(1,9)
term.clearLine()
print(line1)
term.clearLine()
print(line2)
term.clearLine()
print(line3)
term.clearLine()
print(line4)
term.clearLine()
print(line5)
term.clearLine()
print(line6)
term.clearLine()
print(line7)
term.clearLine()
print(line8)
term.clearLine()
print(line9)
term.clearLine()
print(line10)



timerID = os.startTimer(song["delay"])
evt, arg1, arg2 = os.pullEvent()
input(evt, arg1, arg2)
if evt ~= "timer" then	
	os.pullEvent("timer")
end

end


if musicplaylist == false then break end
end
-- End Playing