//
//  LKFScoringDetailsGroupMel.swift
//  LKF
//
//  Created by Alexander Holzer on 18.03.22.
//  Copyright © 2022 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFScoringDetailsGroupMelzu {
    public let group: LKFScoringDetailsMEL
    public let melIndex: UInt32
    public let melPool : Int
    public let melMelWinLose : String?
    public let group_melzu : Int
    public let maingroup_melzu : Int
    public let dynMelId : Int
    public let dynMelDiff : Int
    public let line : String
    
    init (group: LKFScoringDetailsMEL, melIndex: UInt32, line: String) {
        self.group = group
        self.melIndex = melIndex
        self.line = line
        let parts = line.components(separatedBy: "|")

        group_melzu = parts.count > 1 ? (Int(parts[0]) ?? 0) : 0
        maingroup_melzu = parts.count > 2 ? (Int(parts[1]) ?? 0) : 0
        
        if parts.count > 4 {
            dynMelId = Int(parts[2]) ?? -1
            dynMelDiff = Int(parts[3]) ?? 0
        }
        else {
            dynMelId = 0
            dynMelDiff = 0
        }
        
        if parts.count > 6, let melPool = Int(parts[4]), melPool > 0 {
            self.melPool = melPool
            self.melMelWinLose = parts[5]
        }
        else {
            self.melPool = 0
            self.melMelWinLose = nil
        }
    }
    
    public var MELANZString : String {
        return group.mel.aufenthalt.melAt(index: melIndex).MELANZString
    }

    public var MELSeite : String {
        return group.mel.aufenthalt.melAt(index: melIndex).MELSeite
    }
    
    public var MELDate : Date? {
        return group.mel.aufenthalt.melAt(index: melIndex).MELDate
    }

    public var MEL: String {
        return group.mel.aufenthalt.melAt(index: melIndex).MEL
    }
    
    public var Text : String {
        return group.mel.aufenthalt.melAt(index: melIndex).Text
    }
    
    public var MELAbtIndex : Int? {
        return group.mel.aufenthalt.melAt(index: melIndex).abtIndex
    }
    
    public var traceLines : [String] {
        return [line]
    }
        
    public var melMelWinLoseExplained : String? {
        guard let melMelWinLose = melMelWinLose else {
            return nil
        }
        
        return melMelWinLose
    }
}
