import 'package:flutter/material.dart';

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

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

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


class PaymentMethodDropdown extends StatelessWidget {
  final List<Wallet> methods;
  final ValueChanged<Wallet> onChanged;
  final Wallet? selectedMethod;

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

  @override
  Widget build(BuildContext context) => DropdownButtonFormField<Wallet>(
    dropdownColor: Theme.of(context).colorScheme.onSecondary,
    initialValue: _getSelectedMethod(),
    decoration: InputDecoration(
      labelText: AppLocalizations.of(context)!.whereGetMoney,
      border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
    ),
    items: methods.map((method) => DropdownMenuItem<Wallet>(
      value: method,
      child: Row(
        children: [
          Icon(iconForPaymentType(PaymentType.managedWallet), size: 20),
          const SizedBox(width: 8),
          Text(method.name),
        ],
      ),
    )).toList(),
    onChanged: (value) {
      if (value != null) {
        onChanged(value);
      }
    },
  );

  Wallet? _getSelectedMethod() {
    if (selectedMethod != null) return selectedMethod;
    if (methods.isEmpty) return null;
    return methods.first;
  }
}
