//
//  LKFFileProperties.swift
//  LKF
//
//  Created by Alexander Holzer on 20.08.22.
//  Copyright © 2022 Alexander Holzer. All rights reserved.
//

import Foundation

public class LKFFileProperties {
    
    public let file: URL
    public let encoding: String.Encoding
    public let complete: Bool
    public let lineCount : Int
    public let bumi : Bool
    public let x01Count : Int
    public let ambCount : Int
    public let statCount : Int
    public let year: Int
    
    public init (file: URL, encodings: [String.Encoding] = [.utf8, .ascii, .isoLatin1, .windowsCP1252]) throws {
        self.file = file
 
        var fileContents : String? = nil
        
        var loadError : Error?
        var encoding : String.Encoding? = nil

        for e in encodings {
            do {
                fileContents = try String(contentsOf: file, encoding: e)
                encoding = e
                loadError = nil
                break
            }
            catch {
                loadError = error
            }
        }

        guard let fileContents, let encoding else {
            throw loadError ?? LKFError("no content")
        }

        self.encoding = encoding
        
        let lines = fileContents.components(separatedBy: .newlines)
        lineCount = lines.count
 
        var hasK01 = false
        var abrYear = [Int:Int]()
        var bumiCount = 0
        var x01Count = 0
        var ambCount = 0
        var statCount = 0
        for l in lines {
            
            if l.starts(with: "X01") {
                let x01 = l.components(separatedBy: "|")
                
                if x01.count > 10 {
                    x01Count += 1
                    
                    if let year = Int (x01[4].prefix(4)) {
                        if abrYear[year] != nil {
                            abrYear[year]! += 1
                        }
                        else {
                            abrYear[year] = 1
                        }
                    }
                    
                    if x01[2].isEmpty && !x01[3].isEmpty {
                        bumiCount += 1
                    }
                    
                    if x01[6] == "S" {
                        statCount += 1
                    }
                    else if x01[6] == "I" {
                        ambCount += 1
                    }
                }
            }
            else if !hasK01 && l.starts(with: "K01") {
                hasK01 = true
            }
        }
        
        self.ambCount = ambCount
        self.statCount = statCount
        self.x01Count = x01Count
        self.complete = hasK01
        self.bumi = bumiCount * 10 > x01Count * 9
        
        var maxAbrCount = 0
        var maxAbrYear = 0
        
        for d in abrYear {
            if d.value > maxAbrCount {
                maxAbrYear = d.key
                maxAbrCount = d.value
            }
        }
        
        self.year = maxAbrYear
        
        
    }
    
    
}
