import 'package:flutter/material.dart';

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

import 'package:pweb/pages/dashboard/buttons/balance/add/asset_type_field.dart';
import 'package:pweb/pages/dashboard/buttons/balance/add/description.dart';
import 'package:pweb/pages/dashboard/buttons/balance/add/employees_loading_indicator.dart';
import 'package:pweb/pages/dashboard/buttons/balance/add/ledger/fields.dart';
import 'package:pweb/pages/dashboard/buttons/balance/add/managed_wallet_fields.dart';
import 'package:pweb/pages/dashboard/buttons/balance/add/name.dart';
import 'package:pweb/pages/dashboard/buttons/balance/add/owner.dart';


class AddBalanceForm extends StatelessWidget {
  final GlobalKey<FormState> formKey;
  final PaymentType assetType;
  final bool isSaving;
  final List<DropdownMenuItem<String?>> ownerItems;
  final String? ownerValue;
  final ValueChanged<PaymentType?> onAssetTypeChanged;
  final ValueChanged<String?> onOwnerChanged;
  final TextEditingController nameController;
  final TextEditingController descriptionController;
  final CurrencyCode managedCurrency;
  final ChainNetwork network;
  final CurrencyCode ledgerCurrency;
  final ValueChanged<CurrencyCode?> onManagedCurrencyChanged;
  final ValueChanged<ChainNetwork?> onNetworkChanged;
  final ValueChanged<CurrencyCode?> onLedgerCurrencyChanged;
  final bool showEmployeesLoading;

  const AddBalanceForm({
    super.key,
    required this.formKey,
    required this.assetType,
    required this.isSaving,
    required this.ownerItems,
    required this.ownerValue,
    required this.onAssetTypeChanged,
    required this.onOwnerChanged,
    required this.nameController,
    required this.descriptionController,
    required this.managedCurrency,
    required this.network,
    required this.ledgerCurrency,
    required this.onManagedCurrencyChanged,
    required this.onNetworkChanged,
    required this.onLedgerCurrencyChanged,
    required this.showEmployeesLoading,
  });

  @override
  Widget build(BuildContext context) => Form(
    key: formKey,
    child: SingleChildScrollView(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        spacing: 12,
        children: [
          AssetTypeField(
            value: assetType,
            onChanged: isSaving ? null : onAssetTypeChanged,
          ),
          OwnerField(
            value: ownerValue,
            items: ownerItems,
            onChanged: isSaving ? null : onOwnerChanged,
          ),
          NameField(controller: nameController),
          DescriptionField(controller: descriptionController),
          if (assetType == PaymentType.managedWallet)
            ManagedWalletFields(
              currency: managedCurrency,
              network: network,
              onCurrencyChanged: isSaving ? null : onManagedCurrencyChanged,
              onNetworkChanged: isSaving ? null : onNetworkChanged,
            )
          else
            LedgerFields(
              currency: ledgerCurrency,
              onCurrencyChanged: isSaving ? null : onLedgerCurrencyChanged,
            ),
          if (showEmployeesLoading) const EmployeesLoadingIndicator(),
        ],
      ),
    ),
  );
}
