/**This is the main class which interfaces the rest
*/

import java.lang.String;
import java.util.StringTokenizer;
import java.io.*;
import GetSites;
import Parser;
import Database;
import Download;
import SharedFile;
import Gui;

public class Search
{
    
   public String siteNames;
   public int linkRange;
   public int depth;
   public boolean searchDBase;
   public boolean searchNet;

   String indexFileName;
   String linksFileName;
   String countFileName;

   String storeFileName;
   String downloadUrlListFileName;
   String searchTempLinksFileName;
   String scFormatFileName;


   GetSites getSites;

   public Parser parser;
   public Database database;
   public Download download;
   public SharedFile sharedFile;
   public Gui gui;

   Search()
   {

      getSites = new GetSites(this);

      parser = new Parser();
      database = new Database();

      indexFileName    = new String("index.dat");
      linksFileName    = new String("links.dat");
      searchTempLinksFileName = new String("searchTempLinks.dat");
      downloadUrlListFileName = new String("downloadUrlList.dat");
      storeFileName    = new String("store.html");
      countFileName    = new String("count.dat");
      scFormatFileName = new String("scLinks.dat");
      searchDBase = true;
      searchNet = true;

      siteNames = new String("altavista");
      
      
      linkRange = 0;
      depth = 0;
      
   }
  
   public void setSharedFile(SharedFile sf)
   {
      this.sharedFile = sf;
   }

   public void setDownload(Download d)
   {
      this.download = d;
   }

   public void setGui(Gui d)
   {
      this.gui= d;
   }


   void configure(String sNames,int range,boolean sDBase,boolean sNet,int d)
   {
      siteNames = new String(sNames);
      linkRange = range;

      searchDBase = sDBase;
      searchNet = sNet;
      depth = d;
   }


   String extractName(String url)
   {
        StringTokenizer link = new StringTokenizer(url,"/",false);
        String token = null;
        while(link.hasMoreTokens())
            token = link.nextToken();

        return token;
   }


   void submit(String submitString)
   {

        /** if database search is enabled and net search disabled
          * then only the database is searched
          * if databse search is disabled and net search enabled
          * then only net is searched
          * if both are enabled then net is searched only if
          * database dosent contain the string
          */

        try
        {
            FileOutputStream f = new FileOutputStream(downloadUrlListFileName);
            f.close();

            if(searchDBase&&database.search(indexFileName,linksFileName,downloadUrlListFileName,submitString));
            else if(searchNet)
            {
                //connect to each of the sites and get the downloaded results

                f = new FileOutputStream(storeFileName);
                f.close();

//                File durl = new File(downloadUrlListFileName);
//                durl.delete();
//                File sfile = new File(storeFileName);
//                sfile.delete();


                boolean gotSite = getSites.getSearchResults(siteNames,submitString,storeFileName,linkRange);
                if(gotSite)
                {
                   parser.parse(storeFileName,downloadUrlListFileName,siteNames,true);
                   database.update(indexFileName,linksFileName,downloadUrlListFileName,countFileName,submitString);
                }
                else
                   gui.displayMessage("Not Able to connect to search engine\r\n");
                   
            }                    
        }
        catch(FileNotFoundException fe)
        {
        }
        catch(IOException ie)
        {
            System.out.println(ie);  
        }
   }

   void updateSelect(String token)
   {
        try
        {

            FileOutputStream f = new FileOutputStream(downloadUrlListFileName);
            f.close();
            f = new FileOutputStream(storeFileName);
            f.close();

//            File durl = new File(downloadUrlListFileName);
//            durl.delete();
//            File sfile = new File(storeFileName);
//            sfile.delete();
    
            boolean gotSite = getSites.getSearchResults(siteNames,token,storeFileName,linkRange);
            if(gotSite)
             {
               parser.parse(storeFileName,downloadUrlListFileName,siteNames,true);
               database.update(indexFileName,linksFileName,downloadUrlListFileName,countFileName,token);
            }
            else
               gui.displayMessage("Not Able to connect to search engine\r\n");

        }
        catch(FileNotFoundException fe)
        {
        }
        catch(IOException ie)
        {
        }
   }

   void updateAll()
   {
        try
        {

            RandomAccessFile index = new RandomAccessFile(indexFileName,"r");
            RandomAccessFile newIndex = new RandomAccessFile("tempIndex.dat","rw");

            RandomAccessFile countF = new RandomAccessFile("count.dat","rw");
            countF.writeBytes("0\r\n");
            countF.close();

            FileOutputStream f = new FileOutputStream(linksFileName);
            f.close();

//            File linkF = new File(linksFileName);
//            linkF.delete();

            long filePtr = 0, fileSize = index.length();
            while(filePtr < fileSize)
            {

                f = new FileOutputStream(downloadUrlListFileName);
                f.close();
                f = new FileOutputStream(storeFileName);
                f.close();

//               File durl = new File(downloadUrlListFileName);
//               durl.delete();
//               File sfile = new File(storeFileName);
//               sfile.delete();

                String searchToken = index.readLine();
                index.readLine();  //to skip the indices


                boolean gotSite = getSites.getSearchResults(siteNames,searchToken,storeFileName,linkRange);
                if(gotSite)
                {
                   parser.parse(storeFileName,downloadUrlListFileName,siteNames,true);
                   database.update("tempIndex.dat",linksFileName,downloadUrlListFileName,countFileName,searchToken);
                }
                filePtr = index.getFilePointer();
    
            }
    
            index.close();
            newIndex.close();
            File linkF = new File("tempIndex.dat");
            File indexF = new File(indexFileName);
            indexF.delete();
//check
            linkF.renameTo(indexF);

       }
       catch(FileNotFoundException fe)
       {
            System.out.println(fe);
       }
       catch(IOException fe)
       {
       }

   }

   void construct(String url,String fileName)
   {
       sharedFile.construct(url,fileName,scFormatFileName,depth);   
   }

   void construct()
   {
       sharedFile.construct(searchTempLinksFileName,scFormatFileName,depth);   
   }
   void deleteAll()
   {
       sharedFile.deleteAll(scFormatFileName);
   }

   void deleteLinks(String searchString,int idx[])
   {
       database.deleteLinks(searchString,indexFileName,idx);
   }
    
     
}





