//
//  LKFAufenthaltAkWarn.swift
//  LKF
//
//  Created by Alexander Holzer on 29.01.24.
//  Copyright © 2024 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFAufenthaltAkWarn : Identifiable {
    
    let _aufenthalt: LKFAufenthalt
    let _index: UInt32
    public let id = UUID()
    
    public let rawLine : String
    public var _lkfLine: LKFLine?
    
    public var line: LKFLine {
        if let _lkfLine {
            return _lkfLine
        }
        let lkfLine = LKFLine (rawLine)
        self._lkfLine = lkfLine
        return lkfLine
    }
        
    public var error : String {
        line.valueAt(index: 6)
    }
    
    public var value: String {
        line.valueAt(index: 5)
    }
    
    public var comment: String {
        line.valueAt(index: 7)
    }
    
    init (aufenthalt: LKFAufenthalt, index: UInt32) {
        _aufenthalt = aufenthalt
        _index = index
        
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:1024)
        LibLKF.lkf_auf_line_akwarn2 (_aufenthalt.ptr, _index, cline, nil)
        rawLine = String(cString:cline).trimmingCharacters(in: .whitespaces)
        cline.deallocate()
    }
    
    public init (aufenthalt: LKFAufenthalt, errorCode: String, value: String, comment: String) {
        _index = 9999
        let line = LKFLine (type: "X06", aufenthalt: aufenthalt, content: "\(value)|\(errorCode)|\(comment)")
        rawLine = line.line
        self._lkfLine = line
        self._aufenthalt = aufenthalt
    }
}
