//
//  LKFCaseStat.swift
//  lkfc
//
//  Created by Alexander Holzer on 13.09.20.
//  Copyright © 2020 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFCaseStatCategory {
    let key : String
    let category: String
    let ident : String
    let ambCount : Int
    let statCount : Int
    let ambStatCount : Int
    

    init(c:UnsafeMutablePointer<LKF_CASESTAT_CATEGORY>) {
        
        let cc = c.move()
        ambCount = Int(cc.amb)
        statCount = Int(cc.stat)
        ambStatCount = Int(cc.ambstat)
        
        key = withUnsafePointer(to: &c.pointee.key, { (ptr) -> String in
            return ptr.withMemoryRebound(to: Int8.self, capacity: 52) { int8Ptr in
                return String (cString: int8Ptr, encoding: .utf8) ?? ""
            }
        })
        
        category = withUnsafePointer(to: &c.pointee.category, { (ptr) -> String in
            return ptr.withMemoryRebound(to: Int8.self, capacity: 34) { int8Ptr in
                return String (cString: int8Ptr, encoding: .utf8) ?? ""
            }
        })
        
        ident = withUnsafePointer(to: &c.pointee.ident, { (ptr) -> String in
            return ptr.withMemoryRebound(to: Int8.self, capacity: 18) { int8Ptr in
                return String (cString: int8Ptr, encoding: .utf8) ?? ""
            }
        })
    }
    
    init (source:LKFCaseStatCategory, add:LKFCaseStatCategory) {
        key = source.key
        category = source.category
        ident = source.ident
        ambStatCount = source.ambStatCount + add.ambStatCount
        ambCount = source.ambCount + add.ambCount
        statCount = source.statCount + add.statCount
    }
}

public class LKFCaseStat {

    let categories : [String:LKFCaseStatCategory]    

    init() {
        categories = [:]
    }
    
    init (context: LKFContext) {
        guard let ptr = context.ptr else {
            categories = [:]
            return
        }
        
        let statcategoryCount = LibLKF.lkf_casestat_get_category_count(ptr)

        var dict = [String:LKFCaseStatCategory]()
        
        for i in 0..<statcategoryCount {
            if let cur = LibLKF.lkf_casestat_get_category(ptr, i) {
                let category = LKFCaseStatCategory(c: cur)
                dict[category.key] = category
            }
        }
        categories = dict
    }
    
    init (source: LKFCaseStat, add: LKFCaseStat) {
        var dict = [String:LKFCaseStatCategory]()
        dict = source.categories
        for a in add.categories {
            if let s = dict[a.key] {
                dict[a.key] = LKFCaseStatCategory(source: s, add: a.value)
            }
            else {
                dict[a.key] = a.value
            }
        }
        categories = dict
    }
    
    func adding (_ add : LKFCaseStat) -> LKFCaseStat {
        return LKFCaseStat(source: self, add: add)
    }
    
    func out () {
        for x in categories {
           print("\(x.value.key): stat=\(x.value.statCount), amb=\(x.value.ambCount), ambstat=\(x.value.ambStatCount)")
        }
    }
}
