import 'package:flutter/material.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

/// Material Design 3 颜色系统 - "The Kinetic Vault" & "The Ethereal Terminal"
///
/// 深色主题: "The Kinetic Vault" - 赛博朋克风格
/// 浅色主题: "The Ethereal Terminal" - 高端金融科技风格
///
/// 设计原则：
/// - Material Design 3 配色方案
/// - Glass Panel 毛玻璃效果
/// - Neon Glow 霓虹光效
/// - 渐变 CTA: primary → primary_container (135度)
class AppColorScheme {
  AppColorScheme._();

  // ============================================
  // 深色主题 - "The Kinetic Vault" (Material Design 3)
  // ============================================

  /// 背景基色 - 最深
  static const Color darkBackground = Color(0xFF0b0e14);

  /// Surface 层次 (从低到高) - Material Design 3 规范
  static const Color darkSurfaceDim = Color(0xFF0b0e14);
  static const Color darkSurfaceLowest = Color(0xFF000000);
  static const Color darkSurfaceLow = Color(0xFF10131a);
  static const Color darkSurface = Color(0xFF0b0e14);
  static const Color darkSurfaceContainer = Color(0xFF161a21);
  static const Color darkSurfaceContainerHigh = Color(0xFF1c2028);
  static const Color darkSurfaceContainerHighest = Color(0xFF22262f);
  static const Color darkSurfaceBright = Color(0xFF282c36);
  static const Color darkSurfaceVariant = Color(0xFF22262f);

  /// 兼容旧名称
  static const Color darkSurfaceContainerLowest = darkSurfaceLowest;
  static const Color darkSurfaceContainerLow = darkSurfaceLow;
  static const Color darkSurfaceHigh = darkSurfaceContainerHigh;
  static const Color darkSurfaceHighest = darkSurfaceContainerHighest;

  /// Ghost Border
  static const Color darkOutline = Color(0xFF73757d);
  static const Color darkOutlineVariant = Color(0xFF45484f);

  /// Primary - Neon Cyan (青色 - 主要交互)
  static const Color darkPrimary = Color(0xFF72dcff);
  static const Color darkPrimaryDim = Color(0xFF00c3ed);
  static const Color darkPrimaryContainer = Color(0xFF00d2ff);
  static const Color darkPrimaryFixed = Color(0xFF00d2ff);
  static const Color darkPrimaryFixedDim = Color(0xFF00c3ed);
  static const Color darkOnPrimary = Color(0xFF004c5e);
  static const Color darkOnPrimaryContainer = Color(0xFF004253);
  static const Color darkOnPrimaryFixed = Color(0xFF002c38);
  static const Color darkOnPrimaryFixedVariant = Color(0xFF004c5e);

  /// Secondary - Electric Purple (紫色 - 次要强调)
  static const Color darkSecondary = Color(0xFFdd8bfb);
  static const Color darkSecondaryDim = Color(0xFFce7eec);
  static const Color darkSecondaryContainer = Color(0xFF6e208c);
  static const Color darkSecondaryFixed = Color(0xFFf2c1ff);
  static const Color darkSecondaryFixedDim = Color(0xFFebadff);
  static const Color darkOnSecondary = Color(0xFF4c0068);
  static const Color darkOnSecondaryContainer = Color(0xFFf1bfff);
  static const Color darkOnSecondaryFixed = Color(0xFF580078);
  static const Color darkOnSecondaryFixedVariant = Color(0xFF792c97);

  /// Tertiary - Neon Green (绿色 - 仅用于成功/盈利/买入)
  static const Color darkTertiary = Color(0xFFafffd1);
  static const Color darkTertiaryDim = Color(0xFF00efa0);
  static const Color darkTertiaryContainer = Color(0xFF00ffab);
  static const Color darkTertiaryFixed = Color(0xFF00ffab);
  static const Color darkTertiaryFixedDim = Color(0xFF00efa0);
  static const Color darkOnTertiary = Color(0xFF006642);
  static const Color darkOnTertiaryContainer = Color(0xFF005c3b);
  static const Color darkOnTertiaryFixed = Color(0xFF00472d);
  static const Color darkOnTertiaryFixedVariant = Color(0xFF006742);

  /// Error - Neon Red (红色 - 错误/卖出)
  static const Color darkError = Color(0xFFff716c);
  static const Color darkErrorDim = Color(0xFFd7383b);
  static const Color darkErrorContainer = Color(0xFF9f0519);
  static const Color darkOnError = Color(0xFF490006);
  static const Color darkOnErrorContainer = Color(0xFFffa8a3);

  /// 文本色
  static const Color darkOnSurface = Color(0xFFecedf6);
  static const Color darkOnSurfaceVariant = Color(0xFFa9abb3);
  static const Color darkOnSurfaceMuted = Color(0xFF6b6d75);
  static const Color darkOnBackground = Color(0xFFecedf6);
  static const Color darkInverseSurface = Color(0xFFf9f9ff);
  static const Color darkInverseOnSurface = Color(0xFF52555c);
  static const Color darkInversePrimary = Color(0xFF00687f);
  static const Color darkSurfaceTint = Color(0xFF72dcff);

  // ============================================
  // 浅色主题 - "The Ethereal Terminal"
  // ============================================

  /// 背景基色 - 珍珠白
  static const Color lightBackground = Color(0xFFf5f7f9);

  /// Surface 层次 (从低到高)
  static const Color lightSurfaceLowest = Color(0xFFffffff);
  static const Color lightSurfaceLow = Color(0xFFeef1f3);
  static const Color lightSurface = Color(0xFFf5f7f9);
  static const Color lightSurfaceHigh = Color(0xFFe8ebef);
  static const Color lightSurfaceHighest = Color(0xFFd9dde0);

  /// Ghost Border
  static const Color lightOutlineVariant = Color(0xFFc4c8cc);

  /// Primary - Plasma (主要交互)
  static const Color lightPrimary = Color(0xFF0050d4);
  static const Color lightPrimaryContainer = Color(0xFF7b9cff);

  /// Secondary - Pulse indicator
  static const Color lightSecondary = Color(0xFF8319da);
  static const Color lightSecondaryContainer = Color(0xFFe8d4fa);

  /// Tertiary - Success (买入/盈利)
  static const Color lightTertiary = Color(0xFF00a878);
  static const Color lightTertiaryContainer = Color(0xFFd4f5e9);

  /// 文本色
  static const Color lightOnSurface = Color(0xFF2c2f31);
  static const Color lightOnSurfaceVariant = Color(0xFF5a5d60);
  static const Color lightOnSurfaceMuted = Color(0xFF8a8d90);

  // ============================================
  // Glass Panel 毛玻璃效果颜色
  // ============================================

  /// Glass Panel 背景色 - rgba(34, 38, 47, 0.4)
  static const Color glassPanelBackground = Color(0x6622262f);

  /// Glass Panel 边框色 - rgba(69, 72, 79, 0.15)
  static const Color glassPanelBorder = Color(0x2645484f);

  // ============================================
  // Neon Glow 霓虹光效颜色
  // ============================================

  /// Primary Glow - rgba(114, 220, 255, 0.15)
  static const Color neonGlowPrimary = Color(0x2672dcff);

  /// Secondary Glow - rgba(221, 139, 251, 0.15)
  static const Color neonGlowSecondary = Color(0x26dd8bfb);

  /// Tertiary Glow - rgba(175, 255, 209, 0.2)
  static const Color neonGlowTertiary = Color(0x33afffd1);

  // ============================================
  // 语义色 - 明暗通用
  // ============================================

  /// 涨/买入/成功 (深色主题色，亮色模式下请使用 getUpColor)
  static const Color up = darkTertiary;
  static const Color success = darkTertiary;

  /// 跌/卖出/错误 - 使用 Material Design 3 error
  static const Color down = Color(0xFFff716c);
  static const Color error = down;

  /// 静默/禁用/次要
  static const Color muted = darkOnSurfaceVariant;

  /// 警告
  static const Color warning = Color(0xFFFF9800);

  /// 信息
  static const Color info = Color(0xFF2196F3);

  // ============================================
  // 主题感知辅助函数 - 根据明暗模式选择正确的颜色
  // ============================================

  /// 获取涨跌颜色（主题感知）
  ///
  /// 在深色模式下使用亮绿色 (#afffd1)，在亮色模式下使用深绿色 (#00a878) 以确保对比度
  static Color getUpColor(bool isDark) => isDark ? darkTertiary : lightTertiary;

  /// 获取涨跌背景色（主题感知）
  ///
  /// 带透明度的背景色，用于标签、徽章等
  static Color getUpBackgroundColor(bool isDark, {double opacity = 0.15}) {
    return isDark
        ? darkTertiary.withValues(alpha: opacity)
        : lightTertiary.withValues(alpha: opacity);
  }

  /// 获取买入按钮渐变（主题感知）
  static LinearGradient getBuyGradient(bool isDark) => LinearGradient(
        colors: isDark
            ? [darkTertiary, darkTertiaryContainer]
            : [lightTertiary, lightTertiaryContainer],
        begin: const Alignment(-0.7, -0.7),
        end: const Alignment(0.7, 0.7),
      );

  /// 获取翡翠渐变（主题感知）- 用于盈利展示
  static LinearGradient getEmeraldGradient(bool isDark) => LinearGradient(
        colors: isDark
            ? [darkTertiary, const Color(0xFF7de8b8)]
            : [lightTertiary, const Color(0xFF00c987)],
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
      );

  // ============================================
  // 渐变预设
  // ============================================

  /// 深色 CTA 渐变 (primary → primary_container, 135度)
  static const LinearGradient darkCtaGradient = LinearGradient(
    colors: [darkPrimary, darkPrimaryContainer],
    begin: Alignment(-0.7, -0.7),
    end: Alignment(0.7, 0.7),
  );

  /// 浅色 CTA 渐变 (Plasma)
  static const LinearGradient lightCtaGradient = LinearGradient(
    colors: [lightPrimary, lightPrimaryContainer],
    begin: Alignment(-0.7, -0.7),
    end: Alignment(0.7, 0.7),
  );

  /// 兼容别名
  static const LinearGradient ctaGradient = darkCtaGradient;

  /// 买入按钮渐变
  static const LinearGradient buyGradient = LinearGradient(
    colors: [darkTertiary, darkTertiaryContainer],
    begin: Alignment(-0.7, -0.7),
    end: Alignment(0.7, 0.7),
  );

  /// 卖出按钮渐变
  static const LinearGradient sellGradient = LinearGradient(
    colors: [darkError, darkErrorDim],
    begin: Alignment(-0.7, -0.7),
    end: Alignment(0.7, 0.7),
  );

  /// 资产卡片渐变
  static const LinearGradient assetCardGradient = LinearGradient(
    colors: [darkPrimary, darkSecondary],
    begin: Alignment.topLeft,
    end: Alignment.bottomRight,
  );

  // ============================================
  // Shadcn ColorScheme - 深色主题
  // ============================================

  static ShadColorScheme get darkShad => ShadColorScheme(
    background: darkBackground,
    foreground: darkOnSurface,
    card: darkSurfaceContainer,
    cardForeground: darkOnSurface,
    popover: darkSurfaceContainerHigh,
    popoverForeground: darkOnSurface,
    primary: darkPrimary,
    primaryForeground: darkOnPrimary,
    secondary: darkSecondary,
    secondaryForeground: darkOnSurface,
    muted: darkSurfaceContainerHigh,
    mutedForeground: darkOnSurfaceVariant,
    accent: darkPrimary.withValues(alpha: 0.15),
    accentForeground: darkPrimary,
    destructive: error,
    destructiveForeground: darkOnSurface,
    border: darkOutlineVariant.withValues(alpha: 0.15),
    input: darkOutlineVariant.withValues(alpha: 0.15),
    ring: darkPrimary,
    selection: darkPrimary.withValues(alpha: 0.3),
  );

  // ============================================
  // Shadcn ColorScheme - 浅色主题
  // ============================================

  static ShadColorScheme get lightShad => ShadColorScheme(
    background: lightBackground,
    foreground: lightOnSurface,
    card: lightSurfaceLowest,
    cardForeground: lightOnSurface,
    popover: lightSurfaceLowest,
    popoverForeground: lightOnSurface,
    primary: lightPrimary,
    primaryForeground: const Color(0xFFFFFFFF),
    secondary: lightSecondary,
    secondaryForeground: lightOnSurface,
    muted: lightSurfaceHigh,
    mutedForeground: lightOnSurfaceVariant,
    accent: lightPrimary.withValues(alpha: 0.1),
    accentForeground: lightPrimary,
    destructive: error,
    destructiveForeground: const Color(0xFFFFFFFF),
    border: lightOutlineVariant.withValues(alpha: 0.5),
    input: lightOutlineVariant.withValues(alpha: 0.3),
    ring: lightPrimary,
    selection: lightPrimary.withValues(alpha: 0.2),
  );

  // ============================================
  // Material ColorScheme - 深色主题 (Material Design 3)
  // ============================================

  static ColorScheme get darkMaterial => ColorScheme.dark(
    primary: darkPrimary,
    onPrimary: darkOnPrimary,
    primaryContainer: darkPrimaryContainer,
    onPrimaryContainer: darkOnPrimaryContainer,
    secondary: darkSecondary,
    onSecondary: darkOnSecondary,
    secondaryContainer: darkSecondaryContainer,
    onSecondaryContainer: darkOnSecondaryContainer,
    tertiary: darkTertiary,
    onTertiary: darkOnTertiary,
    tertiaryContainer: darkTertiaryContainer,
    onTertiaryContainer: darkOnTertiaryContainer,
    error: darkError,
    onError: darkOnError,
    errorContainer: darkErrorContainer,
    onErrorContainer: darkOnErrorContainer,
    surface: darkSurface,
    onSurface: darkOnSurface,
    surfaceDim: darkSurfaceDim,
    surfaceBright: darkSurfaceBright,
    surfaceContainerLowest: darkSurfaceLowest,
    surfaceContainerLow: darkSurfaceLow,
    surfaceContainer: darkSurfaceContainer,
    surfaceContainerHigh: darkSurfaceContainerHigh,
    surfaceContainerHighest: darkSurfaceContainerHighest,
    onSurfaceVariant: darkOnSurfaceVariant,
    outline: darkOutline,
    outlineVariant: darkOutlineVariant,
    inverseSurface: darkInverseSurface,
    onInverseSurface: darkInverseOnSurface,
    inversePrimary: darkInversePrimary,
    surfaceTint: darkSurfaceTint,
  );

  // ============================================
  // Material ColorScheme - 浅色主题
  // ============================================

  /// 浅色主题 Error 色
  static const Color lightError = Color(0xFFd7383b);
  static const Color lightOnError = Color(0xFFFFFFFF);

  /// 浅色主题 Outline 色
  static const Color lightOutline = Color(0xFF73757d);

  /// 浅色主题 Surface 色 (扩展)
  static const Color lightSurfaceBright = Color(0xFFffffff);
  static const Color lightSurfaceDim = Color(0xFFe8ebef);
  static const Color lightSurfaceVariant = Color(0xFFe8ebef);
  static const Color lightSurfaceContainer = Color(0xFFf0f2f4);
  static const Color lightSurfaceContainerHigh = Color(0xFFe8ebef);
  static const Color lightSurfaceContainerHighest = Color(0xFFd9dde0);

  static ColorScheme get lightMaterial => ColorScheme.light(
    primary: lightPrimary,
    onPrimary: const Color(0xFFFFFFFF),
    primaryContainer: lightPrimaryContainer,
    onPrimaryContainer: lightOnSurface,
    secondary: lightSecondary,
    onSecondary: const Color(0xFFFFFFFF),
    secondaryContainer: lightSecondaryContainer,
    onSecondaryContainer: lightOnSurface,
    tertiary: lightTertiary,
    onTertiary: const Color(0xFFFFFFFF),
    tertiaryContainer: lightTertiaryContainer,
    onTertiaryContainer: lightOnSurface,
    error: lightError,
    onError: lightOnError,
    errorContainer: const Color(0xFFffeaea),
    onErrorContainer: const Color(0xFF490006),
    surface: lightSurface,
    onSurface: lightOnSurface,
    surfaceDim: lightSurfaceDim,
    surfaceBright: lightSurfaceBright,
    surfaceContainerLowest: lightSurfaceLowest,
    surfaceContainerLow: lightSurfaceLow,
    surfaceContainer: lightSurfaceContainer,
    surfaceContainerHigh: lightSurfaceContainerHigh,
    surfaceContainerHighest: lightSurfaceContainerHighest,
    onSurfaceVariant: lightOnSurfaceVariant,
    outline: lightOutline,
    outlineVariant: lightOutlineVariant,
    inverseSurface: const Color(0xFF1c2028),
    onInverseSurface: const Color(0xFFecedf6),
    inversePrimary: darkPrimary,
    surfaceTint: lightPrimary,
  );

  // ============================================
  // 兼容性常量 (已废弃，保留向后兼容)
  // ============================================

  @Deprecated('Use darkPrimary instead')
  static const Color primaryDark = darkPrimary;

  @Deprecated('Use lightPrimary instead')
  static const Color primaryLight = lightPrimary;

  @Deprecated('Use darkBackground instead')
  static const Color _darkBackground = darkBackground;

  @Deprecated('Use darkSurfaceContainer instead')
  static const Color _darkCardBackground = darkSurfaceContainer;

  @Deprecated('Use darkSurfaceContainerHigh instead')
  static const Color _darkSecondary = darkSurfaceContainerHigh;

  @Deprecated('Use darkSurfaceContainerHigh instead')
  static const Color _darkMuted = darkSurfaceContainerHigh;

  @Deprecated('Use darkOutlineVariant instead')
  static const Color _darkBorder = darkOutlineVariant;

  @Deprecated('Use darkOnSurface instead')
  static const Color _darkTextPrimary = darkOnSurface;

  @Deprecated('Use darkOnSurfaceVariant instead')
  static const Color _darkTextSecondary = darkOnSurfaceVariant;

  @Deprecated('Use darkOnSurfaceMuted instead')
  static const Color _darkTextHint = darkOnSurfaceMuted;

  @Deprecated('Use lightBackground instead')
  static const Color _lightBackground = lightBackground;

  @Deprecated('Use lightSurfaceLowest instead')
  static const Color _lightCardBackground = lightSurfaceLowest;

  @Deprecated('Use lightSurfaceHigh instead')
  static const Color _lightSecondary = lightSurfaceHigh;

  @Deprecated('Use lightSurfaceHigh instead')
  static const Color _lightMuted = lightSurfaceHigh;

  @Deprecated('Use lightOutlineVariant instead')
  static const Color _lightBorder = lightOutlineVariant;

  @Deprecated('Use lightOnSurface instead')
  static const Color _lightTextPrimary = lightOnSurface;

  @Deprecated('Use lightOnSurfaceVariant instead')
  static const Color _lightTextSecondary = lightOnSurfaceVariant;

  @Deprecated('Use lightOnSurfaceMuted instead')
  static const Color _lightTextHint = lightOnSurfaceMuted;
}

/// 创建 Shadcn 深色主题
ShadThemeData createDarkShadTheme() {
  return ShadThemeData(
    brightness: Brightness.dark,
    colorScheme: AppColorScheme.darkShad,
  );
}

/// 创建 Shadcn 浅色主题
ShadThemeData createLightShadTheme() {
  return ShadThemeData(
    brightness: Brightness.light,
    colorScheme: AppColorScheme.lightShad,
  );
}

/// 获取涨跌颜色（明暗通用）- 已废弃，请使用带主题感知的版本
@Deprecated('Use AppColorScheme.getUpColor(isDark) instead')
Color getChangeColor(bool isUp) => isUp ? AppColorScheme.up : AppColorScheme.down;

/// 获取涨跌背景色（带透明度）- 已废弃，请使用带主题感知的版本
@Deprecated('Use AppColorScheme.getUpBackgroundColor(isDark, opacity: opacity) instead')
Color getChangeBackgroundColor(bool isUp, {double opacity = 0.15}) {
  return isUp
    ? AppColorScheme.up.withValues(alpha: opacity)
    : AppColorScheme.down.withValues(alpha: opacity);
}

/// 获取涨跌颜色（主题感知）
Color getChangeColorThemeAware(bool isUp, bool isDark) =>
    isUp ? AppColorScheme.getUpColor(isDark) : AppColorScheme.down;

/// 获取涨跌背景色（主题感知）
Color getChangeBackgroundColorThemeAware(bool isUp, bool isDark, {double opacity = 0.15}) {
  return isUp
      ? AppColorScheme.getUpBackgroundColor(isDark, opacity: opacity)
      : AppColorScheme.down.withValues(alpha: opacity);
}
