package fyloz.trial.ColorRecipesExplorer.factory.files;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import fyloz.trial.ColorRecipesExplorer.core.io.FileHandler;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class PdfFactory {

    protected Document document;

    private String fileName;
    private FileHandler fileHandler;

    public PdfFactory(String fileName) {
        this(fileName, FileHandler.FileContext.OTHERS);
    }

    public PdfFactory(String fileName, FileHandler.FileContext context) {
        this.fileName = fileName;

        fileHandler = new FileHandler(fileName, context, FileHandler.FileExtension.PDF);
    }

    public PdfFactory open() throws FileNotFoundException, DocumentException {
        document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(fileHandler.getPath().toString()));

        document.open();

        return this;
    }

    public void write() {
        document.close();
    }

    public FileHandler getFileHandler() {
        return fileHandler;
    }
}
