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

import 'app_theme.dart';

/// Visual tokens for the full-screen monthly draw / slot machine.
/// Anchored to [AppTheme] teal seed + amber tertiary; respects light/dark [ColorScheme].
class DrawSlotTheming {
  DrawSlotTheming(this.scheme, this.brightness);

  final ColorScheme scheme;
  final Brightness brightness;

  bool get _isDark => brightness == Brightness.dark;

  /// Immersive page background (draw route).
  LinearGradient get pageBackdrop {
    if (_isDark) {
      final deep = Color.alphaBlend(
        scheme.primary.withOpacity(0.5),
        const Color(0xFF041014),
      );
      final mid = Color.alphaBlend(
        scheme.primary.withOpacity(0.22),
        const Color(0xFF0A1C22),
      );
      return LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
        stops: const [0.0, 0.55, 1.0],
        colors: [
          deep,
          mid,
          Color.alphaBlend(scheme.surface.withOpacity(0.15), const Color(0xFF020608)),
        ],
      );
    }
    return LinearGradient(
      begin: Alignment.topLeft,
      end: Alignment.bottomRight,
      colors: [
        Color.alphaBlend(scheme.primary.withOpacity(0.12), scheme.surface),
        scheme.surface,
        Color.alphaBlend(scheme.primaryContainer.withOpacity(0.35), scheme.surface),
      ],
    );
  }

  Color get pageOnText =>
      _isDark ? Colors.white.withOpacity(0.95) : scheme.onSurface;

  Color get pageMuted =>
      _isDark ? Colors.white.withOpacity(0.72) : scheme.onSurfaceVariant;

  Color get chipFill => _isDark
      ? Colors.white.withOpacity(0.12)
      : scheme.primaryContainer.withOpacity(0.45);

  Color get chipBorder =>
      _isDark ? Colors.white.withOpacity(0.22) : scheme.outlineVariant;

  /// Pre-start intro card (on draw page).
  BoxDecoration introCardDecoration() {
    return BoxDecoration(
      color: _isDark
          ? Color.alphaBlend(Colors.white.withOpacity(0.08), const Color(0xFF0C181C))
          : scheme.surfaceContainerHighest,
      borderRadius: BorderRadius.circular(20.r),
      border: Border.all(
        color: _isDark
            ? Colors.white.withOpacity(0.14)
            : scheme.outlineVariant,
        width: 1,
      ),
      boxShadow: [
        BoxShadow(
          color: scheme.primary.withOpacity(_isDark ? 0.18 : 0.08),
          blurRadius: 24.r,
          offset: Offset(0, 12.h),
        ),
      ],
    );
  }

  /// Slot machine outer chassis.
  BoxDecoration slotChassisDecoration() {
    return BoxDecoration(
      gradient: LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
        colors: _isDark
            ? [
                Color.alphaBlend(scheme.surfaceContainerHigh.withOpacity(0.4), const Color(0xFF0D1619)),
                const Color(0xFF050A0C),
              ]
            : [
                scheme.surfaceContainerHigh,
                scheme.surfaceContainer,
              ],
      ),
      borderRadius: BorderRadius.circular(22.r),
      border: Border.all(
        color: Color.alphaBlend(
          scheme.tertiary.withOpacity(0.85),
          scheme.outline,
        ),
        width: 2.w,
      ),
      boxShadow: [
        BoxShadow(
          color: scheme.primary.withOpacity(_isDark ? 0.35 : 0.12),
          blurRadius: 28.r,
          spreadRadius: -2,
          offset: Offset(0, 14.h),
        ),
        BoxShadow(
          color: Colors.black.withOpacity(_isDark ? 0.45 : 0.06),
          blurRadius: 20.r,
          offset: Offset(0, 8.h),
        ),
      ],
    );
  }

  Color get slotInnerWell => _isDark
      ? const Color(0xFF020608)
      : Color.alphaBlend(scheme.surfaceContainerHighest, scheme.surface);

  List<Color> rowColorsIdle() => _isDark
      ? [
          Color.alphaBlend(scheme.surfaceContainerHigh.withOpacity(0.5), const Color(0xFF1A2529)),
          Color.alphaBlend(scheme.primary.withOpacity(0.35), const Color(0xFF0F181C)),
        ]
      : [
          scheme.surfaceContainerHigh,
          scheme.surfaceContainer,
        ];

  List<Color> rowColorsSpinning() => [
        Color.alphaBlend(scheme.primary.withOpacity(0.55), rowColorsIdle().first),
        Color.alphaBlend(scheme.primary.withOpacity(0.4), rowColorsIdle().last),
      ];

  List<Color> rowColorsWinner() => _isDark
      ? [
          Color.alphaBlend(scheme.primary.withOpacity(0.9), const Color(0xFF0A3030)),
          Color.alphaBlend(scheme.tertiary.withOpacity(0.35), scheme.primary),
        ]
      : [
          scheme.primary,
          Color.alphaBlend(scheme.tertiary.withOpacity(0.28), scheme.primary),
        ];

  Color get winnerRowText =>
      _isDark ? Colors.white : scheme.onPrimary;

  Color get idleRowText => _isDark
      ? Colors.white.withOpacity(0.92)
      : scheme.onSurface;

  Color get winnerFrameColor =>
      Color.alphaBlend(scheme.tertiary.withOpacity(0.95), AppTheme.accent);

  BoxDecoration winnerSummaryDecoration() {
    return BoxDecoration(
      gradient: LinearGradient(
        colors: [
          Color.alphaBlend(scheme.primaryContainer.withOpacity(_isDark ? 0.35 : 0.65), scheme.surface),
          Color.alphaBlend(scheme.tertiary.withOpacity(_isDark ? 0.12 : 0.2), scheme.surface),
        ],
      ),
      borderRadius: BorderRadius.circular(18.r),
      border: Border.all(
        color: scheme.primary.withOpacity(0.35),
        width: 1.5,
      ),
    );
  }

  TextStyle winnerSummaryTitle(TextTheme t) => (t.titleLarge ?? const TextStyle()).copyWith(
        fontWeight: FontWeight.w800,
        color: scheme.primary,
        height: 1.2,
      );

  Color get winnerSummaryIcon => scheme.tertiary;
}
