//
//  LKFOutputRowDescriptionFile.swift
//  lkfc
//
//  Created by Alexander Holzer on 01.07.18.
//  Copyright © 2018 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFOutputRowDescriptionFile : LKFOutputFile {

    public override init(context: LKFContext) {
        super.init(context: context)
    }

    public override var defaultfileName : String {
        return "satzart\(context.model).csv"
    }
    
    internal func getCheckInfo (checkInfo: UnsafeRawPointer, fieldTextId: UnsafePointer<CChar>, errorIndex: UnsafePointer<CChar>, lineBuffer: UnsafeMutablePointer<CChar>) -> [String]
    {
        var outputStrings = [String]()
        LibLKF.lkf_Satzart_getCheckInfo(context.ptr!, checkInfo, fieldTextId, errorIndex, lineBuffer, 1024)
        outputStrings.append(String(cString:lineBuffer))
        
        if LibLKF.lkf_Satzart_getCheckDetails (context.ptr!, checkInfo, lineBuffer, 2048) > 0 {
            
            for d in String(cString:lineBuffer).components(separatedBy: "\n") {
                outputStrings.append(String(format:"D;%s                         ; %-16.16s;\"%@\"", fieldTextId, errorIndex, d))
            }
        }
        
        let nextCheck = LibLKF.lkf_Satzart_getNextCheck(context.ptr!, checkInfo, 1)
        var nextCheckErr = LibLKF.lkf_Satzart_getNextCheck(context.ptr!, checkInfo, 0)
        
        
        let errorIndexString = String(cString:errorIndex)
        
        if nextCheck != nil {
            let nextErrorIndex : String
            if nextCheck == nextCheckErr {
                nextErrorIndex = errorIndexString + ".->"
                nextCheckErr = nil
            }
            else {
                nextErrorIndex = errorIndexString + ".ok"
            }
            outputStrings.append(contentsOf: getCheckInfo(checkInfo: nextCheck!, fieldTextId: fieldTextId, errorIndex: (nextErrorIndex as NSString).utf8String!, lineBuffer: lineBuffer))

        }
        if nextCheckErr != nil {
            let nextErrorIndex = errorIndexString + ".er"
            outputStrings.append(contentsOf: getCheckInfo(checkInfo: nextCheckErr!, fieldTextId: fieldTextId, errorIndex: (nextErrorIndex as NSString).utf8String!, lineBuffer: lineBuffer))
            
        }
        return outputStrings
    }
    
    
    
    public override func getOutput() -> [String] {
        
        let lineBuffer = UnsafeMutablePointer<CChar>.allocate(capacity:2048)
        let fieldTextId = UnsafeMutablePointer<CChar>.allocate(capacity:128)
        
        defer {
            fieldTextId.deallocate()
            lineBuffer.deallocate()
        }
        
        var outputStrings = [String]()
   
        let satzartDefs = LibLKF.lkf_Satzart_getDefinition(context.model)
        let satzartCount = LibLKF.lkf_Satzart_getCount(satzartDefs)

        guard context.ptr != nil
        else {
            return []
        }
        
        outputStrings.append("SATZART-DEFINITIONEN fuer Modelljahr \(context.model)")
        
        for i in 0..<satzartCount {
            if LibLKF.lkf_Satzart_getSatzartDef(satzartDefs, i, lineBuffer, 1024) != 0 {
                outputStrings.append(String(cString:lineBuffer))
            }
            if LibLKF.lkf_Satzart_getSatzartKeyInfo(satzartDefs, i, lineBuffer, 1024) != 0 {
                outputStrings.append(String(cString:lineBuffer))
            }
            
            let fieldCount = LibLKF.lkf_Satzart_getSatzartFieldCount(satzartDefs, i)
            
            for f in 0..<fieldCount {
                if LibLKF.lkf_Satzart_getSatzartFieldInfo(satzartDefs, i, f, lineBuffer, 1024) != 0 {
                    outputStrings.append(String(cString:lineBuffer))
                }
                
                if let checkInfo = LibLKF.lkf_Satzart_getSatzartFieldCheck (context.ptr!, satzartDefs, i, f) {
                    LibLKF.lkf_Satzart_getSatzartFieldTextId(satzartDefs, i, f, fieldTextId, 128)
                    outputStrings.append(contentsOf: getCheckInfo (checkInfo: checkInfo, fieldTextId: fieldTextId, errorIndex: "", lineBuffer: lineBuffer))
                }
                
            }
            if let checkInfo = LibLKF.lkf_Satzart_getSatzartFieldCheck(context.ptr!, satzartDefs, i, 1<<16) {
                LibLKF.lkf_Satzart_getSatzartFieldTextId(satzartDefs, i, 1<<16, fieldTextId, 128)
                outputStrings.append(contentsOf: getCheckInfo (checkInfo: checkInfo, fieldTextId: fieldTextId, errorIndex: "", lineBuffer: lineBuffer))
                
            }
        }
        
        var geIndex : UInt32 = 0
        var checkInfo = LibLKF.lkf_Satzart_getSatzartFieldCheck(context.ptr!, satzartDefs, 1<<16, geIndex)
        
        if checkInfo != nil {
            repeat {
                LibLKF.lkf_Satzart_getSatzartFieldTextId(satzartDefs, 1<<16, geIndex, fieldTextId, 128)
                outputStrings.append(contentsOf: getCheckInfo (checkInfo: checkInfo!, fieldTextId: fieldTextId, errorIndex: "", lineBuffer: lineBuffer))
                geIndex += 1
                checkInfo = LibLKF.lkf_Satzart_getSatzartFieldCheck(context.ptr!, satzartDefs, 1<<16, geIndex)
            } while checkInfo != nil
        }
        
        geIndex = 0
        checkInfo = LibLKF.lkf_Satzart_getSatzartFieldCheck(context.ptr!, satzartDefs, 1<<17, geIndex)
        
        if checkInfo != nil {
            repeat {
                LibLKF.lkf_Satzart_getSatzartFieldTextId(satzartDefs, 1<<17, geIndex, fieldTextId, 128)
                outputStrings.append(contentsOf: getCheckInfo (checkInfo: checkInfo!, fieldTextId: fieldTextId, errorIndex: "", lineBuffer: lineBuffer))
                geIndex += 1
                checkInfo = LibLKF.lkf_Satzart_getSatzartFieldCheck(context.ptr!, satzartDefs, 1<<17, geIndex)
            } while checkInfo != nil
        }
        
        return outputStrings
    }

}
