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

import Foundation
@_implementationOnly import LibLKF 

public enum LKFAufenthaltMELFields : UInt32 {
    case melzu = 0
    case mel = 23
    case melanz = 24
    case seite = 7
    case uhrzeit = 16
    case datum = 13
    case text = 20
    case statgrp = 21
    case ambgrp = 22
    case kaleist = 14
    case fcleist = 15
    case abtIndex = 85
    case abtPos = 86
    case grpknt = 4
    case knoten = 5
}

public class LKFAufenthaltMEL : Identifiable {
    
    let _aufenthalt: LKFAufenthalt
    let _index: UInt32
    var _line: LKFLine? = nil
    public let MEL : String
    public let id = UUID()
    
    init (aufenthalt: LKFAufenthalt, index: UInt32) {
        _aufenthalt = aufenthalt
        _index = index
        
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:80)
        LibLKF.lkf_auf_get_mel_value (_aufenthalt.ptr, _index, LKFAufenthaltMELFields.mel.rawValue, 80, cline)
        MEL = 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_mel (_aufenthalt.ptr, _index, cline)
            _line = LKFLine (String(cString:cline))
            cline.deallocate()
        }
        return _line!
    }
    
    public func getFieldByIndex (field: UInt32) -> String {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:80)
        LibLKF.lkf_auf_get_mel_value (_aufenthalt.ptr, _index, field, 80, cline)
        let f = String(cString:cline)
        cline.deallocate()
        return f
    }

    public func getField (field: LKFAufenthaltMELFields) -> String {
        return getFieldByIndex(field: field.rawValue)
    }

    public var MELANZString : String {
        return getField(field: .melanz)
    }
    
    public var MELZU : String {
        return getField(field: .melzu)
    }

    public var MELANZ : Int? {
        return Int(getField(field: .melanz).trimmingCharacters(in: .whitespaces))
    }

    public var MELSeite : String {
        return getField(field: .seite)
    }
    
    public var Datum : String {
        return getField(field: .datum)
    }
    
    public var MELDate : Date? {
        return LKFConvert.toDate(string8: Datum)
    }
    
    public var Uhrzeit : String {
        return getField(field: .uhrzeit)
    }
    
    public var abtIndex: Int? {
        if let abtIndex = Int(getField(field: .abtIndex)), abtIndex >= 0 {
            return abtIndex
        }
        return nil
    }
    
    public var abtPos : String? {
        let abtPos = getField(field: .abtPos)
        return abtPos.count > 0 ? abtPos : nil
    }
    
    public var Text : String {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:128)
        LibLKF.lkf_auf_get_mel_value (_aufenthalt.ptr, _index, LKFAufenthaltMELFields.text.rawValue, 110, cline)
        let ret = String(cString:cline, encoding: .ascii)
        cline.deallocate()
        return ret ?? ""
    }

    public var StatGrp : String {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:128)
        LibLKF.lkf_auf_get_mel_value (_aufenthalt.ptr, _index, LKFAufenthaltMELFields.statgrp.rawValue, 110, cline)
        let ret = String(cString:cline, encoding: .ascii)
        cline.deallocate()
        return ret ?? ""
    }

    public var AmbGrp : String {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:128)
        LibLKF.lkf_auf_get_mel_value (_aufenthalt.ptr, _index, LKFAufenthaltMELFields.ambgrp.rawValue, 110, cline)
        let ret = String(cString:cline, encoding: .ascii)
        cline.deallocate()
        return ret ?? ""
    }
    
    public var GruppeKnoten : String {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:128)
        LibLKF.lkf_auf_get_mel_value (_aufenthalt.ptr, _index, LKFAufenthaltMELFields.grpknt.rawValue, 110, cline)
        let ret = String(cString:cline, encoding: .ascii)
        cline.deallocate()
        return ret ?? ""
    }
    
    public var Knoten : String {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:128)
        LibLKF.lkf_auf_get_mel_value (_aufenthalt.ptr, _index, LKFAufenthaltMELFields.knoten.rawValue, 110, cline)
        let ret = String(cString:cline, encoding: .ascii)
        cline.deallocate()
        return ret ?? ""
    }

    
    public var FCLeist : String {
        return getField(field: .fcleist)
    }
    
    public var KALeist : String {
        return getField(field: .kaleist)
    }
    
    public var Leist : String {
        let kaleist = KALeist
        if kaleist.count > 0 {
            return kaleist
        }
        return FCLeist
    }

    
    public var aufenthalt : LKFAufenthalt {
        return _aufenthalt;
    }
    
    public var index : UInt32 {
        return _index;
    }
    
    public func scoringData() -> LKFAufenthaltMELScoringData {
        return LKFAufenthaltMELScoringData(mel: self)
    }

    public func dateTimeString () -> String? {
        if let d = MELDate {
            return LKFConvert.dateTimeString(date: d, time: LKFTime(Uhrzeit)) ?? ""
        }
        return nil
    }
    
    public func dateTimePlusLeist (curFC: String?) -> String {
        if self.FCLeist == curFC {
            return dateTimeString() ?? ""
        }
        let kaleist = self.KALeist.trimmingCharacters(in: .whitespaces)
        if kaleist.count > 0 {
            if let dts = dateTimeString() {
                return "\(dts) (\(kaleist))"
            }
            return kaleist
        }
        let fcleist = self.FCLeist.trimmingCharacters(in: .whitespaces)
        if fcleist.count > 0 {
            if let dts = dateTimeString() {
                return "\(dts) (\(fcleist))"
            }
            return fcleist
        }
        return dateTimeString() ?? ""
    }
}
