import 'package:flutter/material.dart';

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

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

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


class PaymentMethodDropdown extends StatefulWidget {
  final List<PaymentMethod> methods;
  final ValueChanged<PaymentMethod> onChanged;
  final PaymentMethod? initialValue;

  const PaymentMethodDropdown({
    super.key,
    required this.methods,
    required this.onChanged,
    this.initialValue,
  });

  @override
  State<PaymentMethodDropdown> createState() => _PaymentMethodDropdownState();
}

class _PaymentMethodDropdownState extends State<PaymentMethodDropdown> {
  late PaymentMethod _selectedMethod;

  @override
  void initState() {
    super.initState();
    _selectedMethod = widget.initialValue ?? widget.methods.first;
  }

  @override
  Widget build(BuildContext context) {
    return DropdownButtonFormField<PaymentMethod>(
      dropdownColor: Theme.of(context).colorScheme.onSecondary,
      initialValue: _selectedMethod,
      decoration: InputDecoration(
        labelText: AppLocalizations.of(context)!.whereGetMoney,
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
      ),
      items: widget.methods.map((method) {
        return DropdownMenuItem<PaymentMethod>(
          value: method,
          child: Row(
            children: [
              Icon(iconForPaymentType(method.type), size: 20),
              const SizedBox(width: 8),
              Text('${method.label} (${method.details})'),
            ],
          ),
        );
      }).toList(),
      onChanged: (value) {
        if (value != null) {
          setState(() => _selectedMethod = value);
          widget.onChanged(value);
        }
      },
    );
  }
}