/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.pwr.nlptools.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.comparator.SizeFileComparator;
import pl.edu.pwr.nlptools.NLPTool;
import pl.edu.pwr.nlptools.NLPToolException;
import pl.edu.pwr.nlptools.Status;

/**
 *
 * @author www_zsk
 */
public class Test {
    
    public static final String TEST_PATH = "/test/";

		private File[] listOfFiles;    
    public Test()
    {
       File folder = new File(this.getClass().getResource(TEST_PATH).getPath());
       listOfFiles = folder.listFiles();
       Arrays.sort(listOfFiles,SizeFileComparator.SIZE_COMPARATOR);

    }
    
    public String load() throws NLPToolException
    {  String text="";
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(TEST_PATH+"/16.txt"), "UTF-8"));
            String line;
            while((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.length() > 0) {
                    text=text+line;
                }
            }
        } catch (IOException ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
                throw new NLPToolException("Failed reading the file");
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        return text;
    }  

    public List<String> test(int i) throws NLPToolException {
       List<String> res=onetest1(i);
       return res;
    }
    
    
    static class T {
		long startTime;

		public T() {
			reset();
		}

		public void reset() {
			startTime = System.currentTimeMillis();
		}

		public double end() {
			long stopTime = System.currentTimeMillis();
			long elapsedTime = stopTime - startTime;
			return (double) elapsedTime / 1000;

		}
	}

	private String readFile(String path, boolean newline) throws IOException {
		String everything;
		BufferedReader br = new BufferedReader(new FileReader(path));
		try {
			StringBuilder sb = new StringBuilder();
			String line = br.readLine();

			while (line != null) {
				if (!line.equalsIgnoreCase(""))
					sb.append(line);
				if (newline)
					sb.append("\n");
				else
					sb.append(" ");

				line = br.readLine();
			}
			everything = sb.toString();
		} finally {
			br.close();
		}
		return everything;
	}
        
        private String onetest(File file) throws NLPToolException {
		String tekst = "";
		T t = new T();
		double czas = 0;
		try {
			String tekst1 = readFile(file.getAbsolutePath(), true);

			tekst="<td>"+tekst1.length()+"</td>";
		       	
	               NLPTool wcrft=new NLPTool("wcrft2");
                       NLPTool wsd=new NLPTool("wsd");
                       NLPTool chunker=new NLPTool("chunker");
                       czas = t.end();tekst=tekst+"<td>"+czas+"</td>";t.reset();
                       String id=wcrft.saveText(tekst1);
                       
                       id=wcrft.processFile(id);                       
		       czas = t.end();tekst=tekst+"<td>"+czas+"</td>";t.reset();
                       if (tekst1.length()<5000)
                       { id=wsd.processFile(id);	
		       czas = t.end();  tekst=tekst+"<td>"+czas+"</td>";
                       }
                       else tekst=tekst+"<td>&nbsp;</td>";
                       
                        
                       //chunker
                       id=wcrft.saveText(tekst1);
                       id=wcrft.processFile(id);  
                       t.reset();
                       id=chunker.processFile(id);
                       czas = t.end();  tekst=tekst+"<td>"+czas+"</td>";
                       //
                       
                       
                      t.reset();
		       wsd.readFile(id);
                       czas = t.end();tekst=tekst+"<td>"+czas+"</td>";t.reset();
		} catch (IOException e) {
			
			e.printStackTrace();
		}

		

		return tekst;
	}
        
        
        public List<String> paralleltest(int ID,int count) throws NLPToolException {
		ArrayList<String> tekst=new ArrayList<>();
		T t = new T();
		double czas = 0;
                if (ID<1)
                { tekst.add("Size");
                  tekst.add("Upload");
                  
                  tekst.add("WCRFT2");
                  tekst.add("WSD");
                  
                  tekst.add("CHUNKER");
                  tekst.add("Download");
                  
                }   
                else    
		try {     File file=listOfFiles[ID-1];
			String tekst1 = readFile(file.getAbsolutePath(), true);

			tekst.add(""+tekst1.length());
		       	
	               NLPTool wcrft=new NLPTool("wcrft2");
                       NLPTool wsd=new NLPTool("wsd");
                       czas = t.end();tekst.add(""+czas);
                       
                       
                       String[] taskids=new String[count];
                       String[] ids=new String[count];
                       
                       boolean[] taskfinished=new boolean[count];
                       for (int i=0;i<count;i++)
                       {        taskfinished[i]=false;
                               ids[i]=wcrft.saveText(tekst1);
                       
                       }
                       t.reset();
                       for (int i=0;i<count;i++)
                       {  
                          taskids[i]=wcrft.startTask(ids[i]);
                       }
                        boolean rep = true;
                       while (rep)
                       { try {
                           Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                    }
                       rep=false;
                       for (int i=0;i<count;i++)
                           if (!taskfinished[i])
                       {  
                          Status st=wcrft.check(taskids[i]);
                          
                          if (st.isReady())
                          {  taskfinished[i]=true;
                              taskids[i]=st.getValue();
                              
                          }
                          else
                              rep=true;
                          
                       }
                       }
		       czas = t.end();tekst.add(""+czas/count);t.reset();
                       /*if (tekst1.length()<5000)
                       { id=wsd.processFile(id);	
		       czas = t.end();  tekst.add(""+czas);
                       }
                       else*/ tekst.add("");
                       
                      t.reset();
		       //wsd.readFile(id);
                       czas = t.end();tekst.add(""+czas);t.reset();
		} catch (IOException e) {
			
			e.printStackTrace();
		}

		

		return tekst;
	}
        
        private List<String> onetest1(int ID) throws NLPToolException {
		ArrayList<String> tekst=new ArrayList<>();
		T t = new T();
		double czas = 0;
                if (ID<1)
                { tekst.add("Size");
                  tekst.add("Upload");
                  
                  tekst.add("WCRFT2");
                  tekst.add("WSD");
                  
                  tekst.add("Download");
                  
                }   
                else    
		try {     File file=listOfFiles[ID-1];
			String tekst1 = readFile(file.getAbsolutePath(), true);

			tekst.add(""+tekst1.length());
		       	
	               NLPTool wcrft=new NLPTool("wcrft2");
                       NLPTool wsd=new NLPTool("wsd");
                       czas = t.end();tekst.add(""+czas);t.reset();
                       String id=wcrft.saveText(tekst1);
                       
                       id=wcrft.processFile(id);                       
		       czas = t.end();tekst.add(""+czas);t.reset();
                       if (tekst1.length()<5000)
                       { id=wsd.processFile(id);	
		       czas = t.end();  tekst.add(""+czas);
                       }
                       else tekst.add("");
                       
                      t.reset();
		       wsd.readFile(id);
                       czas = t.end();tekst.add(""+czas);t.reset();
		} catch (IOException e) {
			
			e.printStackTrace();
		}

		

		return tekst;
	}

    public String testall() throws NLPToolException  {
		String out="<html><table border=\"1\"><tr><td>Rozmiar</td><td>Upload</td><td>WCRFT2</td><td>WSD</td><td>Chunker</td><td>Download</td></tr>";

                
		for (int i = 0; i < listOfFiles.length; i++) {
                    out=out+"<tr>"+ onetest(listOfFiles[i])+"</tr>";
			
                                               
                                       
				
			
		}
                out=out+"</table></body></html>";
          return out;
	}
}
