import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

import 'package:pweb/pages/dashboard/payouts/quote_status/widgets/body.dart';
import 'package:pweb/utils/quote_duration_format.dart';

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


class QuoteStatus extends StatelessWidget {
  final double spacing;

  const QuoteStatus({super.key, required this.spacing});

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final controller = context.watch<QuotationController>();
    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 QuoteStatusBody(
      spacing: spacing,
      statusType: statusType,
      statusText: statusText,
      helperText: helperText,
      isLoading: isLoading,
      canRefresh: canRefresh,
      showPrimaryRefresh: showPrimaryRefresh,
      autoRefreshMode: autoRefreshMode,
      onAutoRefreshModeChanged: controller.setAutoRefreshMode,
      onRefresh: controller.refreshQuotation,
    );
  }
}
