//
//  AppIconView.swift
//  CasinoKit
//
//  A reusable app icon design that can be customized for different casino games.
//  Render this view to an image for use as your app icon.
//

import SwiftUI

/// Configuration for the app icon appearance.
public struct AppIconConfig: Sendable {
    public let title: String
    public let subtitle: String?
    public let iconSymbol: String
    public let primaryColor: Color
    public let secondaryColor: Color
    public let accentColor: Color
    
    public init(
        title: String,
        subtitle: String? = nil,
        iconSymbol: String,
        primaryColor: Color = Color(red: 0.1, green: 0.2, blue: 0.35),
        secondaryColor: Color = Color(red: 0.05, green: 0.12, blue: 0.25),
        accentColor: Color = .yellow
    ) {
        self.title = title
        self.subtitle = subtitle
        self.iconSymbol = iconSymbol
        self.primaryColor = primaryColor
        self.secondaryColor = secondaryColor
        self.accentColor = accentColor
    }
    
    // MARK: - Preset Configurations
    
    /// Baccarat game icon configuration.
    public static let baccarat = AppIconConfig(
        title: "BACCARAT",
        iconSymbol: "suit.spade.fill"
    )
    
    /// Blackjack game icon configuration.
    public static let blackjack = AppIconConfig(
        title: "BLACKJACK",
        subtitle: "21",
        iconSymbol: "suit.club.fill",
        primaryColor: Color(red: 0.05, green: 0.35, blue: 0.15),
        secondaryColor: Color(red: 0.03, green: 0.2, blue: 0.1)
    )
    
    /// Poker game icon configuration.
    public static let poker = AppIconConfig(
        title: "POKER",
        iconSymbol: "suit.diamond.fill",
        accentColor: .red
    )
    
    /// Roulette game icon configuration.
    public static let roulette = AppIconConfig(
        title: "ROULETTE",
        iconSymbol: "circle.grid.3x3.fill",
        primaryColor: Color(red: 0.4, green: 0.1, blue: 0.1),
        secondaryColor: Color(red: 0.25, green: 0.05, blue: 0.05)
    )
}

/// A customizable app icon view for casino games.
/// Render this view to create your app icon assets.
public struct AppIconView: View {
    let config: AppIconConfig
    let size: CGFloat
    
    public init(config: AppIconConfig, size: CGFloat = 1024) {
        self.config = config
        self.size = size
    }
    
    private var cornerRadius: CGFloat { size * 0.22 }
    private var iconSize: CGFloat { size * 0.35 }
    private var titleSize: CGFloat { size * 0.12 }
    private var subtitleSize: CGFloat { size * 0.25 }
    private var borderWidth: CGFloat { size * 0.02 }
    
    public var body: some View {
        ZStack {
            // Background gradient
            RoundedRectangle(cornerRadius: cornerRadius)
                .fill(
                    LinearGradient(
                        colors: [config.primaryColor, config.secondaryColor],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    )
                )
            
            // Subtle pattern overlay
            DiamondPatternOverlay(size: size)
                .opacity(0.08)
            
            // Gold border
            RoundedRectangle(cornerRadius: cornerRadius)
                .strokeBorder(
                    LinearGradient(
                        colors: [
                            config.accentColor,
                            config.accentColor.opacity(0.6),
                            config.accentColor
                        ],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    ),
                    lineWidth: borderWidth
                )
            
            // Content
            VStack(spacing: size * 0.03) {
                // Icon symbol
                Image(systemName: config.iconSymbol)
                    .font(.system(size: iconSize, weight: .bold))
                    .foregroundStyle(
                        LinearGradient(
                            colors: [config.accentColor, config.accentColor.opacity(0.8)],
                            startPoint: .top,
                            endPoint: .bottom
                        )
                    )
                    .shadow(color: .black.opacity(0.3), radius: size * 0.02, y: size * 0.01)
                
                // Subtitle (e.g., "21" for Blackjack)
                if let subtitle = config.subtitle {
                    Text(subtitle)
                        .font(.system(size: subtitleSize, weight: .black, design: .rounded))
                        .foregroundStyle(
                            LinearGradient(
                                colors: [config.accentColor, .orange],
                                startPoint: .top,
                                endPoint: .bottom
                            )
                        )
                        .shadow(color: .black.opacity(0.5), radius: size * 0.01)
                }
                
                // Title
                Text(config.title)
                    .font(.system(size: titleSize, weight: .black, design: .rounded))
                    .tracking(size * 0.005)
                    .foregroundStyle(
                        LinearGradient(
                            colors: [.white, .white.opacity(0.85)],
                            startPoint: .top,
                            endPoint: .bottom
                        )
                    )
                    .shadow(color: .black.opacity(0.5), radius: size * 0.01)
            }
        }
        .frame(width: size, height: size)
    }
}

/// Diamond pattern overlay for the icon background.
private struct DiamondPatternOverlay: View {
    let size: CGFloat
    
    private var spacing: CGFloat { size * 0.08 }
    private var diamondSize: CGFloat { size * 0.03 }
    
    var body: some View {
        Canvas { context, canvasSize in
            let rows = Int(canvasSize.height / spacing) + 1
            let cols = Int(canvasSize.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: - Preview

#Preview("Baccarat Icon") {
    AppIconView(config: .baccarat, size: 512)
        .padding()
        .background(Color.gray)
}

#Preview("Blackjack Icon") {
    AppIconView(config: .blackjack, size: 512)
        .padding()
        .background(Color.gray)
}

#Preview("All Icons") {
    HStack(spacing: 20) {
        AppIconView(config: .baccarat, size: 200)
        AppIconView(config: .blackjack, size: 200)
        AppIconView(config: .poker, size: 200)
        AppIconView(config: .roulette, size: 200)
    }
    .padding()
    .background(Color.gray)
}

