import 'dart:io' show Platform;

import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';

/// Windows平台检测与Fluent UI主题工具
class PlatformUtils {
  /// Web平台不支持dart:io，需先排除
  static bool get isWindows => !kIsWeb && Platform.isWindows;

  /// 根据当前Material主题生成对应的FluentThemeData
  static fluent.FluentThemeData getFluentTheme(BuildContext context) {
    final materialTheme = Theme.of(context);
    final brightness = materialTheme.brightness;
    final primary = materialTheme.colorScheme.primary;

    // 根据primary color构建AccentColor渐变色阶
    final accent = fluent.AccentColor.swatch({
      'darkest': _darken(primary, 0.4),
      'darker': _darken(primary, 0.2),
      'dark': _darken(primary, 0.1),
      'normal': primary,
      'light': _lighten(primary, 0.15),
      'lighter': _lighten(primary, 0.3),
      'lightest': _lighten(primary, 0.5),
    });

    return fluent.FluentThemeData(
      brightness: brightness,
      accentColor: accent,
      scaffoldBackgroundColor: brightness == Brightness.dark
          ? const Color(0xff202020)
          : const Color(0xfff3f3f3),
    );
  }

  static Color _darken(Color color, double amount) {
    final hsl = HSLColor.fromColor(color);
    return hsl
        .withLightness((hsl.lightness - amount).clamp(0.0, 1.0))
        .toColor();
  }

  static Color _lighten(Color color, double amount) {
    final hsl = HSLColor.fromColor(color);
    return hsl
        .withLightness((hsl.lightness + amount).clamp(0.0, 1.0))
        .toColor();
  }
}
