require ('iuplua')

local cancelflag		
local downloadProgress		

local function downloadStart(YtLink)
     cancelButton = iup.button{
        title = "Cancel",
        action = function()	      
            cancelflag = true         
          end

    }
    local downloadProgress = iup.progressbar{expand="HORIZONTAL", MARQUEE="YES"}	
    local dlgProgress = iup.dialog{				   
        title = "converting",
        dialogframe = "YES", border = "YES",
        iup.vbox {			
            downloadProgress,
            cancelButton,
        }
    }
    dlgProgress.size = "QUARTERxEIGHT" 		
    dlgProgress.menubox = "NO"		
    dlgProgress.close_cb = cancelButton.action	
    dlgProgress:showxy(iup.CENTER, iup.CENTER)	
    return dlgProgress
end


local function DL_progress(YtLink) 
   downloadStart() 		  
   local pipe = io.popen(YtLink)
   --os.execute(YtLink)
   
    repeat
        local c = pipe:read()	  
        if c then 		
             
             iup.Flush()		     
            
             iup.LoopStep() 
             iup.Flush() 
             if cancelflag then  
                   
                   iup.Message("Terminated", "You have canceled the conversion") 
                   iup.Flush()
                   break 

             end

       end
       

        

until not c
pipe:close() 
iup.Close()
return
end

convert_mp3 = {}
convert_mp3.go = function(tubeLink)
  DL_progress(tubeLink)
  return
end