import 'package:flutter/material.dart';

import 'ui_colors.dart';
import 'ui_constants.dart';
import 'ui_typography.dart';

/// The main entry point for the Staff Design System theme.
/// Assembles colors, typography, and constants into a comprehensive Material 3 theme.
///
/// Adheres to the tokens defined in design_tokens_react.md.
class UiTheme {
  UiTheme._();

  /// Returns the light theme for the Staff application.
  static ThemeData get light {
    final ColorScheme colorScheme = UiColors.colorScheme;
    final TextTheme textTheme = UiTypography.textTheme;

    return ThemeData(
      useMaterial3: true,
      colorScheme: colorScheme,
      scaffoldBackgroundColor: UiColors.background,
      primaryColor: UiColors.primary,
      canvasColor: UiColors.background,

      // Typography
      textTheme: textTheme,

      // Icon Theme
      iconTheme: const IconThemeData(color: UiColors.iconPrimary, size: 24),

      // Text Selection Theme
      textSelectionTheme: const TextSelectionThemeData(
        cursorColor: UiColors.primary,
        selectionColor: UiColors.primaryInverse,
        selectionHandleColor: UiColors.primary,
      ),

      // Divider Theme
      dividerTheme: const DividerThemeData(
        color: UiColors.separatorPrimary,
        space: 1,
        thickness: 0.5,
      ),

      // Card Theme
      cardTheme: CardThemeData(
        color: UiColors.white,
        elevation: 2,
        shadowColor: UiColors.popupShadow,
        shape: RoundedRectangleBorder(
          borderRadius: UiConstants.radiusLg,
          side: const BorderSide(color: UiColors.borderStill),
        ),
        margin: EdgeInsets.zero,
      ),

      // Elevated Button Theme (Primary)
      elevatedButtonTheme: ElevatedButtonThemeData(
        style:
            ElevatedButton.styleFrom(
              elevation: 0,
              backgroundColor: UiColors.buttonPrimaryStill,
              foregroundColor: UiColors.primaryForeground,
              disabledBackgroundColor: UiColors.buttonPrimaryInactive,
              textStyle: UiTypography.buttonXL,
              shape: RoundedRectangleBorder(borderRadius: UiConstants.radiusLg),
              padding: const EdgeInsets.symmetric(
                horizontal: UiConstants.space6,
                vertical: UiConstants.space3,
              ),
              maximumSize: const Size(double.infinity, 54),
            ).copyWith(
              side: WidgetStateProperty.resolveWith<BorderSide?>((Set<WidgetState> states) {
                if (states.contains(WidgetState.disabled)) {
                  return const BorderSide(
                    color: UiColors.borderPrimary,
                    width: 0.5,
                  );
                }
                return null;
              }),
              overlayColor: WidgetStateProperty.resolveWith((Set<WidgetState> states) {
                if (states.contains(WidgetState.hovered))
                  return UiColors.buttonPrimaryHover;
                return null;
              }),
            ),
      ),

      // Text Button Theme
      textButtonTheme: TextButtonThemeData(
        style: TextButton.styleFrom(
          foregroundColor: UiColors.textPrimary,
          disabledForegroundColor: UiColors.textInactive,
          textStyle: UiTypography.buttonXL,
          shape: RoundedRectangleBorder(borderRadius: UiConstants.radiusLg),
          padding: const EdgeInsets.symmetric(
            horizontal: UiConstants.space4,
            vertical: UiConstants.space2,
          ),
          maximumSize: const Size(double.infinity, 52),
        ),
      ),

      // Outlined Button Theme (Secondary)
      outlinedButtonTheme: OutlinedButtonThemeData(
        style: OutlinedButton.styleFrom(
          elevation: 0,
          backgroundColor: UiColors.buttonSecondaryStill,
          foregroundColor: UiColors.primary,
          side: const BorderSide(color: UiColors.borderFocus, width: 0.5),
          textStyle: UiTypography.buttonXL,
          shape: RoundedRectangleBorder(borderRadius: UiConstants.radiusLg),
          padding: const EdgeInsets.symmetric(
            horizontal: UiConstants.space4,
            vertical: UiConstants.space3,
          ),
          maximumSize: const Size(double.infinity, 52),
        ),
      ),

      // Icon Button Theme
      iconButtonTheme: IconButtonThemeData(
        style: IconButton.styleFrom(
          foregroundColor: UiColors.iconPrimary,
          disabledForegroundColor: UiColors.iconInactive,
          shape: RoundedRectangleBorder(borderRadius: UiConstants.radiusFull),
        ),
      ),

      // Floating Action Button Theme
      floatingActionButtonTheme: const FloatingActionButtonThemeData(
        backgroundColor: UiColors.primary,
        foregroundColor: UiColors.primaryForeground,
        elevation: 4,
        shape: CircleBorder(),
      ),

      // Tab Bar Theme
      tabBarTheme: TabBarThemeData(
        labelColor: UiColors.primary,
        unselectedLabelColor: UiColors.textSecondary,
        labelStyle: UiTypography.buttonM,
        unselectedLabelStyle: UiTypography.buttonM,
        indicatorSize: TabBarIndicatorSize.label,
        indicator: const UnderlineTabIndicator(
          borderSide: BorderSide(color: UiColors.primary, width: 2),
        ),
      ),

      // Input Theme
      inputDecorationTheme: InputDecorationTheme(
        filled: true,
        fillColor: UiColors.bgInputField,
        hintStyle: UiTypography.body2r.textPlaceholder,
        labelStyle: UiTypography.body4r.textPrimary,
        errorStyle: UiTypography.footnote1r.textError,
        contentPadding: const EdgeInsets.symmetric(
          horizontal: UiConstants.space3,
          vertical: UiConstants.space3,
        ),
        border: OutlineInputBorder(
          borderRadius: UiConstants.radiusMd,
          borderSide: const BorderSide(color: UiColors.borderStill),
        ),
        enabledBorder: OutlineInputBorder(
          borderRadius: UiConstants.radiusMd,
          borderSide: const BorderSide(color: UiColors.borderStill),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: UiConstants.radiusMd,
          borderSide: const BorderSide(
            color: UiColors.borderFocus,
            width: 0.75,
          ),
        ),
        errorBorder: OutlineInputBorder(
          borderRadius: UiConstants.radiusMd,
          borderSide: const BorderSide(color: UiColors.textError),
        ),
        focusedErrorBorder: OutlineInputBorder(
          borderRadius: UiConstants.radiusMd,
          borderSide: const BorderSide(color: UiColors.textError, width: 1),
        ),
      ),

      // List Tile Theme
      listTileTheme: ListTileThemeData(
        textColor: UiColors.textPrimary,
        iconColor: UiColors.iconPrimary,
        titleTextStyle: UiTypography.body1m,
        subtitleTextStyle: UiTypography.body2r.textSecondary,
        contentPadding: const EdgeInsets.symmetric(
          horizontal: UiConstants.space4,
        ),
        tileColor: UiColors.transparent,
      ),

      // Badge Theme
      badgeTheme: BadgeThemeData(
        backgroundColor: UiColors.primary,
        textColor: UiColors.primaryForeground,
        textStyle: UiTypography.footnote2m,
        padding: const EdgeInsets.symmetric(horizontal: 4),
      ),

      // App Bar Theme
      appBarTheme: AppBarTheme(
        backgroundColor: UiColors.background,
        elevation: 0,
        titleTextStyle: UiTypography.headline5m.textPrimary,
        iconTheme: const IconThemeData(color: UiColors.iconThird, size: 20),
        surfaceTintColor: UiColors.transparent,
      ),

      // Dialog Theme
      dialogTheme: DialogThemeData(
        backgroundColor: UiColors.bgPopup,
        elevation: 8,
        shadowColor: UiColors.popupShadow,
        shape: RoundedRectangleBorder(borderRadius: UiConstants.radiusLg),
        titleTextStyle: UiTypography.headline2r.textPrimary,
        contentTextStyle: UiTypography.body2r.textDescription,
      ),

      // Bottom Navigation Bar Theme
      bottomNavigationBarTheme: BottomNavigationBarThemeData(
        backgroundColor: UiColors.white,
        selectedItemColor: UiColors.primary,
        unselectedItemColor: UiColors.textInactive,
        selectedLabelStyle: UiTypography.footnote2m,
        unselectedLabelStyle: UiTypography.footnote2r,
        type: BottomNavigationBarType.fixed,
        elevation: 8,
      ),

      // Navigation Bar Theme (Modern M3 Bottom Nav)
      navigationBarTheme: NavigationBarThemeData(
        backgroundColor: UiColors.white,
        indicatorColor: UiColors.primaryInverse.withAlpha(51), // 20% of 255
        labelTextStyle: WidgetStateProperty.resolveWith((Set<WidgetState> states) {
          if (states.contains(WidgetState.selected)) {
            return UiTypography.footnote2m.textPrimary;
          }
          return UiTypography.footnote2r.textInactive;
        }),
      ),

      // Switch Theme
      switchTheme: SwitchThemeData(
        trackColor: WidgetStateProperty.resolveWith<Color>((Set<WidgetState> states) {
          if (states.contains(WidgetState.selected)) {
            return UiColors.switchActive;
          }
          return UiColors.switchInactive;
        }),
        thumbColor: const WidgetStatePropertyAll<Color>(UiColors.white),
      ),

      // Checkbox Theme
      checkboxTheme: CheckboxThemeData(
        fillColor: WidgetStateProperty.resolveWith((Set<WidgetState> states) {
          if (states.contains(WidgetState.selected)) return UiColors.primary;
          return null;
        }),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
      ),

      // Radio Theme
      radioTheme: RadioThemeData(
        fillColor: WidgetStateProperty.resolveWith((Set<WidgetState> states) {
          if (states.contains(WidgetState.selected)) return UiColors.primary;
          return null;
        }),
      ),

      // Slider Theme
      sliderTheme: const SliderThemeData(
        activeTrackColor: UiColors.primary,
        inactiveTrackColor: UiColors.loaderInactive,
        thumbColor: UiColors.primary,
        overlayColor: UiColors.primaryInverse,
      ),

      // Chip Theme
      chipTheme: ChipThemeData(
        backgroundColor: UiColors.bgSecondary,
        labelStyle: UiTypography.footnote1m,
        secondaryLabelStyle: UiTypography.footnote1m.white,
        padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
        shape: RoundedRectangleBorder(borderRadius: UiConstants.radiusMd),
        side: const BorderSide(color: UiColors.borderStill, width: 0.5),
      ),

      // SnackBar Theme
      snackBarTheme: SnackBarThemeData(
        backgroundColor: UiColors.toastBg,
        contentTextStyle: UiTypography.body2r.white,
        behavior: SnackBarBehavior.floating,
        shape: RoundedRectangleBorder(borderRadius: UiConstants.radiusMd),
        elevation: 4,
      ),

      // Bottom Sheet Theme
      bottomSheetTheme: const BottomSheetThemeData(
        backgroundColor: UiColors.bgSecondary,
        modalBackgroundColor: UiColors.bgSecondary,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(top: Radius.circular(32)),
        ),
      ),

      // Expansion Tile Theme
      expansionTileTheme: ExpansionTileThemeData(
        iconColor: UiColors.iconSecondary,
        collapsedIconColor: UiColors.iconPrimary,
        backgroundColor: UiColors.bgPopup,
        collapsedBackgroundColor: UiColors.transparent,
        textColor: UiColors.textPrimary,
        collapsedTextColor: UiColors.textPrimary,
        tilePadding: const EdgeInsets.symmetric(horizontal: UiConstants.space4),
        shape: RoundedRectangleBorder(borderRadius: UiConstants.radiusMd),
        collapsedShape: RoundedRectangleBorder(
          borderRadius: UiConstants.radiusMd,
        ),
      ),

      // Menu Theme
      menuTheme: MenuThemeData(
        style: MenuStyle(
          backgroundColor: WidgetStateProperty.all(UiColors.bgPopup),
          elevation: WidgetStateProperty.all(4),
          shape: WidgetStateProperty.all(
            RoundedRectangleBorder(borderRadius: UiConstants.radiusMd),
          ),
        ),
      ),

      // Tooltip Theme
      tooltipTheme: TooltipThemeData(
        decoration: BoxDecoration(
          color: UiColors.toastBg.withAlpha(230), // ~90% of 255
          borderRadius: UiConstants.radiusMd,
        ),
        textStyle: UiTypography.footnote2r.white,
      ),

      // Progress Indicator Theme
      progressIndicatorTheme: const ProgressIndicatorThemeData(
        color: UiColors.primary,
        linearTrackColor: UiColors.loaderInactive,
        linearMinHeight: 4,
      ),
    );
  }
}
