//
//  GameResult.swift
//  Blackjack
//
//  Possible outcomes for a Blackjack hand.
//

import SwiftUI

/// Result of a single Blackjack hand.
enum HandResult: Equatable {
    case blackjack          // Natural 21
    case win                // Beat dealer
    case lose               // Lost to dealer or bust
    case push               // Tie
    case bust               // Over 21
    case surrender          // Gave up half bet
    case insuranceWin       // Dealer had blackjack
    case insuranceLose      // Dealer didn't have blackjack
    
    /// String identifier for persistence.
    var saveName: String {
        switch self {
        case .blackjack: return "blackjack"
        case .win: return "win"
        case .lose: return "lose"
        case .push: return "push"
        case .bust: return "bust"
        case .surrender: return "surrender"
        case .insuranceWin: return "insuranceWin"
        case .insuranceLose: return "insuranceLose"
        }
    }
    
    var displayText: String {
        switch self {
        case .blackjack: return String(localized: "BLACKJACK!")
        case .win: return String(localized: "WIN!")
        case .lose: return String(localized: "LOSE")
        case .push: return String(localized: "PUSH")
        case .bust: return String(localized: "BUST!")
        case .surrender: return String(localized: "SURRENDER")
        case .insuranceWin: return String(localized: "INSURANCE WINS")
        case .insuranceLose: return String(localized: "INSURANCE LOSES")
        }
    }
    
    var color: Color {
        switch self {
        case .blackjack: return .yellow
        case .win: return .green
        case .lose, .bust, .insuranceLose: return .red
        case .push: return .blue
        case .surrender: return .orange
        case .insuranceWin: return .green
        }
    }
    
    var isWin: Bool {
        switch self {
        case .blackjack, .win, .insuranceWin: return true
        default: return false
        }
    }
}

/// Overall game result for the round.
struct RoundResult: Equatable {
    /// Results for all player hands (index 0 = Hand 1, index 1 = Hand 2, etc.)
    let handResults: [HandResult]
    /// Net winnings for each hand (parallel to handResults)
    let handWinnings: [Int]
    let insuranceResult: HandResult?
    /// Insurance winnings (positive if won, negative if lost)
    let insuranceWinnings: Int
    let totalWinnings: Int
    let wasBlackjack: Bool
    
    // MARK: - Side Bets
    
    /// Perfect Pairs result (nil if no bet placed)
    let perfectPairsResult: PerfectPairsResult?
    /// Perfect Pairs winnings (positive if won, negative if lost)
    let perfectPairsWinnings: Int
    /// 21+3 result (nil if no bet placed)
    let twentyOnePlusThreeResult: TwentyOnePlusThreeResult?
    /// 21+3 winnings (positive if won, negative if lost)
    let twentyOnePlusThreeWinnings: Int
    
    /// Convenience initializer without per-hand winnings (backwards compatibility)
    init(handResults: [HandResult], insuranceResult: HandResult?, totalWinnings: Int, wasBlackjack: Bool) {
        self.handResults = handResults
        self.handWinnings = []  // Empty means don't show per-hand amounts
        self.insuranceResult = insuranceResult
        self.insuranceWinnings = 0
        self.totalWinnings = totalWinnings
        self.wasBlackjack = wasBlackjack
        self.perfectPairsResult = nil
        self.perfectPairsWinnings = 0
        self.twentyOnePlusThreeResult = nil
        self.twentyOnePlusThreeWinnings = 0
    }
    
    /// Full initializer with per-hand winnings
    init(handResults: [HandResult], handWinnings: [Int], insuranceResult: HandResult?, insuranceWinnings: Int, totalWinnings: Int, wasBlackjack: Bool) {
        self.handResults = handResults
        self.handWinnings = handWinnings
        self.insuranceResult = insuranceResult
        self.insuranceWinnings = insuranceWinnings
        self.totalWinnings = totalWinnings
        self.wasBlackjack = wasBlackjack
        self.perfectPairsResult = nil
        self.perfectPairsWinnings = 0
        self.twentyOnePlusThreeResult = nil
        self.twentyOnePlusThreeWinnings = 0
    }
    
    /// Full initializer with side bets
    init(
        handResults: [HandResult],
        handWinnings: [Int],
        insuranceResult: HandResult?,
        insuranceWinnings: Int,
        perfectPairsResult: PerfectPairsResult?,
        perfectPairsWinnings: Int,
        twentyOnePlusThreeResult: TwentyOnePlusThreeResult?,
        twentyOnePlusThreeWinnings: Int,
        totalWinnings: Int,
        wasBlackjack: Bool
    ) {
        self.handResults = handResults
        self.handWinnings = handWinnings
        self.insuranceResult = insuranceResult
        self.insuranceWinnings = insuranceWinnings
        self.perfectPairsResult = perfectPairsResult
        self.perfectPairsWinnings = perfectPairsWinnings
        self.twentyOnePlusThreeResult = twentyOnePlusThreeResult
        self.twentyOnePlusThreeWinnings = twentyOnePlusThreeWinnings
        self.totalWinnings = totalWinnings
        self.wasBlackjack = wasBlackjack
    }
    
    /// The main/best result for display purposes (first hand, or best if split)
    var mainHandResult: HandResult {
        // Return the best result for the headline
        if wasBlackjack { return .blackjack }
        if handResults.contains(.win) { return .win }
        if handResults.contains(.push) { return .push }
        if handResults.contains(.surrender) { return .surrender }
        if handResults.allSatisfy({ $0 == .bust }) { return .bust }
        return handResults.first ?? .lose
    }
    
    /// Whether this round had split hands
    var hadSplit: Bool {
        handResults.count > 1
    }
    
    /// Whether this round had any side bets
    var hadSideBets: Bool {
        perfectPairsResult != nil || twentyOnePlusThreeResult != nil
    }
    
    /// Legacy accessor for backwards compatibility
    var splitHandResult: HandResult? {
        handResults.count > 1 ? handResults[1] : nil
    }
}

