//
//  LKFAmbStat.swift
//  lkfc
//
//  Created by Alexander Holzer on 02.12.19.
//  Copyright © 2019 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFAmbStat {
    
    public let kostenstellen: [LKFAmbStatKst]
    
    init (stat: LKFStat) {
        var arr = [LKFAmbStatKst]()
        let kstCount = stat.GetAmbKostenstelleCount()
        for i in 0..<kstCount {
            arr.append(LKFAmbStatKst(stat: stat, kostenstelleIndex: i))
        }
        kostenstellen = arr
    }
}

public class LKFAmbStatKst : Identifiable {
    
    public let kostenstelle: String
    public let chemo: Int32
    public let psych: Int32
    public let tkl: Int32
    public let zae: Int32
    
    public var id: String {
        return kostenstelle
    }
    
    init (stat: LKFStat, kostenstelleIndex: UInt32) {
        if let s = stat.GetAmbKostenstelleStringValue(kostenstelleIndex: kostenstelleIndex, statValue: .AbtFC) {
            kostenstelle = s
        }
        else {
            kostenstelle = "???"
        }
        
        chemo = stat.GetAmbKostenstelleValue(kostenstelleIndex: kostenstelleIndex, statValue: .Chemo)
        psych = stat.GetAmbKostenstelleValue(kostenstelleIndex: kostenstelleIndex, statValue: .Psych)
        tkl = stat.GetAmbKostenstelleValue(kostenstelleIndex: kostenstelleIndex, statValue: .TKL)
        zae = stat.GetAmbKostenstelleValue(kostenstelleIndex: kostenstelleIndex, statValue: .ZAE)
    }
    
    init (kostenstelle: String, chemo: Int32, psych: Int32, tkl: Int32, zae: Int32) {
        self.kostenstelle = kostenstelle
        self.chemo = chemo
        self.psych = psych
        self.tkl = tkl
        self.zae = zae
    }
    
    convenience init (kostenstelle: String, statCount: LKFAmbStatValue) {
        switch statCount {
        case .Chemo:
            self.init (kostenstelle: kostenstelle, chemo: 1, psych: 0, tkl: 0, zae: 0)
            break
        case .Psych:
            self.init (kostenstelle: kostenstelle, chemo: 0, psych: 1, tkl: 0, zae: 0)
            break
        case .TKL:
            self.init (kostenstelle: kostenstelle, chemo: 0, psych: 0, tkl: 1, zae: 0)
            break
        case .ZAE:
            self.init (kostenstelle: kostenstelle, chemo: 0, psych: 0, tkl: 0, zae: 1)
            break
        default:
            self.init (kostenstelle: kostenstelle, chemo: 0, psych: 0, tkl: 0, zae: 0)
            break
        }
    }
    
    func difference (candidate: LKFAmbStatKst) -> String? {
        if !candidate.kostenstelle.isEmpty && kostenstelle.compare(candidate.kostenstelle) != .orderedSame {
            return "different ABTFC expected:\(kostenstelle), has: \(candidate.kostenstelle)"
        }
        if candidate.chemo != chemo {
            return "chemo different for ABTFC \(kostenstelle): expected: \(chemo), has:\(candidate.chemo)"
        }
        if candidate.psych != psych {
            return "chemo different for ABTFC \(kostenstelle): expected: \(psych), has:\(candidate.psych)"
        }
        if candidate.tkl != tkl {
            return "chemo different for ABTFC \(kostenstelle): expected: \(tkl), has:\(candidate.tkl)"
        }
        if candidate.zae != zae {
            return "chemo different for ABTFC \(kostenstelle): expected: \(zae), has:\(candidate.zae)"
        }
        return nil
    }
}
