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

import 'package:pweb/controllers/payouts/quotation.dart';
import 'package:pweb/utils/quote_duration_format.dart';

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


class QuoteStatusData {
  final QuoteStatusType statusType;
  final String statusText;
  final String? helperText;
  final bool isLoading;
  final bool canRefresh;
  final bool showPrimaryRefresh;
  final AutoRefreshMode autoRefreshMode;

  const QuoteStatusData({
    required this.statusType,
    required this.statusText,
    required this.helperText,
    required this.isLoading,
    required this.canRefresh,
    required this.showPrimaryRefresh,
    required this.autoRefreshMode,
  });

  static QuoteStatusData resolve({
    required QuotationController controller,
    required AppLocalizations loc,
  }) {
    final timeLeft = controller.timeLeft;
    final isLoading = controller.isLoading;
    final statusType = controller.quoteStatus;
    final autoRefreshMode = controller.autoRefreshMode;

    String statusText;
    String? helperText;
    switch (statusType) {
      case QuoteStatusType.loading:
        statusText = loc.quoteUpdating;
        break;
      case QuoteStatusType.error:
        statusText = loc.quoteErrorGeneric;
        break;
      case QuoteStatusType.missing:
        statusText = loc.quoteUnavailable;
        break;
      case QuoteStatusType.expired:
        statusText = loc.quoteExpired;
        helperText = loc.quoteRefreshRequired;
        break;
      case QuoteStatusType.active:
        statusText = timeLeft == null
            ? loc.quoteActive
            : loc.quoteExpiresIn(formatQuoteDuration(timeLeft));
        break;
    }

    final canRefresh = controller.canRefresh && !isLoading;
    final showPrimaryRefresh = canRefresh &&
        (statusType == QuoteStatusType.expired ||
            statusType == QuoteStatusType.error ||
            statusType == QuoteStatusType.missing);

    return QuoteStatusData(
      statusType: statusType,
      statusText: statusText,
      helperText: helperText,
      isLoading: isLoading,
      canRefresh: canRefresh,
      showPrimaryRefresh: showPrimaryRefresh,
      autoRefreshMode: autoRefreshMode,
    );
  }
}