
import java.lang.*;
import java.io.*;
import java.util.StringTokenizer;
import Gui;
import DownloadURL;
import GetPage;
import Search;
import SharedFile;


public class Download  implements Runnable
{

        public String scFormatFileName;
        public String downloadTempLinksFileName;
        public String directory;

        public static int MAX = 100;
        public DownloadURL urlList[];
//        public int noOfUrls;



       public SharedFile sharedFile;
       public Gui gui;
//       public Search search;
       public Thread t;

       Download(String scfile,String tempf)
       {
            /* input file : "scLinks.dat"
               tempf : "dTempLinks.dat"
             */

            downloadTempLinksFileName = new String(tempf);
            scFormatFileName = new String(scfile);

            urlList = new DownloadURL[MAX];

            for(int i=0; i<MAX; i++)
                 urlList[i] = new DownloadURL();
            directory = System.getProperty("user.dir");

//            noOfUrls = -1;

       }
       public void start()
       {
           t = new Thread(this,"download");
           t.start();
       }
       public void stop()
       {
           sharedFile.windUp(scFormatFileName);
           t.stop();
       }

  
       public void setSharedFile(SharedFile sf)
       {
           this.sharedFile = sf;
       }

       public void setGui(Gui g)
       {
          this.gui = g;
       }

       public void setDirectory(String d)
       {
         this.directory = new String(d);
       }


       public void construct(int depth)
       {
           sharedFile.construct(depth,scFormatFileName,downloadTempLinksFileName);
       }

       public void run()
       {
            GetPage page = new GetPage(gui);
            Parser parser = new Parser();
            long retValue = -2;
            while(true)
            {   
                if(sharedFile.isFileChanged)
                    sharedFile.readFile();
                
                for(int count = 0; count < MAX; count++)
                {
                    if(urlList[count].over == 0)
                    {
                        urlList[count].timesRepeated++;

                        /**code for downloading http & ftp pages
                        */
                        gui.displayMessage("Downloading "+urlList[count].url+"...\r\n");
                        try
                        {
                          retValue = page.retrieve(urlList[count].url,urlList[count].bytesCompleted,urlList[count].fileName);
                        }
                        catch(IOException ie)
                        {
                           System.out.println(ie);
                        }
                        if(retValue >= 0)
                        {
                               urlList[count].bytesCompleted = retValue;
                               gui.displayMessage("Partially download  "+urlList[count].url+"\r\n");
                               gui.displayMessage("Bytes Completed : "+retValue+"\r\n");
                        }
                        else
                        if(retValue == -1)
                        {
                            gui.displayMessage(urlList[count].url+"Downloaded  successfully\r\n");
                            if(urlList[count].depth >0)
                            {
                                try
                                {
                                  FileOutputStream f = new FileOutputStream(downloadTempLinksFileName);
                                  f.close();
                                }
                                catch(FileNotFoundException fe)
                                {
                                }
                                catch(IOException ie)
                                {

                                }
                                

                                //Parsing of the downloaded page
                                //add it to the list of links

                                System.out.println("comes here");
                                parser.parse(urlList[count].fileName,downloadTempLinksFileName,"",false);
                                construct(urlList[count].depth - 1);

                            }
                            urlList[count].over = 1;
                        }
                        else
                        if((retValue == -3)||(urlList[count].timesRepeated > 10))
                        {
                            urlList[count].over = 1;
                            gui.displayMessage(urlList[count].url+" Downloading Aborted permanently\r\n");
                        }
                        else
                        if(retValue == -2)
                             gui.displayMessage("Temporary error..trying later\r\n");


                    }
                    
                }

                //ditch **remove the ones with 1 from data structure


            }

        }
     
             
}
                


