//
//  LKFOutputFile.swift
//  lkfc
//
//  Created by Alexander Holzer on 07.06.18.
//  Copyright © 2018 Alexander Holzer. All rights reserved.
//

import Foundation

public class LKFOutputFile {
    var _context: LKFContext
    
    init(context: LKFContext) {
        self._context = context
    }
    
    public var context: LKFContext {
        get {
            return _context
        }
    }
    
    public func getOutput() throws -> [String] {
        throw LKFError ("getOutput not implemented for base class LKFOutputFile")
    }
    
    public var defaultfileName : String {
        return ""
    }
    
    public func getData (using encoding: String.Encoding = .utf8) throws -> Data? {
        let output = try getOutput()
        let outputString = output.joined(separator: "\n")
        return outputString.data(using: encoding)
    }

    
    public func write (file: URL) throws {        
        if let data = try getData() {
            try data.write(to: file, options: [])
        }
        else {
            throw LKFError(message: "cannot create data")
        }
    }

}
