//
//  LKFNoMetaDataContext.swift
//  LKF
//
//  Created by Alexander Holzer on 28.02.24.
//  Copyright © 2024 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFNoMetaDataContext : LKFContext {
    public init(year: UInt32, path: String? = nil) throws {
        try super.init (model:year, path:path, stammProvider:nil)
    }
    
    public override func initialize(model: UInt32, path: String?) throws {
        let errorString = UnsafeMutablePointer<CChar>.allocate(capacity:1024)
        
        if let path {
            let unsafePath = (path as NSString).utf8String
            ptr = LibLKF.lkf_alloc_context(0, unsafePath, errorString)
            LibLKF.lkf_set_statjahr (ptr!, model, unsafePath, errorString)
        }
        else {
            ptr = LibLKF.lkf_alloc_context(0, UnsafeMutablePointer<CChar>(nil), errorString)
            LibLKF.lkf_set_statjahr (ptr!, model, UnsafeMutablePointer<CChar>(nil), errorString)
        }
        
        let text = String(cString: errorString)
        errorString.deallocate()
        
        if !text.isEmpty {
            throw LKFError(message:text)
        }
        
        LibLKF.lkf_set_contextflags(ptr, flags)
        if let period = _period {
            LibLKF.lkf_set_contextparametersFromString (ptr!, (period as NSString).utf8String, flags)
        }
    }
}
