package controller;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JFrame;
import view.ThePanel;
import model.Calcul;

/**
 * Classe de test contenant la méthode main du projet
 *
 */

public class Test {
	public static void main(String[] args) throws IOException
	{
		//Chargement d'un fichier de coordonnées des bulles
		File file = new File("Coordonnees_mm/norma_N5_tau4_dt2_delai820_000006.txt");
		//Tableau représentant les données
		double[][] data = new double[Test.countLines(file)][3];
		//Lecture du fichier
	    FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);

        int numLine = 0 ;
        for (String line = br.readLine(); line != null; line = br.readLine())
        {
        	int numCol = 0 ;
        	
           	for( String s : line.split(" "))
           	{
           		if( !s.isEmpty() && numCol < 3)
           		{
           			data[numLine][numCol] = Double.parseDouble(s);
           			numCol++ ;
           		}
           	}
        	numLine++ ;
        }

        br.close();
        fr.close();
        
        for ( double[] d : data)
        {
        	for( double v: d)
        	{
        		System.out.print(v+" |||||| ");
        	}
        	System.out.println();
        }
        
        
        Calcul c = new Calcul(data, Test.countLines(file)/5, true);
        c.setEpsilon(0.01);
        c.calculateClusters();
        
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocation(50,50);
        f.add(new ThePanel(data, c));
        f.setSize(550,530);

        f.setVisible(true);
	}
	
	//méthode pour compter le nombre de lignes dans un fichier
	public static int countLines(File file) throws IOException 
	{
	    InputStream is = new BufferedInputStream(new FileInputStream(file));
	    try {
	        byte[] c = new byte[1024];
	        int count = 0;
	        int readChars = 0;
	        boolean empty = true;
	        while ((readChars = is.read(c)) != -1) {
	            empty = false;
	            for (int i = 0; i < readChars; ++i) {
	                if (c[i] == '\n') {
	                    ++count;
	                }
	            }
	        }
	        return (count == 0 && !empty) ? 1 : count;
	    } finally {
	        is.close();
	    }
	}
}
