//
//  LKFContextDataPerType.swift
//  lkfc
//
//  Created by Alexander Holzer on 06.04.23.
//  Copyright © 2023 Alexander Holzer. All rights reserved.
//

import Foundation

public class LKFContextDataRecordTypes {
    
    public let lkfContext: LKFContext
    public let recordTypes : LKFRecordTypes?
    public let dict : [String:LKFContextDataRecordType]
    public let arr : [LKFContextDataRecordType]
    
    
    
    public init (lkfContext: LKFContext, lkfAufenthalt: LKFAufenthalt? = nil, all: Bool = false) {
        self.lkfContext = lkfContext
        self.recordTypes = lkfContext.recordTypes
        
        var dict = [String:LKFContextDataRecordType]()
        
        let lineBuffer = UnsafeMutablePointer<CChar>.allocate(capacity:1024)
        let identBuffer = UnsafeMutablePointer<CChar>.allocate(capacity:128)
        
        defer {
            lineBuffer.deallocate()
            identBuffer.deallocate()
        }
        
        if let lkfAufenthalt {
            
            let lineCount = lkfAufenthalt.mbdsLineCount

            for i in 0..<lineCount {
                let l = lkfAufenthalt.mbdsLine (at: i, lineBuffer: lineBuffer, identBuffer: identBuffer)
                let ident = String(cString: identBuffer)
                let dl = LKFContextDataRecordTypeAufenthaltLine(lineIdent: ident, aufenthalt: lkfAufenthalt, line: l)
                if let rt = dict[dl.type] {
                    rt.add(line: dl)
                }
                else {
                    dict[dl.type] = LKFContextDataRecordType(type: dl.type, line: dl)
                }
            }
            
            let intLineCount = lkfAufenthalt.intLineCount

            for i in 0..<intLineCount {
                let l = lkfAufenthalt.intLine (at: i, lineBuffer: lineBuffer, identBuffer: identBuffer)
                let ident = String(cString: identBuffer)
                let dl = LKFContextDataRecordTypeAufenthaltLine(lineIdent: ident, aufenthalt: lkfAufenthalt, line: l)
                if let rt = dict[dl.type] {
                    rt.add(line: dl)
                }
                else {
                    dict[dl.type] = LKFContextDataRecordType(type: dl.type, line: dl)
                }
            }
        }
        else {
            let addLineCount = lkfContext.getAddLineCount
            
            for i in 0..<addLineCount {
                let l = lkfContext.getAddLine(at: i, lineBuffer: lineBuffer, identBuffer: identBuffer)
                let ident = String(cString: identBuffer)
                let dl = LKFContextDataRecordTypeLine(lineIdent: ident, line: l)
                if let rt = dict[dl.type] {
                    rt.add(line: dl)
                }
                else {
                    dict[dl.type] = LKFContextDataRecordType(type: dl.type, line: dl)
                }
            }

            if all {
                let aufenthaltCount = lkfContext.getAufenthaltCount()
                for i in 0..<aufenthaltCount {
                    let lkfAufenthalt = lkfContext[i]

                    let lineCount = lkfAufenthalt.mbdsLineCount

                    for i in 0..<lineCount {
                        let l = lkfAufenthalt.mbdsLine (at: i, lineBuffer: lineBuffer, identBuffer: identBuffer)
                        let ident = String(cString: identBuffer)
                        let dl = LKFContextDataRecordTypeAufenthaltLine(lineIdent: ident, aufenthalt: lkfAufenthalt, line: l)
                        if let rt = dict[dl.type] {
                            rt.add(line: dl)
                        }
                        else {
                            dict[dl.type] = LKFContextDataRecordType(type: dl.type, line: dl)
                        }
                    }
                    
                    let identCount = lkfAufenthalt.intLineCount

                    for i in 0..<identCount {
                        let l = lkfAufenthalt.intLine (at: i, lineBuffer: lineBuffer, identBuffer: identBuffer)
                        let ident = String(cString: identBuffer)
                        let dl = LKFContextDataRecordTypeAufenthaltLine(lineIdent: ident, aufenthalt: lkfAufenthalt, line: l)
                        if let rt = dict[dl.type] {
                            rt.add(line: dl)
                        }
                        else {
                            dict[dl.type] = LKFContextDataRecordType(type: dl.type, line: dl)
                        }
                    }
                }
            }
        }
        
        self.dict = dict
        self.arr = dict.values.sorted(by: { $0.type < $1.type })
    }
}
