package ryhma57.backend;

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 java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import ryhma57.references.Reference;

public class Storage {

    private ObjectOutputStream out;
    private ObjectInputStream in;
    private PrintWriter writer;
    private ReferenceList list;
    private Validator validator;

    public Storage() {
        list = new ReferenceList();
        validator = new Validator();
        getPreviousReferenceList();
    }

    public String storeNewReference(Reference reference) {
        String message = validator.validateReference(reference);
        if (message != null) {
            return message;
        }
        if (!validator.hasField(reference, BibtexReferenceField.ID)) {
            String newID = reference.getField(BibtexReferenceField.AUTHOR).charAt(0) + reference.getField(BibtexReferenceField.YEAR);
            reference.setField(BibtexReferenceField.ID, newID);
        }
        if (validator.hasIDUsed(reference.getID())) {
            for (char i = 'a'; i <= 'z'; i++) {
                String newID = reference.getID() + i;
                if (!validator.hasIDUsed(newID)) {
                    reference.setField(BibtexReferenceField.ID, newID);
                    break;
                }
            }
        }
        try {
            out = new ObjectOutputStream(new FileOutputStream("db.txt"));
            list.addReference(reference);
            out.writeObject(list);
            out.flush();
        } catch (IOException ex) {
            Logger.getLogger(Storage.class.getName()).log(Level.SEVERE, null, ex);
            return "exception happened";
        }
        validator.setReferenceList(list);
        return null;
    }

    public void removeReference(Reference reference) {
        try {
            writer = new PrintWriter("db.txt");
            writer.print("");
            writer.close();
            list.deleteReference(reference);
            out = new ObjectOutputStream(new FileOutputStream("db.txt"));
            out.writeObject(list);
            out.flush();
        } catch (IOException ex) {
            Logger.getLogger(Storage.class.getName()).log(Level.SEVERE, null, ex);
        }
        validator.setReferenceList(list);
    }

    public void generateBibTex() {
        try {
            writer = new PrintWriter("references.bib", "UTF-8");
            writer.print(list.toBibTex());
            writer.close();
        } catch (FileNotFoundException | UnsupportedEncodingException ex) {
            Logger.getLogger(StorageJson.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void getPreviousReferenceList() {
        try {
            in = new ObjectInputStream(new FileInputStream("db.txt"));
            list = (ReferenceList) in.readObject();
        } catch (FileNotFoundException ex) {
            /* Do nothing if the file doesn't exist */
        } catch (IOException | ClassNotFoundException ex) {
            Logger.getLogger(Storage.class.getName()).log(Level.SEVERE, null, ex);
        }
        validator.setReferenceList(list);
    }

    /*TODO This propably should be refactored */
    public ReferenceList getReferenceList() {
        return list;
    }
    
    public static void removeTmpFiles() {
        try {
            Files.delete(Paths.get("references.bib"));
        } catch (IOException ex) {
        }
        try {
            Files.delete(Paths.get("db.txt"));
        } catch (IOException ex) {
        }
    }
}

@@ -1614,307 +1614,63 @@
     
-try %7B%0A            out = new ObjectOutputStream(new FileOutputStream(%22db.txt%22));%0A            list.addReference(reference);%0A            out.writeObject(list);%0A            out.flush();%0A        %7D catch (IOException ex) %7B%0A            Logger.getLogger(Storage.class.getName()).log(Level.SEVERE, null, ex);
+list.addReference(reference);%0A        if (!writeDB()) %7B
 %0A   
@@ -1686,17 +1686,17 @@
 return %22
-e
+E
 xception
@@ -1854,164 +1854,158 @@
     
-try %7B%0A            writer = new PrintWriter(%22db.txt%22);%0A            writer.print(%22%22
+list.deleteReference(reference);%0A        writeDB();%0A        validator.setReferenceList(list
 );%0A    
+%7D%0A
     
+%0A
     
-writer.close();%0A            list.deleteReference(reference);
+private boolean writeDB() %7B%0A        try %7B
 %0A   
@@ -2131,24 +2131,49 @@
 ut.flush();%0A
+            return true;%0A
         %7D ca
@@ -2290,57 +2290,42 @@
     
-%7D%0A
     
-    validator.setReferenceList(list);%0A    %7D
+return false;%0A        %7D%0A    %7D 
 %0A%0A  

refactor some code in storage