//
//  LaunchScreenView.swift
//  CasinoKit
//
//  A reusable launch screen design that can be customized for different casino games.
//

import SwiftUI

/// Configuration for the launch screen appearance.
public struct LaunchScreenConfig: Sendable {
    public let title: String
    public let subtitle: String?
    public let tagline: String?
    public let iconSymbols: [String]
    public let primaryColor: Color
    public let secondaryColor: Color
    public let accentColor: Color
    public let showLoadingIndicator: Bool
    
    public init(
        title: String,
        subtitle: String? = nil,
        tagline: String? = nil,
        iconSymbols: [String] = ["suit.spade.fill", "suit.heart.fill"],
        primaryColor: Color = Color(red: 0.05, green: 0.12, blue: 0.22),
        secondaryColor: Color = Color(red: 0.02, green: 0.06, blue: 0.12),
        accentColor: Color = .yellow,
        showLoadingIndicator: Bool = false
    ) {
        self.title = title
        self.subtitle = subtitle
        self.tagline = tagline
        self.iconSymbols = iconSymbols
        self.primaryColor = primaryColor
        self.secondaryColor = secondaryColor
        self.accentColor = accentColor
        self.showLoadingIndicator = showLoadingIndicator
    }
    
    // MARK: - Example Configuration (for previews only)
    
    /// Example configuration for CasinoKit previews.
    /// Apps should define their own configs in `BrandingConfig.swift`.
    public static let example = LaunchScreenConfig(
        title: "CASINO",
        tagline: "Your Game Tagline",
        iconSymbols: ["suit.diamond.fill", "suit.heart.fill"]
    )
}

/// A customizable launch screen view for casino games.
public struct LaunchScreenView: View {
    let config: LaunchScreenConfig
    
    @State private var logoScale: CGFloat = 0.8
    @State private var logoOpacity: Double = 0
    @State private var titleOffset: CGFloat = 20
    @State private var titleOpacity: Double = 0
    
    public init(config: LaunchScreenConfig) {
        self.config = config
    }
    
    public var body: some View {
        GeometryReader { geometry in
            ZStack {
                // Background gradient
                backgroundGradient
                
                // Pattern overlay
                patternOverlay
                    .opacity(0.05)
                
                // Decorative corner elements
                cornerDecorations(in: geometry)
                
                // Main content
                VStack(spacing: 0) {
                    Spacer()
                    
                    // Logo section
                    logoSection
                        .scaleEffect(logoScale)
                        .opacity(logoOpacity)
                    
                    Spacer()
                    
                    // Bottom tagline
                    if let tagline = config.tagline {
                        Text(tagline)
                            .font(.system(size: 14, weight: .medium, design: .rounded))
                            .foregroundStyle(.white.opacity(0.6))
                            .tracking(2)
                            .padding(.bottom, 40)
                            .offset(y: titleOffset)
                            .opacity(titleOpacity)
                    }
                    
                    // Loading indicator
                    if config.showLoadingIndicator {
                        ProgressView()
                            .progressViewStyle(.circular)
                            .tint(config.accentColor)
                            .scaleEffect(1.2)
                            .padding(.bottom, 60)
                    }
                }
            }
        }
        .ignoresSafeArea()
        .onAppear {
            withAnimation(.easeOut(duration: 0.6)) {
                logoScale = 1.0
                logoOpacity = 1.0
            }
            withAnimation(.easeOut(duration: 0.6).delay(0.3)) {
                titleOffset = 0
                titleOpacity = 1.0
            }
        }
    }
    
    // MARK: - Background
    
    private var backgroundGradient: some View {
        LinearGradient(
            colors: [
                config.primaryColor,
                config.secondaryColor
            ],
            startPoint: .top,
            endPoint: .bottom
        )
    }
    
    private var patternOverlay: some View {
        Canvas { context, size in
            let spacing: CGFloat = 40
            let diamondSize: CGFloat = 8
            let rows = Int(size.height / spacing) + 1
            let cols = Int(size.width / spacing) + 1
            
            for row in 0..<rows {
                for col in 0..<cols {
                    let offset: CGFloat = row % 2 == 0 ? 0 : spacing / 2
                    let x = CGFloat(col) * spacing + offset
                    let y = CGFloat(row) * spacing
                    
                    let diamond = Path { path in
                        path.move(to: CGPoint(x: x, y: y - diamondSize))
                        path.addLine(to: CGPoint(x: x + diamondSize, y: y))
                        path.addLine(to: CGPoint(x: x, y: y + diamondSize))
                        path.addLine(to: CGPoint(x: x - diamondSize, y: y))
                        path.closeSubpath()
                    }
                    
                    context.fill(diamond, with: .color(.white))
                }
            }
        }
    }
    
    // MARK: - Corner Decorations
    
    private func cornerDecorations(in geometry: GeometryProxy) -> some View {
        ZStack {
            // Top-left
            cornerSymbol
                .position(x: 50, y: 80)
            
            // Top-right
            cornerSymbol
                .rotationEffect(.degrees(90))
                .position(x: geometry.size.width - 50, y: 80)
            
            // Bottom-left
            cornerSymbol
                .rotationEffect(.degrees(-90))
                .position(x: 50, y: geometry.size.height - 80)
            
            // Bottom-right
            cornerSymbol
                .rotationEffect(.degrees(180))
                .position(x: geometry.size.width - 50, y: geometry.size.height - 80)
        }
        .opacity(0.15)
    }
    
    private var cornerSymbol: some View {
        Image(systemName: "suit.spade.fill")
            .font(.system(size: 30))
            .foregroundStyle(config.accentColor)
    }
    
    // MARK: - Logo Section
    
    private var logoSection: some View {
        VStack(spacing: 16) {
            // Card suit icons
            HStack(spacing: -8) {
                ForEach(config.iconSymbols.indices, id: \.self) { index in
                    Image(systemName: config.iconSymbols[index])
                        .font(.system(size: 48, weight: .bold))
                        .foregroundStyle(iconColor(for: index))
                        .shadow(color: .black.opacity(0.3), radius: 4, y: 2)
                }
            }
            
            // Subtitle (e.g., "21" for Blackjack)
            if let subtitle = config.subtitle {
                Text(subtitle)
                    .font(.system(size: 72, weight: .black, design: .rounded))
                    .foregroundStyle(
                        LinearGradient(
                            colors: [config.accentColor, .orange],
                            startPoint: .top,
                            endPoint: .bottom
                        )
                    )
                    .shadow(color: .black.opacity(0.4), radius: 4, y: 2)
            }
            
            // Title
            Text(config.title)
                .font(.system(size: 42, weight: .black, design: .rounded))
                .tracking(6)
                .foregroundStyle(
                    LinearGradient(
                        colors: [config.accentColor, .orange],
                        startPoint: .top,
                        endPoint: .bottom
                    )
                )
                .shadow(color: .black.opacity(0.4), radius: 4, y: 2)
            
            // Decorative line
            HStack(spacing: 12) {
                decorativeLine
                
                Image(systemName: "diamond.fill")
                    .font(.system(size: 10))
                    .foregroundStyle(config.accentColor.opacity(0.6))
                
                decorativeLine
            }
            .frame(width: 200)
        }
    }
    
    private func iconColor(for index: Int) -> Color {
        let symbol = config.iconSymbols[index]
        if symbol.contains("heart") || symbol.contains("diamond") {
            return .red
        }
        return .white
    }
    
    private var decorativeLine: some View {
        Rectangle()
            .fill(
                LinearGradient(
                    colors: [.clear, config.accentColor.opacity(0.4), .clear],
                    startPoint: .leading,
                    endPoint: .trailing
                )
            )
            .frame(height: 1)
    }
}

// MARK: - Static Launch Screen (for LaunchScreen.storyboard alternative)

/// A static version of the launch screen without animations.
/// Use this if you need to render a static image.
public struct StaticLaunchScreenView: View {
    let config: LaunchScreenConfig
    
    public init(config: LaunchScreenConfig) {
        self.config = config
    }
    
    public var body: some View {
        GeometryReader { geometry in
            ZStack {
                // Background
                LinearGradient(
                    colors: [config.primaryColor, config.secondaryColor],
                    startPoint: .top,
                    endPoint: .bottom
                )
                
                // Logo
                VStack(spacing: 16) {
                    HStack(spacing: -8) {
                        ForEach(config.iconSymbols.indices, id: \.self) { index in
                            let symbol = config.iconSymbols[index]
                            Image(systemName: symbol)
                                .font(.system(size: 48, weight: .bold))
                                .foregroundStyle(
                                    symbol.contains("heart") || symbol.contains("diamond") ? .red : .white
                                )
                        }
                    }
                    
                    if let subtitle = config.subtitle {
                        Text(subtitle)
                            .font(.system(size: 72, weight: .black, design: .rounded))
                            .foregroundStyle(config.accentColor)
                    }
                    
                    Text(config.title)
                        .font(.system(size: 42, weight: .black, design: .rounded))
                        .tracking(6)
                        .foregroundStyle(config.accentColor)
                }
            }
        }
        .ignoresSafeArea()
    }
}

// MARK: - Previews

#Preview("Launch Screen") {
    LaunchScreenView(config: .example)
}

#Preview("Static Launch") {
    StaticLaunchScreenView(config: .example)
}

