import 'package:flutter/material.dart';

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

import 'package:pweb/pages/payment_methods/form.dart';
import 'package:pweb/pages/payment_methods/icon.dart';


class AdressBookPaymentMethodTile extends StatefulWidget {
  final PaymentType type;
  final String title;
  final Map<PaymentType, Object?> methods;
  final ValueChanged<Object?> onChanged;

  final double spacingM;
  final double spacingS;
  final double sizeM;
  final TextStyle? titleTextStyle;

  const AdressBookPaymentMethodTile({
    super.key,
    required this.type,
    required this.title,
    required this.methods,
    required this.onChanged,
    this.spacingM = 12,
    this.spacingS = 8,
    this.sizeM = 20,
    this.titleTextStyle,
  });

  @override
  State<AdressBookPaymentMethodTile> createState() => _AdressBookPaymentMethodTileState();
}

class _AdressBookPaymentMethodTileState extends State<AdressBookPaymentMethodTile> {
  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final isAdded = widget.methods.containsKey(widget.type);

    return ExpansionTile(
      title: Row(
        children: [
          Icon(
            iconForPaymentType(widget.type), 
            size: widget.sizeM,
            color: isAdded 
              ? theme.colorScheme.primary 
              : theme.colorScheme.onSurface,
          ),
          SizedBox(width: widget.spacingS),
          Text(
            widget.title,
            style: widget.titleTextStyle ??
                theme.textTheme.titleMedium?.copyWith(
                  fontWeight: FontWeight.bold,
                  color: isAdded ? theme.colorScheme.primary : null,
                ),
          ),
        ],
      ),
      trailing: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (isAdded)
            IconButton(
              icon: Icon(Icons.delete, color: theme.colorScheme.error),
              onPressed: () {
                widget.onChanged(null);
              },
            ),
          Icon(
            isAdded ? Icons.check_circle : Icons.add_circle_outline,
            color: isAdded ? theme.colorScheme.primary : null,
          ),
        ],
      ),
      children: [
        PaymentMethodForm(
          key: ValueKey(widget.type),
          selectedType: widget.type,
          initialData: widget.methods[widget.type],
          onChanged: widget.onChanged,
        ),
        SizedBox(height: widget.spacingM),
      ],
    );
  }
}
