//
//  LKFAufenthaltCategory.swift
//  lkfc
//
//  Created by Alexander Holzer on 10.09.24.
//  Copyright © 2024 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFAufenthaltCategory : Identifiable {
    
    public let id = UUID()
    
    public let code: String
    public let comment: String?
    public let line: String
    
    
    
    
    init (line: String) {
        self.line = line
        let parts = line.components(separatedBy: ";")
        if parts.count < 2 {
            code = line
            comment = nil
        }
        else {
            code = parts[0];
            comment = parts[1];
        }
        
    }
    
    public static func getCategories (aufenthalt: LKFAufenthalt) -> [LKFAufenthaltCategory]? {
        
        let count = LibLKF.lkf_auf_getCategoryCount(aufenthalt.ptr)
 
        if count == 0 {
            return nil
        }
        
        var categories = [LKFAufenthaltCategory]()
        let buf = UnsafeMutablePointer<CChar>.allocate(capacity: 512)
        for i in 0..<count {
            LibLKF.lkf_auf_getCategory (aufenthalt.ptr, i, buf, 512)
            categories.append(LKFAufenthaltCategory(line: String(cString: buf)))
        }
        buf.deallocate()
        return categories
    }
 
    public static var descriptions : [String:String] = [
        "NTA":    "Null-Tage-Aufenthalt",
        "ETA":    "Eintage-Aufenthalt",
        "TKLMEL": "Tagesklinische MEL",
        "MELMEL": "MEL-MEL-Optimierung (stationär)",
        "0ENT":   "Entlassungsart S/T und NTA"
    ]
    
    public static func description (for code: String) -> String? {
        return descriptions[code]
    }
}

