package id.iptek.utms.agent.model;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author jakar
 */
public class FileWriterCacheObj implements Serializable {
    
    private File file;
    private BufferedWriter writer;
    private final AtomicInteger currentWriteRowNum = new AtomicInteger(0);
    private final Object lock = new Object();

    public FileWriterCacheObj() {
    }

    /**
     * @return the file
     */
    public File getFile() {
        return file;
    }

    /**
     * @param file the file to set
     */
    public void setFile(File file) {
        this.file = file;
    }

    /**
     * @return the reader
     */
    public BufferedWriter getWriter() {
        return writer;
    }

    /**
     * @return the currentReadRowNum
     */
    public AtomicInteger getCurrentWriteRowNum() {
        return currentWriteRowNum;
    }
    
    public void pointToFile(File file) throws IOException {
        if(file == null) {
            throw new IllegalArgumentException("File cannot be null!");
        }
        /*
        if(file.exists()) {
            throw new IOException("File exists!");
        }
        if(!file.canWrite()) {
            throw new IOException("File not writable!");
        }*/
        // close old writer
        synchronized (lock) {
            if(this.writer != null) {
                this.writer.flush();
                try {
                    this.writer.close();
                } catch(IOException ignore) {}
                this.writer = null;
            }
            this.file = file;
            this.writer = new BufferedWriter(new FileWriter(this.file));
            this.currentWriteRowNum.set(0);
        }
    }
    
    public void closeWriter() throws IOException {
        if(this.writer != null) {
            synchronized (lock) {
                this.writer.close();
                this.file = null;
                this.currentWriteRowNum.set(0);
            }
        } else {
            // ignore null reader
        }
    }
    
    public BufferedWriter writeLine(String line) throws IOException {
        if(this.writer == null) {
            throw new IOException("Writer cannot be null!");
        }
        synchronized (lock) {
            this.writer.write(line);
            this.writer.newLine();
            this.writer.flush();
        }
        this.currentWriteRowNum.incrementAndGet();
        return this.writer;
    }
    
    public boolean writeLineWithMaxLineNo(int maxLineNo, String line) throws IOException {
        if(this.writer == null) {
            throw new IOException("Writer cannot be null!");
        }
        if(this.currentWriteRowNum.get() < maxLineNo) {
            synchronized (lock) {
                this.writer.write(line);
                this.writer.newLine();
                this.writer.flush();
            }
            this.currentWriteRowNum.incrementAndGet();
            return true;
        }
        return false;
    }
}
