/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.jmap;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import fr.tlse.irap.pt.client.controller.common.Formatter;
import fr.tlse.irap.fit.FitPoint;

/**
 * Save/read click point list.
 * 
 * @author frup58596
 */
public class ClickPointsFileHandler {
    
    /**
     * Save clicked points.
     * 
     * @param clickPoints the click point list to save.
     * @param file the file to save the click point list.
     */
    static public void writeClickPointsFile(List<FitPoint> clickPoints, File file) {
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(file);
            bw = new BufferedWriter(fw);
            for (int i = 0; i < clickPoints.size(); i++) {
                bw.write(Formatter.getSdf().format(new Date(clickPoints.get(i).getTime())) + "\t" + Formatter.getFormatter1D().format(clickPoints.get(i).getElongation()) + "\n");
            }
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Read previously saved click point list.
     * 
     * @param file
     * @return
     */
    static public List<FitPoint> readClickPointsFile(File file) {
        List<FitPoint> fitPoints = null;
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fitPoints = new ArrayList<FitPoint>();
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            String line = br.readLine();
            while (line != null) {
                String clickPoint[] = line.split("\t");
                fitPoints.add(new FitPoint(Formatter.getSdf().parse(clickPoint[0]).getTime(), Double.parseDouble(clickPoint[1])));
                line = br.readLine();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fitPoints;
    }
}
