import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

import 'package:pweb/pages/address_book/form/view.dart';
import 'package:pweb/providers/address_book_recipient_form.dart';
import 'package:pweb/utils/payment/availability.dart';


class AddressBookRecipientFormBody extends StatefulWidget {
  final GlobalKey<FormState> formKey;
  final TextEditingController nameCtrl;
  final TextEditingController emailCtrl;
  final bool isEditing;
  final Future<void> Function(AddressBookRecipientFormProvider) onSave;
  final VoidCallback onBack;

  const AddressBookRecipientFormBody({
    required this.formKey,
    required this.nameCtrl,
    required this.emailCtrl,
    required this.isEditing,
    required this.onSave,
    required this.onBack,
  });

  @override
  State<AddressBookRecipientFormBody> createState() => _AddressBookRecipientFormBodyState();
}

class _AddressBookRecipientFormBodyState extends State<AddressBookRecipientFormBody> {
  PaymentType? _selectedType;
  int? _selectedIndex;

  void _reconcileSelection(AddressBookRecipientFormProvider formState) {
    final types = formState.supportedTypes;
    if (types.isEmpty) return;

    var nextType = _selectedType;
    var nextIndex = _selectedIndex;

    if (nextType == null || !types.contains(nextType)) {
      nextType = formState.preferredType ?? types.first;
      nextIndex = null;
    }

    final entries = formState.methods[nextType] ?? const [];
    if (entries.isEmpty) {
      nextIndex = null;
    } else if (nextIndex == null || nextIndex < 0 || nextIndex >= entries.length) {
      nextIndex = 0;
    }

    if (nextType == _selectedType && nextIndex == _selectedIndex) return;
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (!mounted) return;
      setState(() {
        _selectedType = nextType;
        _selectedIndex = nextIndex;
      });
    });
  }

  void _onMethodSelected(PaymentType type, int index) {
    setState(() {
      _selectedType = type;
      _selectedIndex = index;
    });
  }

  void _onMethodAdd(AddressBookRecipientFormProvider formState, PaymentType type) {
    final newIndex = formState.addMethod(type);
    setState(() {
      _selectedType = type;
      _selectedIndex = newIndex;
    });
  }

  void _onMethodRemove(AddressBookRecipientFormProvider formState, int index) {
    final type = _selectedType ?? formState.supportedTypes.first;
    formState.removeMethod(type, index);
  }

  void _onMethodChanged(
    AddressBookRecipientFormProvider formState,
    int index,
    PaymentMethodData data,
  ) {
    final type = _selectedType ?? formState.supportedTypes.first;
    formState.updateMethod(type, index, data);
  }

  @override
  Widget build(BuildContext context) {
    final formState = context.watch<AddressBookRecipientFormProvider>();
    _reconcileSelection(formState);

    final selectedType = _selectedType ?? formState.supportedTypes.first;
    return FormView(
      formKey: widget.formKey,
      nameCtrl: widget.nameCtrl,
      emailCtrl: widget.emailCtrl,
      types: formState.supportedTypes,
      selectedType: selectedType,
      selectedIndex: _selectedIndex,
      methods: formState.methods,
      onMethodSelected: _onMethodSelected,
      onMethodAdd: (type) => _onMethodAdd(formState, type),
      disabledTypes: disabledPaymentTypes,
      onMethodRemove: (index) => _onMethodRemove(formState, index),
      onMethodChanged: (index, data) => _onMethodChanged(formState, index, data),
      onSave: () => widget.onSave(formState),
      isEditing: widget.isEditing,
      onBack: widget.onBack,
    );
  }
}
