//
//  RulesHelpView.swift
//  Baccarat
//
//  Game rules and how to play guide.
//

import SwiftUI
import CasinoKit

struct RulesHelpView: View {
    @Environment(\.dismiss) private var dismiss
    @State private var currentPage = 0
    
    private let pages: [RulePage] = [
        RulePage(
            title: String(localized: "Objective"),
            icon: "target",
            content: [
                String(localized: "Bet on which hand will win: Player, Banker, or Tie."),
                String(localized: "The hand closest to 9 wins."),
                String(localized: "Cards are dealt automatically — no decisions to make!"),
                String(localized: "Baccarat has one of the lowest house edges in the casino.")
            ]
        ),
        RulePage(
            title: String(localized: "Card Values"),
            icon: "suit.spade.fill",
            content: [
                String(localized: "Ace: 1 point"),
                String(localized: "2-9: Face value"),
                String(localized: "10, Jack, Queen, King: 0 points"),
                String(localized: "Hand values use only the last digit (e.g., 7+8=15 → 5).")
            ]
        ),
        RulePage(
            title: String(localized: "Natural Win"),
            icon: "star.fill",
            content: [
                String(localized: "If either hand totals 8 or 9 with two cards, it's a 'Natural'."),
                String(localized: "A Natural ends the round immediately."),
                String(localized: "Natural 9 beats Natural 8."),
                String(localized: "Two Naturals of the same value result in a Tie.")
            ]
        ),
        RulePage(
            title: String(localized: "Main Bets"),
            icon: "dollarsign.circle.fill",
            content: [
                String(localized: "Player Bet: Pays 1:1 (even money)"),
                String(localized: "Banker Bet: Pays 0.95:1 (5% commission)"),
                String(localized: "Tie Bet: Pays 8:1"),
                String(localized: "Banker bet has the lowest house edge (1.06%).")
            ]
        ),
        RulePage(
            title: String(localized: "Third Card - Player"),
            icon: "hand.draw.fill",
            content: [
                String(localized: "If neither hand has a Natural, third card rules apply."),
                String(localized: "Player with 0-5: Draws a third card"),
                String(localized: "Player with 6-7: Stands"),
                String(localized: "Player with 8-9: Natural (no third card)")
            ]
        ),
        RulePage(
            title: String(localized: "Third Card - Banker"),
            icon: "person.fill",
            content: [
                String(localized: "If Player stands, Banker draws on 0-5, stands on 6-7."),
                String(localized: "If Player draws, Banker's action depends on Player's third card:"),
                String(localized: "Banker 0-2: Always draws"),
                String(localized: "Banker 3: Draws unless Player's 3rd was 8"),
                String(localized: "Banker 4: Draws if Player's 3rd was 2-7"),
                String(localized: "Banker 5: Draws if Player's 3rd was 4-7"),
                String(localized: "Banker 6: Draws if Player's 3rd was 6-7"),
                String(localized: "Banker 7: Always stands")
            ]
        ),
        RulePage(
            title: String(localized: "Dragon Bonus"),
            icon: "flame.fill",
            content: [
                String(localized: "Side bet on Player or Banker winning by a margin."),
                String(localized: "Natural Win: 1:1"),
                String(localized: "Win by 9 points: 30:1"),
                String(localized: "Win by 8 points: 10:1"),
                String(localized: "Win by 7 points: 6:1"),
                String(localized: "Win by 6 points: 4:1"),
                String(localized: "Win by 5 points: 2:1"),
                String(localized: "Win by 4 points: 1:1")
            ]
        ),
        RulePage(
            title: String(localized: "Pair Bonus"),
            icon: "square.on.square.fill",
            content: [
                String(localized: "Side bet on the first two cards being a pair."),
                String(localized: "Pair Pays: 11:1"),
                String(localized: "Only the rank matters (suits are ignored)."),
                String(localized: "Example: 5♥ + 5♣ = Pair (wins!)"),
                String(localized: "Pairs occur roughly once every 15 hands."),
                String(localized: "Independent of the main game result.")
            ]
        ),
        RulePage(
            title: String(localized: "Strategy Tips"),
            icon: "lightbulb.fill",
            content: [
                String(localized: "Always bet Banker — it has the best odds (1.06% edge)."),
                String(localized: "Avoid the Tie bet — 14.4% house edge!"),
                String(localized: "Dragon Bonus is fun but has ~2.7% house edge."),
                String(localized: "Pair bets have ~10% house edge."),
                String(localized: "There's no skill involved — just enjoy the game!"),
                String(localized: "Set a budget and stick to it.")
            ]
        )
    ]
    
    var body: some View {
        NavigationStack {
        ZStack {
                Color.Sheet.background
                .ignoresSafeArea()
            
        VStack(spacing: 0) {
                    // Page content
                    TabView(selection: $currentPage) {
                        ForEach(pages.indices, id: \.self) { index in
                            RulePageView(page: pages[index])
                                .tag(index)
                        }
                    }
                    .tabViewStyle(.page(indexDisplayMode: .never))
            
                    // Page indicator
                    HStack(spacing: Design.Spacing.small) {
                        ForEach(pages.indices, id: \.self) { index in
                            Circle()
                                .fill(index == currentPage ? Color.Sheet.accent : Color.white.opacity(Design.Opacity.light))
                                .frame(width: 8, height: 8)
        }
    }
                    .padding(.vertical, Design.Spacing.medium)
                }
            }
            .navigationTitle(String(localized: "How to Play"))
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    Button(String(localized: "Done")) {
                dismiss()
                    }
                    .foregroundStyle(Color.Sheet.accent)
                }
            }
            .toolbarBackground(Color.Sheet.background, for: .navigationBar)
            .toolbarColorScheme(.dark, for: .navigationBar)
        }
    }
}

// MARK: - Rule Page Model

struct RulePage: Identifiable {
    let id = UUID()
    let title: String
    let icon: String
    let content: [String]
}

// MARK: - Rule Page View

struct RulePageView: View {
    let page: RulePage
    
    @ScaledMetric(relativeTo: .title) private var iconSize: CGFloat = Design.BaseFontSize.display
    @ScaledMetric(relativeTo: .title) private var titleSize: CGFloat = Design.BaseFontSize.title
    @ScaledMetric(relativeTo: .body) private var bodySize: CGFloat = Design.BaseFontSize.body
    
    var body: some View {
        ScrollView {
            VStack(spacing: Design.Spacing.xLarge) {
                // Icon
                Image(systemName: page.icon)
                    .font(.system(size: iconSize))
                    .foregroundStyle(Color.Sheet.accent)
                    .padding(.top, Design.Spacing.xxLarge)
                
                // Title
                Text(page.title)
                    .font(.system(size: titleSize, weight: .bold))
                .foregroundStyle(.white)
            
                // Content
        VStack(alignment: .leading, spacing: Design.Spacing.medium) {
                    ForEach(page.content.indices, id: \.self) { index in
                        HStack(alignment: .top, spacing: Design.Spacing.medium) {
                            Text("•")
                                .foregroundStyle(Color.Sheet.accent)
                            
                            Text(page.content[index])
                                .font(.system(size: bodySize))
                                .foregroundStyle(.white.opacity(Design.Opacity.heavy))
                        }
                    }
                }
                .padding(.horizontal, Design.Spacing.xxLarge)
                
                Spacer()
            }
        }
    }
}

#Preview {
    RulesHelpView()
}
