import Foundation

struct EnglishGameSummarizer: GameSummarizer {

    let table: FinishedTable

    private var winText: String {
        table.selectorDidWin ? "won" : "lost"
    }

    private var gameText: String {
        switch table.game {
        case .rufBlatt:
            return "the call of Blatt"
        case .rufEichel:
            return "the call of Eichel"
        case .rufSchelln:
            return "the call of Schelln"
        case .bettel:
            return "the Bettel"
        case .geier:
            return "the Geier"
        case .wenz:
            return "the Wenz"
        case .hochzeit:
            return "the wedding"
        case .soloBlatt:
            return "the Solo Blatt"
        case .soloEichel:
            return "the Solo Eichel"
        case .soloHerz:
            return "the Solo Herz"
        case .soloSchelln:
            return "the Solo Schelln"
        }
    }

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

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

    private var costExplanation: String {
        var components = [String]()
        components.append("Game \(table.game.basicCost)")
        if !table.isBettel {
            if table.isHochzeit {
                components.append("wedding")
            }
            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 doubled")
        return components.joined(separator: ", ")
    }

    var text: String {
        let start = "\(table.gameSelector.name) \(winText) the \(gameText)"
        let cost = " The game costs \(costText) (\(costExplanation))."
        guard table.game != .bettel else {
            return start + cost
        }
        return start + "\(coPlayerNames) with \(table.selectorTeamPoints) points." + cost
    }
}
