//
//  LKFMetaDataDiag.swift
//  LKF
//
//  Created by Alexander Holzer on 14.01.22.
//  Copyright © 2022 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFMetaDataDiag : LKFMetaDataItem, Identifiable {
    
    public enum Fields : UInt32 {
        case Diag =  1
        case Group = 4
        case Text = 20
        case DGR = 7
    }
    
    public let context : LKFContext
    public let diagIndex: UInt32
    public let id: String
    
    public lazy var group : LKFMetaDataGroup? = { () -> LKFMetaDataGroup? in
        let g = GroupName
        if g.count > 0 {
            return try? LKFMetaDataGroup (context: context, group: g)
        }
        return nil
    }()
    
    public init (context: LKFContext, diagIndex: UInt32) {
        self.context = context
        self.diagIndex = diagIndex
        self.id = LKFMetaDataDiag.utf8value(for: .Diag, context: context, diagIndex: diagIndex)
    }
    
    public static func utf8value (for field:LKFMetaDataDiag.Fields, context: LKFContext, diagIndex: UInt32) -> String {
        if let ptr = context.ptr {
            let lineBuffer = UnsafeMutablePointer<CChar>.allocate(capacity:256)
            defer {
                lineBuffer.deallocate()
            }
            LibLKF.lkf_stamm_getDiagField (ptr, diagIndex, field.rawValue, lineBuffer, 256)
            return String(cString: lineBuffer).trimmingCharacters(in: .whitespaces)
        }
        return ""
    }
    
    public func value (for field:LKFMetaDataDiag.Fields, encodings:[String.Encoding]? = [.utf8, .ascii, .isoLatin1, .windowsCP1252]) -> String {
        if let ptr = context.ptr {
            let lineBuffer = UnsafeMutablePointer<CChar>.allocate(capacity:256)
            defer {
                lineBuffer.deallocate()
            }
            LibLKF.lkf_stamm_getDiagField (ptr, diagIndex, field.rawValue, lineBuffer, 256)
            
            if let encodings = encodings {
                for e in encodings {
                    if let r = String(cString: lineBuffer, encoding: e) {
                        return r.trimmingCharacters(in: .whitespaces)
                    }
                }
            }
            return String(cString: lineBuffer).trimmingCharacters(in: .whitespaces)        
        }
        return ""
     }

    
    public var Diag : String {
        return id
    }
    
    public var GroupName : String {
        return value (for: .Group)
    }

    public var Text: String {
        return value (for: .Text)
    }
    
    public var DGR: String {
        return value (for: .DGR)
    }
    
    override public var itemKey: String { return self.Diag }
    override public var itemDescription: String { return self.Text }
    
    override public func getLowercasedSearchTexts() -> [String] {
        var arr = [self.Diag.lowercased(), self.Text.lowercased()]
        let groupName = self.GroupName
        
        if groupName.count > 0 {
            arr.append(groupName.lowercased())
        }
        return arr
    }

}
