//
//  IconRenderer.swift
//  CasinoKit
//
//  Utility to render SwiftUI views to images for app icons.
//

import SwiftUI

/// Utility to render SwiftUI views to images.
@MainActor
public struct IconRenderer {
    
    /// Standard iOS app icon sizes.
    public static let iOSIconSizes: [CGFloat] = [
        1024,  // App Store
        180,   // iPhone @3x
        120,   // iPhone @2x
        167,   // iPad Pro @2x
        152,   // iPad @2x
        76,    // iPad @1x
        40,    // Spotlight @2x
        60,    // Spotlight @3x
        29,    // Settings @1x
        58,    // Settings @2x
        87     // Settings @3x
    ]
    
    /// Renders an app icon view to a UIImage.
    /// - Parameters:
    ///   - config: The app icon configuration.
    ///   - size: The size to render at (default 1024 for App Store).
    /// - Returns: A rendered UIImage.
    public static func renderAppIcon(config: AppIconConfig, size: CGFloat = 1024) -> UIImage? {
        let view = AppIconView(config: config, size: size)
        let renderer = ImageRenderer(content: view)
        renderer.scale = 1.0
        return renderer.uiImage
    }
    
    /// Renders app icons at all standard iOS sizes.
    /// - Parameter config: The app icon configuration.
    /// - Returns: Dictionary of size to UIImage.
    public static func renderAllSizes(config: AppIconConfig) -> [CGFloat: UIImage] {
        var images: [CGFloat: UIImage] = [:]
        for size in iOSIconSizes {
            if let image = renderAppIcon(config: config, size: size) {
                images[size] = image
            }
        }
        return images
    }
    
    /// Renders a launch screen to a UIImage.
    /// - Parameters:
    ///   - config: The launch screen configuration.
    ///   - size: The size to render at.
    /// - Returns: A rendered UIImage.
    public static func renderLaunchScreen(config: LaunchScreenConfig, size: CGSize) -> UIImage? {
        let view = StaticLaunchScreenView(config: config)
            .frame(width: size.width, height: size.height)
        let renderer = ImageRenderer(content: view)
        renderer.scale = 1.0
        return renderer.uiImage
    }
}

// MARK: - Icon Export View

/// A development view for previewing and exporting app icons.
/// Add this to your app during development to easily export icons.
public struct IconExportView: View {
    let config: AppIconConfig
    @State private var exportedMessage: String?
    
    public init(config: AppIconConfig) {
        self.config = config
    }
    
    public var body: some View {
        ScrollView {
            VStack(spacing: 24) {
                Text("App Icon Preview")
                    .font(.title.bold())
                
                // Large preview
                AppIconView(config: config, size: 256)
                    .clipShape(.rect(cornerRadius: 256 * 0.22))
                    .shadow(radius: 10)
                
                // Size variants
                Text("Size Variants")
                    .font(.headline)
                
                LazyVGrid(columns: [
                    GridItem(.adaptive(minimum: 100))
                ], spacing: 16) {
                    ForEach([180, 120, 87, 60, 40], id: \.self) { size in
                        VStack {
                            AppIconView(config: config, size: CGFloat(size))
                                .clipShape(.rect(cornerRadius: CGFloat(size) * 0.22))
                            Text("\(size)pt")
                                .font(.caption)
                                .foregroundStyle(.secondary)
                        }
                    }
                }
                
                // Export instructions
                Text("Export Instructions")
                    .font(.headline)
                    .padding(.top)
                
                VStack(alignment: .leading, spacing: 8) {
                    Text("1. Use Xcode's preview to screenshot these icons")
                    Text("2. Or use IconRenderer.renderAppIcon() in code")
                    Text("3. Add generated images to Assets.xcassets/AppIcon")
                }
                .font(.callout)
                .foregroundStyle(.secondary)
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding()
                .background(Color.gray.opacity(0.1))
                .clipShape(.rect(cornerRadius: 12))
                
                if let message = exportedMessage {
                    Text(message)
                        .foregroundStyle(.green)
                }
            }
            .padding()
        }
    }
}

#Preview("Icon Export") {
    IconExportView(config: .baccarat)
}

