import Foundation

struct GermanGameSummarizer: GameSummarizer {

    let table: FinishedTable

    private var winText: String {
        table.selectorDidWin ? "gewinnt" : "verliert"
    }

    private var gameText: String {
        switch table.game {
        case .rufBlatt:
            return "den Ruf Blatt"
        case .rufEichel:
            return "den Ruf Eichel"
        case .rufSchelln:
            return "den Ruf Schelln"
        case .bettel:
            return "den Bettel"
        case .geier:
            return "den Geier"
        case .wenz:
            return "den Wenz"
        case .hochzeit:
            return "die Hochzeit"
        case .soloBlatt:
            return "das Solo Blatt"
        case .soloEichel:
            return "das Solo Eichel"
        case .soloHerz:
            return "das Solo Herz"
        case .soloSchelln:
            return "das Solo Schelln"
        }
    }

    private var coPlayerNames: String {
        let coPlayers = table.coPlayers
        switch coPlayers.count {
        case 0:
            return ""
        case 1:
            return " mit \(coPlayers[0].name)"
        case 2:
            return " mit \(coPlayers[0].name) und \(coPlayers[1].name)"
        default:
            return ""
        }
    }

    private var costText: String {
        let cost = table.cost
        guard cost >= 100 else {
            return "\(cost) Cent"
        }
        return String(format: "%d.%02d €", cost / 100, cost % 100)
    }

    private var costExplanation: String {
        var components = [String]()
        components.append("Grundspiel \(table.game.basicCost)")
        if !table.isBettel {
            if table.isHochzeit {
                components.append("Hochzeit")
            }
            if table.isSchwarz {
                components.append("Schwarz")
            } else if table.isSchneider {
                components.append("Schneider")
            }
            if table.leadingTrumps > 0 {
                components.append("\(table.leadingTrumps) Laufende")
            }
        }
        components.append("\(table.totalNumberOfDoubles)x gedoppelt")
        return components.joined(separator: ", ")
    }

    var text: String {
        let start = "\(table.gameSelector.name) \(winText) \(gameText)"
        let cost = " Das Spiel kostet \(costText) (\(costExplanation))."
        guard table.game != .bettel else {
            return start + cost
        }
        return start + "\(coPlayerNames) mit \(table.selectorTeamPoints) Punkten." + cost
    }
}
