package ru.upos;

import java.io.*;

/**
 * Эмуляция печати чека на принтере
 */
public class PrinterEmulation {

    private static final String fileName = "receipt";

    private File file;
    private FileOutputStream buf;

    public PrinterEmulation() {
        file = null;
    }

    public boolean open(boolean isCopyReceipt) throws IOException {
        if (file == null) {
            file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            buf = new FileOutputStream(file);
        }
        //if (isCopyReceipt) {
            // TODO: add receipt copy functionality
        //}
        return file.exists();
    }

    public void write(byte[] data) throws IOException {
        String str = new String(data, "cp866");

        // TODO: change algorithm for remove bytes '\0' from data
        str = str.replaceAll("\0", "");
        buf.write(str.getBytes()); // print utf8 string
    }

    public void close() throws IOException {
        buf.close();
    }
}
