import 'package:flutter/material.dart';

import 'package:pshared/models/payment/type.dart';

import 'package:pweb/models/payment/method_tile/availability.dart';
import 'package:pweb/models/payment/method_tile/selection.dart';
import 'package:pweb/pages/payment_methods/icon.dart';
import 'package:pweb/utils/payment/label.dart';

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


class PaymentMethodTile extends StatelessWidget {
  final PaymentType type;
  final PaymentMethodTileSelection selection;
  final PaymentMethodTileAvailability availability;
  final double padding;
  final String? detailsText;
  final VoidCallback? onTap;

  const PaymentMethodTile({
    required this.type,
    required this.selection,
    required this.availability,
    required this.padding,
    this.detailsText,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final l10n = AppLocalizations.of(context)!;
    final label = getPaymentTypeLabel(context, type);
    final badgeLabel = switch (availability) {
      PaymentMethodTileAvailability.added => l10n.paymentMethodAdded,
      PaymentMethodTileAvailability.comingSoon => l10n.paymentMethodComingSoon,
      PaymentMethodTileAvailability.available => l10n.paymentMethodNotAdded,
    };
    final isSelected = selection == PaymentMethodTileSelection.selected;
    final isDisabled = availability == PaymentMethodTileAvailability.comingSoon;
    final disabledOpacity = isDisabled ? 0.55 : 1.0;
    final badgeColor = availability == PaymentMethodTileAvailability.added
        ? theme.colorScheme.primary.withValues(alpha: 0.12)
        : theme.colorScheme.onSurface.withValues(alpha: isDisabled ? 0.06 : 0.08);
    final badgeTextColor = availability == PaymentMethodTileAvailability.added
        ? theme.colorScheme.primary
        : theme.colorScheme.onSurface.withValues(alpha: isDisabled ? 0.6 : 1.0);
    final borderColor = isSelected
        ? theme.colorScheme.primary
        : theme.colorScheme.onSurface.withValues(alpha: 0.12);
    final backgroundColor = isSelected
        ? theme.colorScheme.primary.withValues(alpha: 0.08)
        : theme.colorScheme.onSecondary;
    final showDetails =
        availability == PaymentMethodTileAvailability.added &&
        detailsText != null &&
        detailsText!.isNotEmpty;
    final detailsColor = isSelected
        ? theme.colorScheme.primary
        : theme.colorScheme.onSurfaceVariant;

    return IntrinsicWidth(
      child: Opacity(
        opacity: disabledOpacity,
        child: Material(
          color: backgroundColor,
          borderRadius: BorderRadius.circular(14),
          child: InkWell(
            onTap: onTap,
            borderRadius: BorderRadius.circular(14),
            child: AnimatedContainer(
              duration: const Duration(milliseconds: 160),
              padding: EdgeInsets.all(padding),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(14),
                border: Border.all(color: borderColor),
              ),
              child: Column(
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Icon(
                        iconForPaymentType(type),
                        size: 20,
                        color: isSelected ? theme.colorScheme.primary : theme.colorScheme.onSurface,
                      ),
                      const SizedBox(width: 8),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            label,
                            style: theme.textTheme.titleSmall?.copyWith(
                              fontWeight: FontWeight.w600,
                              color: isSelected ? theme.colorScheme.primary : theme.colorScheme.onSurface,
                            ),
                          ),
                          const SizedBox(height: 8),
                          if (showDetails)
                            Text(
                              detailsText!,
                              style: theme.textTheme.labelSmall?.copyWith(
                                color: detailsColor,
                                fontWeight: FontWeight.w600,
                              ),
                            )
                          else
                            Container(
                              padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
                              decoration: BoxDecoration(
                                color: badgeColor,
                                borderRadius: BorderRadius.circular(999),
                              ),
                              child: Text(
                                badgeLabel,
                                style: theme.textTheme.labelSmall?.copyWith(
                                  color: badgeTextColor,
                                  fontWeight: FontWeight.w600,
                                ),
                              ),
                            ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
