//
//  lkfEX26tests.swift
//  lkfcTests
//
//  Created by Alexander Holzer on 07.09.20.
//  Copyright © 2020 Alexander Holzer. All rights reserved.
//

import XCTest
@testable import lkfc

class lkfEX26tests: XCTestCase {

    func createContextWith (model: UInt32, lines : [String]) throws -> LKFContext {
        return try lkfcTests.createContextWith(model:model, lines:lines)
    }
    
    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 fallWX3Q =
        [
            "X01|K901|123||20190109|0615|S|20030603|25|W|AT|AT|8753|62007|A|P||||S|20190113|1800|15|J|2",
            "X02|K901|123||20190109|001|11211100|20190109|0615||20190113|1800||||N|25|",
            "X03|K901|123||20190109|M48.0|H|D||",
            "X04|K901|123||20190109|AM100||J|1|20190111|0740||K914|"
        ]
   
    static var fallEX26 =
        [
            "X01|K901|123||20190109|0615|S|20030603|25|W|AT|AT|8753|62007|A|P||||S|20190113|1800|15|J|2",
            "X02|K901|123||20190109|001|14628500|20190109|0615||20190113|1800||||N|25|",
            "X03|K901|123||20190109|M48.0|H|D||",
            "X04|K901|123||20190109|AM080||J|1|20190111|0740||K914|"
        ]
    
    static var fallEX26Amb =
        [
            "X01|K901|125||20190201|1245|I|19600912|55|M|AT|AT|8301|60661|||||||||15|J|0",
            "X02|K901|125||20190201|001|17725100|20190201|1245|J||||||N|55|2017032079",
            "X02|K901|125||20190201|002|17725100|20190201|1608|J||||||N|55|2019084448",
            "X03|K901|125||20190201|F90.1|H|||2019078251",
            "X04|K901|125||20190201|AM080||K|1|20190201|1704|17725100||2019084448",
        ]
    
    func testEX26() throws {
        var context = try createContextWith(model:2019, lines: lkfEX26tests.fallEX26Amb)
        _ = lkfcTests.resultForContext(context: context)
        var errors = context.errorsWith (code:"EX26") // Diagnosen-Kombination
        XCTAssert (errors != nil, "missing error EX26")

        context = try createContextWith(model:2019, lines: replaceMEL(fall: lkfEX26tests.fallEX26Amb, oldmel: "AM080", newmel: "AM100"))
        _ = lkfcTests.resultForContext(context: context)
        errors = context.errorsWith (code:"WX3Q") // Diagnosen-Kombination
        XCTAssert (errors != nil, "missing error WX3Q")

        
        context = try createContextWith(model:2019, lines: lkfEX26tests.fallEX26)
        _ = lkfcTests.resultForContext(context: context)
        errors = context.errorsWith (code:"WX3R") // Diagnosen-Kombination
        XCTAssert (errors != nil, "missing error WX3R")

        context = try createContextWith(model:2019, lines: lkfEX26tests.fallWX3Q)
        _ = lkfcTests.resultForContext(context: context)
        errors = context.errorsWith (code:"WX3Q") // Diagnosen-Kombination
        XCTAssert (errors != nil, "missing error WX3Q")
    }
    
    var spezmellist = ["AA040", "AJ140", "AK010", "AM020", "AM030", "AM040", "AM050", "AM060", "AM070", "AM080", "AM090", "AM100", "AM110", "AM120", "AM130", "DB020", "LH010", "LH020", "LH021", "ZZ020", "ZZ710"]
     
    func replaceMEL (fall: [String], oldmel: String, newmel: String) -> [String] {
        var arr = [String]()
        for s in fall {
            if s.starts(with: "X04") {
                arr.append(s.replacingOccurrences(of: oldmel, with: newmel))
                continue
            }
            arr.append(s)
        }
        return arr;
    }
    
    func testSpezMEL() throws {
        var caseStat = LKFCaseStat()

        let model : UInt32 = 2020
        
        for s in spezmellist {
            let statContext = try createContextWith(model:model, lines: replaceMEL(fall: lkfEX26tests.fallWX3Q, oldmel: "AM100", newmel: s))
            _ = lkfcTests.resultForContext(context: statContext)
            caseStat = caseStat.adding (statContext.CaseStat())

            let stat2Context = try createContextWith(model:model, lines: replaceMEL(fall: lkfEX26tests.fallEX26, oldmel: "AM080", newmel: s))
                _ = lkfcTests.resultForContext(context: stat2Context)
                caseStat = caseStat.adding (statContext.CaseStat())

            let ambContext = try createContextWith(model: model, lines: replaceMEL(fall: lkfEX26tests.fallEX26Amb, oldmel: "AM080", newmel: s))
            _ = lkfcTests.resultForContext(context: ambContext)
            caseStat = caseStat.adding (ambContext.CaseStat())
        }
        
        caseStat.out()
    }

}
