package save;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import signals.AdaptSignals;
import signals.Signals;
import tools.Arrow;

import main.ChronoWindow;
import main.Main;

public class Slot {

	public static void save(File saveDir){
        ObjectOutputStream oos;
        try {
        	oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(saveDir)));
        	
        	oos.writeObject(ChronoWindow.chrono.get(ChronoWindow.active).signals.size());
        	for (int i=0; i<ChronoWindow.chrono.get(ChronoWindow.active).signals.size(); i++){
        		oos.writeObject(ChronoWindow.chrono.get(ChronoWindow.active).signals.get(i));
        	}
        	oos.writeObject(ChronoWindow.chrono.get(ChronoWindow.active).nbrTimeLapse);
        	oos.writeObject(ChronoWindow.chrono.get(ChronoWindow.active).timeLapse);
        	oos.writeObject(ChronoWindow.chrono.get(ChronoWindow.active).displayBackLines);
        	oos.writeObject(Main.version);
        	// v2.2
        	oos.writeObject(ChronoWindow.chrono.get(ChronoWindow.active).antialiasing);
        	oos.writeObject(ChronoWindow.chrono.get(ChronoWindow.active).arrows.size());
        	for (int i=0; i<ChronoWindow.chrono.get(ChronoWindow.active).arrows.size(); i++){
        		oos.writeObject(ChronoWindow.chrono.get(ChronoWindow.active).arrows.get(i));
        	}
        	oos.close();
        	
        	
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }     	
	}
	
	public static void load(File openDir){
		ObjectInputStream ois;
		int size=0;
		try {
	    	ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(openDir)));
	        
	    	try {
	    		size=(Integer)ois.readObject();
	    		for (int i=0; i<size; i++){
	    			//ChronoWindow.chrono.get(ChronoWindow.active).signals.add((Signals)ois.readObject());
	    			ChronoWindow.chrono.get(ChronoWindow.active).signals.add(new AdaptSignals((Signals)ois.readObject()));
	        	}
	    		ChronoWindow.chrono.get(ChronoWindow.active).nbrTimeLapse=(Integer)ois.readObject();
	    		ChronoWindow.chrono.get(ChronoWindow.active).timeLapse=(int[][])ois.readObject();
	    		ChronoWindow.chrono.get(ChronoWindow.active).displayBackLines=(Boolean) ois.readObject();
	    		String version = (String) ois.readObject(); // Unused for now on.
	    		// v2.2
	    		ChronoWindow.chrono.get(ChronoWindow.active).antialiasing=(Boolean) ois.readObject();
	    		int arrowSize =(Integer)ois.readObject();
	        	for (int i=0; i<arrowSize; i++){
	        		ChronoWindow.chrono.get(ChronoWindow.active).arrows.add((Arrow)ois.readObject());
	        	}
	        	
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			ois.close();
		} catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  
    	
	}
}
