//
//  LKFScoringDetailsDIAG.swift
//  LKF
//
//  Created by Alexander Holzer on 21.03.22.
//  Copyright © 2022 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFScoringDetailsDIAG : LKFScoringDetailsGroup {

    public let diag: LKFAufenthaltDiag
    public let diagIndex: UInt32

    init (aufenthalt:LKFAufenthalt, diagIndex: UInt32, mainGroupIndex: Int)  {
        let lineBuffer = UnsafeMutablePointer<CChar>.allocate(capacity:2048)
        let aufenthaltPtr = aufenthalt.ptr
        LibLKF.lkf_scoringdetails_getDIAGInfo (aufenthaltPtr, diagIndex, lineBuffer, 2048)
        let parts = String.utf8String(cString: lineBuffer).components(separatedBy: "|")

        self.diagIndex = diagIndex
        self.diag = aufenthalt.diagAt(index: diagIndex)
        super.init(aufenthalt: aufenthalt, parts:parts, mainGroupIndex: mainGroupIndex)
        let ch = LibLKF.lkf_scoringdetails_getDIAGCommentCount(aufenthaltPtr, diagIndex)
        if ch > 0 {
            var comments = [String]()
            for i in 0..<ch {
                LibLKF.lkf_scoringdetails_getDIAGComment(aufenthaltPtr, diagIndex, i, lineBuffer, 1024)
                comments.append(String.utf8String(cString: lineBuffer))
            }
            self.comments = comments
        }
        else {
            comments = nil
        }
        lineBuffer.deallocate()
    }
    
    public override var traceIdent: String {
        return "\(diag.diag)"
    }

    override public var index : UInt32 {
        return diagIndex
    }
}
