//
//  lkfcIntensivTests.swift
//  lkfcTests
//
//  Created by Alexander Holzer on 19.10.18.
//  Copyright © 2018 Alexander Holzer. All rights reserved.
//

import lkfc
import XCTest

class lkfcIntensivTests: XCTestCase {

    override func setUp() {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }
    
    static var sapsTest = [
        "X01|K427|1234||20180102|1443|S|19310501|85|M|AT|AT|4941|41214|A|A||||E|20180112|1612|UB|N|||",
        "X02|K427|1234||20180102|001|11111100|20180102|1443|||||||N|85||00001",
        "X02|K427|1234||20180102|002|12718100|20180105|0851||20180106|1610|1|||N|85||00002",
        "X02|K427|1234||20180102|003|11211100|20180106|1610|||||||N|85||00003",
        "X03|K427|1234||20180102|F10.0|H|||||00001",
    "I11|K427|1234||20180102|002|20180105|00|1|3|0|0|00|0|0|0|0|0|0|1|6,71|7,4|0259|32|1||4|4|044|000000000000000000000|36,5|1|00199|050|0,3|0,73|170|001|5",
        "I12|K427|1234||20180102|002|20180105|1|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0",
        "I12|K427|1234||20180102|002|20180106|1|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0",
        "L01|2017|K427|12718100|I|E1|"
    ]

    func createContextWith (model: UInt32, lines : [String]) throws -> LKFContext {
        return try lkfcTests.createContextWith(model:model, lines:lines)
    }
    
    func expectErrorCount (context: LKFContext, code:String) -> Int {
        let err = context.errorsWith(code: code)
        return err?.count ?? 0
    }
    
    func expectError (context: LKFContext, code:String) -> Bool {
        return expectErrorCount(context: context, code: code) == 1
    }
    
    func testSDK() throws {
        let context = try createContextWith(model: 2018, lines: lkfcIntensivTests.sapsTest)
        try context.execute()
        
        XCTAssert(!expectError(context: context, code: "EX0P"), "EX0P wrong")
        
        let intAbtCount = context.computeIntensiv()
        XCTAssert(intAbtCount == 1, "one int abt expected")
        if let abt = context.getIntAbt(atIndex:0) {
            XCTAssert(abt.score == 33, "wrong score: expected 33, result \(abt.score)")
        }
        else {
            XCTAssert(false, "no abt returned")
        }
        
        let aufenthalt = context.first
        let intResult = aufenthalt.getIntResults()
        
        if intResult.count != 3 {
            XCTAssert(false, "count of abt wrong (expected 3, has \(intResult.count)")
        }
        else {
            if let abt = intResult[1] {
                XCTAssert(abt.score == 33, "wrong score: expected 33, result \(abt.score) (auf)")
                XCTAssert(abt.gcs == 14, "wrong gcs: expected 14, result \(abt.gcs) (auf)")
                XCTAssert(abt.partString == "18|0|0|0|0|0|0|0|0|0|0|-8|0|0|0|0|0|0|0|0|0|0|7|16", "wrong part values")
            }
            else {
                XCTAssert(false, "wrong abt")
            }
        }
        
    }
}
