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

import Foundation
@_implementationOnly import LibLKF

public enum LKFAufenthaltAbtFields : UInt32 {
    case pos = 5
    case abtfc = 6
    case datum = 7
    case uhrzeit = 8
    case entdatum = 10
    case entzeit = 11
    case entart = 12
    case funktext = 90
    case k01text = 91
    case k01orFunk = 92
}


public class LKFAufenthaltAbt : Identifiable {
    
    public let id = UUID()
    let _aufenthalt: LKFAufenthalt
    let _index: UInt32
    
    var _line: LKFLine? = nil
    
    init (aufenthalt: LKFAufenthalt, index: UInt32) {
        _aufenthalt = aufenthalt
        _index = index
    }
    
    public var line : LKFLine
    {
        if _line == nil {
            let cline = UnsafeMutablePointer<CChar>.allocate(capacity:1024)
            LibLKF.lkf_auf_line_abt(_aufenthalt.ptr, _index, cline)
            _line = LKFLine (String(cString:cline))
            cline.deallocate()
        }
        return _line!
    }
    
    public var ABTFC : String {
        return line.valueAt(index: Int (LKFAufenthaltAbtFields.abtfc.rawValue))
    }
    
    public var POS : String {
        return line.valueAt(index: Int (LKFAufenthaltAbtFields.pos.rawValue))
    }
    
    public var Name : String {
        return line.valueAt(index: 6)
    }
    
    public var AbtDateString : String {
        return line.valueAt(index: Int (LKFAufenthaltAbtFields.datum.rawValue))
    }
    
    public var AbtTimeString : String {
        return line.valueAt(index: Int (LKFAufenthaltAbtFields.uhrzeit.rawValue))
    }
    
    public var AbtDate : Date? {
        return LKFConvert.toDate(string8: AbtDateString)
    }
    
    public var AbtEntDate : Date? {
        return LKFConvert.toDate(string8: EntAbtDateString)
    }
    
    public var AbtTime : LKFTime? {
        return LKFTime(AbtTimeString)
    }
    
    public var AbtEntTime: LKFTime? {
        return LKFTime(EntAbtTimeString)
    }
    
    public var EntAbtDateString : String {
        return line.valueAt(index: Int (LKFAufenthaltAbtFields.entdatum.rawValue))
    }
    
    public var EntAbtTimeString : String {
        return line.valueAt(index: Int (LKFAufenthaltAbtFields.entzeit.rawValue))

    }
    
    public var Text : String {
        let cline = UnsafeMutablePointer<CChar>.allocate(capacity:128)
        LibLKF.lkf_auf_get_abtfield(_aufenthalt.ptr, _index, LKFAufenthaltAbtFields.k01orFunk.rawValue, cline, 210)
        let ret = String(cString:cline, encoding: .ascii)
        cline.deallocate()
        return ret ?? ""
    }

    
    public var aufenthalt: LKFAufenthalt {
        return _aufenthalt
    }
    
    public var index : UInt32 {
        return _index
    }
    
    public func scoringData() -> LKFAufenthaltAbtScoringData {
        return LKFAufenthaltAbtScoringData(abt: self)
    }
    
    public var Fields : (String,String,String) {
        let cabtfc = UnsafeMutablePointer<CChar>.allocate(capacity:20)
        let cabtdat = UnsafeMutablePointer<CChar>.allocate(capacity:20)
        let cpos = UnsafeMutablePointer<CChar>.allocate(capacity:20)
        LibLKF.lkf_auf_get_abt (_aufenthalt.ptr, _index, cabtfc, cabtdat, cpos)
        let f = (String(cString:cabtfc), String(cString:cpos), String(cString:cabtdat))
        cabtfc.deallocate()
        cabtdat.deallocate()
        cpos.deallocate()
        return f
    }
    
    public func fromToString () -> String? {
        let from = LKFConvert.dateTimeString(date: AbtDate, time: AbtTime)
        let to = LKFConvert.dateTimeString(date: AbtEntDate, time: AbtEntTime)

        if from != nil {
            if to != nil {
                return "\(from!) - \(to!)"
            }
            return from
        }
        else if to != nil {
            return "? - \(to!)"
        }
        return nil
    }

}
