import 'package:flutter/material.dart';

import 'package:pshared/models/payment/quote/status_type.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';


class QuoteStatusCard extends StatelessWidget {
  final QuoteStatusType statusType;
  final bool isLoading;
  final String statusText;
  final String? helperText;
  final bool canRefresh;
  final bool showPrimaryRefresh;
  final VoidCallback onRefresh;

  const QuoteStatusCard({
    super.key,
    required this.statusType,
    required this.isLoading,
    required this.statusText,
    required this.helperText,
    required this.canRefresh,
    required this.showPrimaryRefresh,
    required this.onRefresh,
  });

  static const double _cardRadius = 12;
  static const double _cardSpacing = 8;
  static const double _iconSize = 18;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final loc = AppLocalizations.of(context)!;
    final foregroundColor = _resolveForegroundColor(theme, statusType);
    final elementColor = _resolveElementColor(theme, statusType);
    final statusStyle = theme.textTheme.bodyMedium?.copyWith(
      color: elementColor,
    );
    final helperStyle = theme.textTheme.bodySmall?.copyWith(
      color: foregroundColor.withValues(alpha: 0.8),
    );

    return Container(
      padding: const EdgeInsets.all(_cardSpacing),
      decoration: BoxDecoration(
        color: _resolveCardColor(theme, statusType),
        borderRadius: BorderRadius.circular(_cardRadius),
        border: Border.all(color: elementColor.withValues(alpha: 0.5)),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 2),
            child: isLoading
                ? SizedBox(
                    width: _iconSize,
                    height: _iconSize,
                    child: CircularProgressIndicator(
                      strokeWidth: 2,
                      valueColor: AlwaysStoppedAnimation<Color>(
                        foregroundColor,
                      ),
                    ),
                  )
                : Icon(
                    _resolveIcon(statusType),
                    size: _iconSize,
                    color: elementColor,
                  ),
          ),
          const SizedBox(width: _cardSpacing),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(statusText, style: statusStyle),
                if (helperText != null) ...[
                  const SizedBox(height: 4),
                  Text(helperText!, style: helperStyle),
                ],
              ],
            ),
          ),
          if (canRefresh) ...[
            const SizedBox(width: _cardSpacing),
            IconButton(
              onPressed: onRefresh,
              tooltip: loc.quoteRefresh,
              icon: const Icon(Icons.refresh),
              color: showPrimaryRefresh ? foregroundColor : elementColor,
            ),
          ],
        ],
      ),
    );
  }

  Color _resolveCardColor(ThemeData theme, QuoteStatusType status) {
    switch (status) {
      case QuoteStatusType.loading:
      case QuoteStatusType.active:
      case QuoteStatusType.missing:
        return theme.colorScheme.onSecondary;
      case QuoteStatusType.error:
      case QuoteStatusType.expired:
        return theme.colorScheme.errorContainer;
    }
  }

  Color _resolveForegroundColor(ThemeData theme, QuoteStatusType status) {
    switch (status) {
      case QuoteStatusType.active:
      case QuoteStatusType.missing:
      case QuoteStatusType.loading:
        return theme.colorScheme.onSecondary;
      case QuoteStatusType.error:
      case QuoteStatusType.expired:
        return theme.colorScheme.onErrorContainer;
    }
  }

  Color _resolveElementColor(ThemeData theme, QuoteStatusType status) {
    switch (status) {
      case QuoteStatusType.active:
      case QuoteStatusType.missing:
      case QuoteStatusType.loading:
        return theme.colorScheme.primary;
      case QuoteStatusType.error:
      case QuoteStatusType.expired:
        return theme.colorScheme.onErrorContainer;
    }
  }

  IconData _resolveIcon(QuoteStatusType status) {
    switch (status) {
      case QuoteStatusType.loading:
        return Icons.sync_rounded;
      case QuoteStatusType.error:
        return Icons.warning_amber_rounded;
      case QuoteStatusType.expired:
        return Icons.error_outline_rounded;
      case QuoteStatusType.active:
        return Icons.timer_outlined;
      case QuoteStatusType.missing:
        return Icons.info_outline_rounded;
    }
  }
}
