//
//  AppLaunchView.swift
//  CasinoKit
//
//  A wrapper view that shows an animated launch screen before transitioning
//  to the main app content. Use this to create seamless animated launches.
//

import SwiftUI

/// A wrapper that shows an animated launch screen before the main content.
///
/// Usage:
/// ```swift
/// AppLaunchView(config: .baccarat) {
///     ContentView()
/// }
/// ```
public struct AppLaunchView<Content: View>: View {
    let config: LaunchScreenConfig
    let content: () -> Content
    
    @State private var showLaunchScreen = true
    
    /// Creates a launch wrapper.
    /// - Parameters:
    ///   - config: The launch screen configuration.
    ///   - content: The main app content to show after the launch animation.
    public init(
        config: LaunchScreenConfig,
        @ViewBuilder content: @escaping () -> Content
    ) {
        self.config = config
        self.content = content
    }
    
    public var body: some View {
        ZStack {
            // Main content (always rendered underneath)
            content()
            
            // Launch screen overlay
            if showLaunchScreen {
                LaunchScreenView(config: config)
                    .transition(.opacity)
                    .zIndex(1)
            }
        }
        .task {
            // Wait for launch animation to complete, then fade out
            try? await Task.sleep(for: .seconds(2.0))
            withAnimation(.easeOut(duration: 0.5)) {
                showLaunchScreen = false
            }
        }
    }
}

#Preview {
    AppLaunchView(config: .example) {
        Text("Main App Content")
            .font(.largeTitle)
    }
}
