//
//  LKFAufenthaltDiag.swift
//  lkfc
//
//  Created by Alexander Holzer on 20.08.18.
//  Copyright © 2018 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF


public enum LKFAufenthaltDiagFields : UInt32 {
    case diag = 0
    case type = 1
    case pos = 2
    case text = 3
    case grp = 4
}

public class LKFAufenthaltDiag : Identifiable {
    
    public let id = UUID()
    let _aufenthalt: LKFAufenthalt
    let _index: UInt32
    var _line: LKFLine? = nil
    public let diag: String
    init (aufenthalt: LKFAufenthalt, index: UInt32) {
        _aufenthalt = aufenthalt
        _index = index
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:24)
        LibLKF.lkf_auf_get_diagfield (_aufenthalt.ptr, _index, LKFAufenthaltDiagFields.diag.rawValue, cline)
        diag = String(cString:cline).trimmingCharacters(in: .whitespaces)
        cline.deallocate()
    }
    
    public var line : LKFLine
    {
        if _line == nil {
            let cline = UnsafeMutablePointer<CChar>.allocate(capacity:1024)
            LibLKF.lkf_auf_line_diag(_aufenthalt.ptr, _index, cline)
            _line = LKFLine (String(cString:cline))
            cline.deallocate()
        }
        return _line!
    }
    
    public var isMainDiag : Bool {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:24)
        LibLKF.lkf_auf_get_diagfield (_aufenthalt.ptr, _index, LKFAufenthaltDiagFields.type.rawValue, cline)
        let ret = String(cString:cline)=="H"
        cline.deallocate()
        return ret
    }
    
    public var diagPos : String {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:24)
        LibLKF.lkf_auf_get_diagfield (_aufenthalt.ptr, _index, LKFAufenthaltDiagFields.pos.rawValue, cline)
        let ret = String(cString:cline)
        cline.deallocate()
        return ret
    }
    
    public var diagText : String {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:256)
        LibLKF.lkf_auf_get_diagfield (_aufenthalt.ptr, _index, LKFAufenthaltDiagFields.text.rawValue, cline)
        let ret = String(cString:cline, encoding: .ascii)
        cline.deallocate()
        return ret ?? ""
    }
    
    public var gruppe : String {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:256)
        LibLKF.lkf_auf_get_diagfield (_aufenthalt.ptr, _index, LKFAufenthaltDiagFields.grp.rawValue, cline)
        let ret = String(cString:cline, encoding: .ascii)
        cline.deallocate()
        return ret ?? ""
    }
}
