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

/// Kasy Design System — Typography Tokens
///
/// Single source of truth for all text styles in the app.
/// To change the font family or any style globally, change it here.
///
/// Usage via context extension:
///   context.textTheme.bodyMedium
///   context.textTheme.titleLarge
///
/// Font family is defined once in [KasyTextTheme._fontFamily].
/// Change it here → all text in the app updates automatically.
class KasyTextTheme extends ThemeExtension<KasyTextTheme> {
  // -----------------------------------------------------------------------
  // Font family — change here to update the entire app
  // -----------------------------------------------------------------------
  static TextStyle get _base => GoogleFonts.plusJakartaSans();

  // -----------------------------------------------------------------------
  // Display — for large, prominent text (hero sections, splash screens)
  // -----------------------------------------------------------------------
  final TextStyle displayLarge;
  final TextStyle displayMedium;
  final TextStyle displaySmall;

  // -----------------------------------------------------------------------
  // Headline — section headings
  // -----------------------------------------------------------------------
  final TextStyle headlineLarge;
  final TextStyle headlineMedium;
  final TextStyle headlineSmall;

  // -----------------------------------------------------------------------
  // Title — card titles, dialog headers, navigation labels
  // -----------------------------------------------------------------------
  final TextStyle titleLarge;
  final TextStyle titleMedium;
  final TextStyle titleSmall;

  // -----------------------------------------------------------------------
  // Body — paragraph text, list content
  // -----------------------------------------------------------------------
  final TextStyle bodyLarge;
  final TextStyle bodyMedium;
  final TextStyle bodySmall;

  // -----------------------------------------------------------------------
  // Label — buttons, tags, chips, captions
  // -----------------------------------------------------------------------
  final TextStyle labelLarge;
  final TextStyle labelMedium;
  final TextStyle labelSmall;

  // Legacy — kept for backward compatibility
  final TextStyle primary;

  const KasyTextTheme({
    required this.displayLarge,
    required this.displayMedium,
    required this.displaySmall,
    required this.headlineLarge,
    required this.headlineMedium,
    required this.headlineSmall,
    required this.titleLarge,
    required this.titleMedium,
    required this.titleSmall,
    required this.bodyLarge,
    required this.bodyMedium,
    required this.bodySmall,
    required this.labelLarge,
    required this.labelMedium,
    required this.labelSmall,
    required this.primary,
  });

  factory KasyTextTheme.build() {
    final base = _base;

    return KasyTextTheme(
      // Display
      displayLarge: base.copyWith(fontSize: 57, fontWeight: FontWeight.w300, letterSpacing: -0.25),
      displayMedium: base.copyWith(fontSize: 45, fontWeight: FontWeight.w300),
      displaySmall: base.copyWith(fontSize: 36, fontWeight: FontWeight.w400),

      // Headline
      headlineLarge: base.copyWith(fontSize: 32, fontWeight: FontWeight.w600),
      headlineMedium: base.copyWith(fontSize: 26, fontWeight: FontWeight.w600),
      headlineSmall: base.copyWith(fontSize: 22, fontWeight: FontWeight.w500),

      // Title
      titleLarge: base.copyWith(fontSize: 20, fontWeight: FontWeight.w500),
      titleMedium: base.copyWith(fontSize: 15, fontWeight: FontWeight.w600, letterSpacing: 0.1),
      titleSmall: base.copyWith(fontSize: 14, fontWeight: FontWeight.w500, letterSpacing: 0.1),

      // Body
      bodyLarge: base.copyWith(fontSize: 16, fontWeight: FontWeight.w400, letterSpacing: 0.15),
      bodyMedium: base.copyWith(fontSize: 14, fontWeight: FontWeight.w400, letterSpacing: 0.1),
      bodySmall: base.copyWith(fontSize: 12, fontWeight: FontWeight.w400, letterSpacing: 0.2),

      // Label
      labelLarge: base.copyWith(fontSize: 14, fontWeight: FontWeight.w600, letterSpacing: 0.05),
      labelMedium: base.copyWith(fontSize: 12, fontWeight: FontWeight.w500, letterSpacing: 0.4),
      labelSmall: base.copyWith(fontSize: 11, fontWeight: FontWeight.w500, letterSpacing: 0.4),

      // Legacy primary
      primary: base.copyWith(fontSize: 16, fontWeight: FontWeight.w400),
    );
  }

  @override
  ThemeExtension<KasyTextTheme> copyWith({
    TextStyle? displayLarge,
    TextStyle? displayMedium,
    TextStyle? displaySmall,
    TextStyle? headlineLarge,
    TextStyle? headlineMedium,
    TextStyle? headlineSmall,
    TextStyle? titleLarge,
    TextStyle? titleMedium,
    TextStyle? titleSmall,
    TextStyle? bodyLarge,
    TextStyle? bodyMedium,
    TextStyle? bodySmall,
    TextStyle? labelLarge,
    TextStyle? labelMedium,
    TextStyle? labelSmall,
    TextStyle? primary,
  }) {
    return KasyTextTheme(
      displayLarge: displayLarge ?? this.displayLarge,
      displayMedium: displayMedium ?? this.displayMedium,
      displaySmall: displaySmall ?? this.displaySmall,
      headlineLarge: headlineLarge ?? this.headlineLarge,
      headlineMedium: headlineMedium ?? this.headlineMedium,
      headlineSmall: headlineSmall ?? this.headlineSmall,
      titleLarge: titleLarge ?? this.titleLarge,
      titleMedium: titleMedium ?? this.titleMedium,
      titleSmall: titleSmall ?? this.titleSmall,
      bodyLarge: bodyLarge ?? this.bodyLarge,
      bodyMedium: bodyMedium ?? this.bodyMedium,
      bodySmall: bodySmall ?? this.bodySmall,
      labelLarge: labelLarge ?? this.labelLarge,
      labelMedium: labelMedium ?? this.labelMedium,
      labelSmall: labelSmall ?? this.labelSmall,
      primary: primary ?? this.primary,
    );
  }

  @override
  ThemeExtension<KasyTextTheme> lerp(
    covariant ThemeExtension<KasyTextTheme>? other,
    double t,
  ) {
    if (other is! KasyTextTheme) return this;
    return KasyTextTheme(
      displayLarge: TextStyle.lerp(displayLarge, other.displayLarge, t)!,
      displayMedium: TextStyle.lerp(displayMedium, other.displayMedium, t)!,
      displaySmall: TextStyle.lerp(displaySmall, other.displaySmall, t)!,
      headlineLarge: TextStyle.lerp(headlineLarge, other.headlineLarge, t)!,
      headlineMedium: TextStyle.lerp(headlineMedium, other.headlineMedium, t)!,
      headlineSmall: TextStyle.lerp(headlineSmall, other.headlineSmall, t)!,
      titleLarge: TextStyle.lerp(titleLarge, other.titleLarge, t)!,
      titleMedium: TextStyle.lerp(titleMedium, other.titleMedium, t)!,
      titleSmall: TextStyle.lerp(titleSmall, other.titleSmall, t)!,
      bodyLarge: TextStyle.lerp(bodyLarge, other.bodyLarge, t)!,
      bodyMedium: TextStyle.lerp(bodyMedium, other.bodyMedium, t)!,
      bodySmall: TextStyle.lerp(bodySmall, other.bodySmall, t)!,
      labelLarge: TextStyle.lerp(labelLarge, other.labelLarge, t)!,
      labelMedium: TextStyle.lerp(labelMedium, other.labelMedium, t)!,
      labelSmall: TextStyle.lerp(labelSmall, other.labelSmall, t)!,
      primary: TextStyle.lerp(primary, other.primary, t)!,
    );
  }
}
