//
//  CasinoDesign.swift
//  CasinoKit
//
//  Shared design constants for casino game UI components.
//

import SwiftUI

/// Shared design constants for casino game components.
public enum CasinoDesign {
    
    // MARK: - Debug
    
    /// Set to true to enable debug logging in CasinoKit.
    public static let showDebugLogs = true
    
    /// Logs a message only in debug builds when `showDebugLogs` is enabled.
    public static func debugLog(_ message: String) {
        #if DEBUG
        if showDebugLogs {
            print("[CasinoKit] \(message)")
        }
        #endif
    }
    
    // MARK: - Spacing
    
    public enum Spacing {
        public static let xxxSmall: CGFloat = 1
        public static let xxSmall: CGFloat = 2
        public static let xSmall: CGFloat = 4
        public static let small: CGFloat = 8
        public static let medium: CGFloat = 12
        public static let large: CGFloat = 16
        public static let xLarge: CGFloat = 20
        public static let xxLarge: CGFloat = 24
        public static let xxxLarge: CGFloat = 32
        
        /// Slide distance for toast animations
        public static let toastSlide: CGFloat = 50
    }
    
    // MARK: - Corner Radius
    
    public enum CornerRadius {
        public static let xSmall: CGFloat = 4
        public static let small: CGFloat = 8
        public static let medium: CGFloat = 10
        public static let large: CGFloat = 12
        public static let xLarge: CGFloat = 14
        public static let xxLarge: CGFloat = 20
        public static let xxxLarge: CGFloat = 28
    }
    
    // MARK: - Line Width
    
    public enum LineWidth {
        public static let thin: CGFloat = 1
        public static let standard: CGFloat = 2
        public static let medium: CGFloat = 2
        public static let thick: CGFloat = 3
        public static let heavy: CGFloat = 4
    }
    
    // MARK: - Shadow
    
    public enum Shadow {
        public static let radiusSmall: CGFloat = 4
        public static let radiusMedium: CGFloat = 8
        public static let radiusLarge: CGFloat = 12
        public static let radiusXLarge: CGFloat = 16
        public static let radiusXXLarge: CGFloat = 24
        
        public static let offsetSmall: CGFloat = 1
        public static let offsetMedium: CGFloat = 2
        public static let offsetLarge: CGFloat = 3
    }
    
    // MARK: - Opacity
    
    public enum Opacity {
        public static let verySubtle: Double = 0.05
        public static let subtle: Double = 0.1
        public static let selection: Double = 0.15
        public static let hint: Double = 0.2
        public static let quarter: Double = 0.25
        public static let light: Double = 0.3
        public static let overlay: Double = 0.4
        public static let medium: Double = 0.5
        public static let secondary: Double = 0.5
        public static let disabled: Double = 0.5
        public static let accent: Double = 0.6
        public static let strong: Double = 0.7
        public static let heavy: Double = 0.8
        public static let nearOpaque: Double = 0.85
        public static let almostFull: Double = 0.9
    }
    
    // MARK: - Animation
    
    public enum Animation {
        public static let quick: Double = 0.3
        public static let standard: Double = 0.3
        public static let springDuration: Double = 0.4
        public static let springBounce: Double = 0.3
        public static let cardFlipBounce: Double = 0.2
        public static let fadeInDuration: Double = 0.3
        public static let cardFlipDuration: Double = 0.5
        public static let selectionDuration: Double = 0.2
        public static let staggerDelay1: Double = 0.1
        public static let staggerDelay2: Double = 0.25
        public static let staggerDelay3: Double = 0.4
        
        /// Creates an easeOut animation for card dealing.
        /// Cards decelerate as they arrive, simulating friction—more realistic than spring bounce.
        /// - Parameter speed: Speed multiplier (1.0 = normal, lower = faster)
        public static func cardDeal(speed: Double = 1.0) -> SwiftUI.Animation {
            .easeOut(duration: springDuration * speed)
        }
    }
    
    // MARK: - Dealing Speed
    
    /// Speed multipliers for card dealing animations.
    /// Lower values = faster dealing. Multiplied by `Animation.springDuration`.
    public enum DealingSpeed {
        /// Fast dealing (snappy, quick deal)
        public static let fast: Double = 0.4
        /// Normal dealing speed
        public static let normal: Double = 0.6
        /// Slow dealing (relaxed pace)
        public static let slow: Double = 0.8
    }
    
    // MARK: - Sizes
    
    public enum Size {
        // MARK: - Chip Sizes
        
        /// Default chip size for selectors.
        public static let chipSmall: CGFloat = 36
        public static let chipMedium: CGFloat = 50
        public static let chipLarge: CGFloat = 60
        
        /// Default card widths.
        public static let cardWidthSmall: CGFloat = 55
        public static let cardWidthMedium: CGFloat = 65
        public static let cardWidthLarge: CGFloat = 74
        public static let cardWidth: CGFloat = 80
        
        /// Card overlap for stacking.
        public static let cardOverlap: CGFloat = -15
        
        /// Card aspect ratio (height = width * this value).
        /// Standard poker is 1.4, but 1.35 looks better on screen.
        public static let cardAspectRatio: CGFloat = 1.4
        
        /// Pattern dimensions for decorative elements.
        public static let patternSpacing: CGFloat = 12
        public static let patternDiamondSize: CGFloat = 6
        public static let dashLength: CGFloat = 8
        public static let dashGap: CGFloat = 4
        
        /// Chip edge stripe dimensions.
        public static let chipStripeWidth: CGFloat = 4
        public static let chipStripeInset: CGFloat = 2
        
        /// iPad max widths for overlays and content.
        public static let maxContentWidthPortrait: CGFloat = 500
        public static let maxContentWidthLandscape: CGFloat = 800
        public static let maxModalWidth: CGFloat = 450
        
        /// Value badge size.
        public static let valueBadge: CGFloat = 26
        
        /// Chip badge for bet indicators.
        public static let chipBadge: CGFloat = 32
        public static let chipBadgeInner: CGFloat = 28
        
        /// Checkmark size.
        public static let checkmark: CGFloat = 22
        
        /// Minimum height for tappable action rows (Apple HIG: 44pt minimum touch target).
        public static let actionRowMinHeight: CGFloat = 44
        
        /// Common button dimensions.
        public static let actionButtonHeight: CGFloat = 50
        public static var actionButtonMinWidth: CGFloat {
            if let width = UIScreen.current?.bounds.width {
                return width < 400 ? 35.0 : 50.0
            } else {
                return 35.0
            }
        }
        
        /// Betting zone height.
        public static let bettingZoneHeight: CGFloat = 70
    }
    
    // MARK: - Icon Sizes
    
    public enum IconSize {
        public static let small: CGFloat = 12
        public static let medium: CGFloat = 16
        public static let large: CGFloat = 22
        public static let xLarge: CGFloat = 32
        public static let xxLarge: CGFloat = 60
        public static let xxxLarge: CGFloat = 70
    }
    
    // MARK: - Font Sizes (Base values for @ScaledMetric)
    
    public enum BaseFontSize {
        public static let xxSmall: CGFloat = 7
        public static let xSmall: CGFloat = 9
        public static let small: CGFloat = 10
        public static let body: CGFloat = 12
        public static let callout: CGFloat = 13
        public static let medium: CGFloat = 14
        public static let subheadline: CGFloat = 15
        public static let large: CGFloat = 16
        public static let xLarge: CGFloat = 18
        public static let xxLarge: CGFloat = 20
        public static let title: CGFloat = 32
        public static let largeTitle: CGFloat = 36
        public static let display: CGFloat = 70
    }
    
    // MARK: - Scale
    
    public enum Scale {
        public static let shrunk: CGFloat = 0.5
        public static let slightShrink: CGFloat = 0.8
        public static let normal: CGFloat = 1.0
        public static let selected: CGFloat = 1.1
    }
    
    // MARK: - Minimum Scale Factors
    
    public enum MinScaleFactor {
        public static let tight: CGFloat = 0.5
        public static let comfortable: CGFloat = 0.6
        public static let relaxed: CGFloat = 0.7
    }
    
    // MARK: - Max Chip Stack
    
    public enum ChipStack {
        public static let maxChipsToShow: Int = 5
    }
    
    // MARK: - Toast Configuration
    
    public enum Toast {
        /// Duration toasts stay visible before auto-dismiss.
        public static let duration: Duration = .seconds(3)
    }
    
    // MARK: - Hint Sizes
    
    public enum HintSize {
        public static let iconSize: CGFloat = 16
        public static let fontSize: CGFloat = 13
        public static let paddingH: CGFloat = 12
        public static let paddingV: CGFloat = 8
        public static let minWidth: CGFloat = 180
    }
}

// MARK: - CasinoKit Colors

/// Shared colors for casino game UI components.
public extension Color {
    
    /// Sheet and popup colors.
    enum Sheet {
        /// Dark background for sheets and popups.
        public static let background = Color(red: 0.08, green: 0.12, blue: 0.18)
        
        /// Subtle fill for section cards.
        public static let sectionFill = Color.white.opacity(CasinoDesign.Opacity.subtle)
        
        /// Card background in settings/sheets.
        public static let cardBackground = Color.white.opacity(CasinoDesign.Opacity.verySubtle)
        
        /// Accent color for buttons and highlights (gold).
        public static let accent = Color(red: 0.9, green: 0.75, blue: 0.3)
        
        /// Secondary text color.
        public static let secondaryText = Color.white.opacity(CasinoDesign.Opacity.accent)
        
        /// Cancel button color.
        public static let cancelText = Color.white.opacity(CasinoDesign.Opacity.strong)
    }
    
    /// Button colors (gold gradient, destructive, etc.).
    enum CasinoButton {
        /// Light gold for button gradients.
        public static let goldLight = Color(red: 1.0, green: 0.85, blue: 0.3)
        /// Dark gold for button gradients.
        public static let goldDark = Color(red: 0.9, green: 0.7, blue: 0.2)
        /// Destructive button color (red).
        public static let destructive = Color.red.opacity(0.8)
    }
    
    /// Modal overlay colors.
    enum CasinoModal {
        /// Light background for modal cards.
        public static let backgroundLight = Color(white: 0.15)
        /// Dark background for modal cards.
        public static let backgroundDark = Color(white: 0.08)
    }
    
    /// Table colors - shared across all casino games.
    enum CasinoTable {
        /// Casino table green felt (main color).
        public static let felt = Color(red: 0.0, green: 0.3, blue: 0.15)
        /// Darker felt for gradients and shadows.
        public static let feltDark = Color(red: 0.0, green: 0.2, blue: 0.1)
        /// Lighter felt for highlights.
        public static let feltLight = Color(red: 0.0, green: 0.35, blue: 0.18)
        /// Background gradient dark.
        public static let backgroundDark = Color(red: 0.01, green: 0.12, blue: 0.06)
        /// Background gradient light.
        public static let backgroundLight = Color(red: 0.03, green: 0.25, blue: 0.12)
        /// Base dark (for table edges).
        public static let baseDark = Color(red: 0.02, green: 0.15, blue: 0.08)
        /// Preview background (same as felt).
        public static let preview = felt
        /// Table edge border.
        public static let border = Color(red: 0.6, green: 0.5, blue: 0.3)
        /// Gold accent for table elements.
        public static let gold = Color(red: 0.85, green: 0.65, blue: 0.2)
        /// Gold light for highlights.
        public static let goldLight = Color(red: 0.85, green: 0.7, blue: 0.35)
        /// Gold dark for gradients.
        public static let goldDark = Color(red: 0.65, green: 0.5, blue: 0.2)
    }
    
    /// Top bar colors.
    enum CasinoTopBar {
        /// Balance text color.
        public static let balanceText = Color.yellow
        /// Secondary info color.
        public static let secondaryText = Color.white.opacity(CasinoDesign.Opacity.medium)
        /// Icon button color.
        public static let iconButton = Color.white.opacity(CasinoDesign.Opacity.strong)
    }
}

import UIKit

extension UIScreen {
    /// Returns the current active screen (the one displaying the key window).
    /// This is the recommended replacement for the deprecated `UIScreen.main`.
    static var current: UIScreen? {
        // Find the active foreground scene
        UIApplication.shared.connectedScenes
            .compactMap { $0 as? UIWindowScene }
            .filter { $0.activationState == .foregroundActive }
            .first?
            .screen
        
        // Alternative (more robust for key window):
        // UIApplication.shared.connectedScenes
        //     .compactMap { $0 as? UIWindowScene }
        //     .flatMap { $0.windows }
        //     .first { $0.isKeyWindow }?
        //     .screen
    }
}
