import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pweb/providers/mock_payment.dart';

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


class PaymentFormWidget extends StatelessWidget {
  const PaymentFormWidget({super.key});

  static const double _smallSpacing = 5;
  static const double _mediumSpacing = 10;
  static const double _largeSpacing = 16;
  static const double _extraSpacing = 15;

  String _formatAmount(double amount) => amount.toStringAsFixed(2);

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<MockPaymentProvider>(context);
    final theme = Theme.of(context);
    final loc = AppLocalizations.of(context)!;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(loc.details, style: theme.textTheme.titleMedium),
        const SizedBox(height: _smallSpacing),

        TextField(
          keyboardType: const TextInputType.numberWithOptions(decimal: true),
          decoration: InputDecoration(
            labelText: loc.amount,
            border: const OutlineInputBorder(),
          ),
          onChanged: (val) {
            final parsed = double.tryParse(val.replaceAll(',', '.')) ?? 0.0;
            provider.setAmount(parsed);
          },
        ),

        const SizedBox(height: _mediumSpacing),

        Row(
          spacing: _mediumSpacing,
          children: [
            Text(loc.recipientPaysFee, style: theme.textTheme.titleMedium),
            Switch(
              value: !provider.payerCoversFee,
              onChanged: (val) => provider.setPayerCoversFee(!val),
            ),
          ],
        ),

        const SizedBox(height: _largeSpacing),

        Align(
          alignment: Alignment.center,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              _SummaryRow(label: loc.sentAmount(_formatAmount(provider.amount)), style: theme.textTheme.titleMedium),
              _SummaryRow(label: loc.fee(_formatAmount(provider.fee)), style: theme.textTheme.titleMedium),
              _SummaryRow(label: loc.recipientWillReceive(_formatAmount(provider.recipientGets)), style: theme.textTheme.titleMedium),

              const SizedBox(height: _extraSpacing),

              _SummaryRow(
                label: loc.total(_formatAmount(provider.total)),
                style: theme.textTheme.titleMedium!.copyWith(fontWeight: FontWeight.w600),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

class _SummaryRow extends StatelessWidget {
  final String label;
  final TextStyle? style;

  const _SummaryRow({required this.label, this.style});

  @override
  Widget build(BuildContext context) {
    return Text(label, style: style);
  }
}
