import 'package:flutter/material.dart';

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

import 'package:pweb/models/wallet.dart';
import 'package:pweb/providers/wallets.dart';
import 'package:pweb/widgets/sidebar/destinations.dart';
import 'package:pweb/services/amplitude.dart';
import 'package:pweb/providers/recipient.dart';


class PageSelectorProvider extends ChangeNotifier {
  PayoutDestination _selected = PayoutDestination.dashboard;
  PaymentType? _type;
  bool _cameFromRecipientList = false;

  final RecipientProvider? recipientProvider;
  final WalletsProvider? walletsProvider;

  PayoutDestination get selected => _selected;
  PaymentType? get type => _type;

  PageSelectorProvider({this.recipientProvider, this.walletsProvider});

  void selectPage(PayoutDestination dest) {
    _selected = dest;
    notifyListeners();
  }

  void selectRecipient(Recipient? recipient, {bool fromList = false}) {
    if (recipientProvider != null) {
      recipientProvider!.selectRecipient(recipient);
      _cameFromRecipientList = fromList;
      _selected = PayoutDestination.payment;
      notifyListeners();
    } else {
      debugPrint("RecipientProvider is null — cannot select recipient");
    }
  }

  void editRecipient(Recipient? recipient, {bool fromList = false}) {
    if (recipientProvider != null) {
      recipientProvider!.selectRecipient(recipient);
      _cameFromRecipientList = fromList;
      _selected = PayoutDestination.addrecipient;
      notifyListeners();
    } else {
      debugPrint("RecipientProvider is null — cannot select recipient");
    }
  }

  void goToAddRecipient() {
    if (recipientProvider != null) {
      AmplitudeService.recipientAddStarted();
      recipientProvider!.selectRecipient(null);
      _selected = PayoutDestination.addrecipient;
      _cameFromRecipientList = false;
      notifyListeners();
    } else {
      debugPrint("RecipientProvider is null — cannot go to add recipient");
    }
  }

  void startPaymentWithoutRecipient(PaymentType type) {
    if (recipientProvider != null) {
      recipientProvider!.selectRecipient(null);
    }
    _type = type;
    _cameFromRecipientList = false;
    _selected = PayoutDestination.payment;
    notifyListeners();
  }

  void goBackFromPayment() {
    _selected = _cameFromRecipientList
        ? PayoutDestination.recipients
        : PayoutDestination.dashboard;
    _type = null;
    notifyListeners();
  }

  void goBackFromWalletEdit() {
    selectPage(PayoutDestination.methods);
  }

  void selectWallet(Wallet wallet) {
    if (walletsProvider != null) {
      walletsProvider!.selectWallet(wallet);
      _selected = PayoutDestination.editwallet;
      notifyListeners();
    } else {
      debugPrint("RecipientProvider is null — cannot select wallet");
    }
  }

  Recipient? get selectedRecipient => recipientProvider?.selectedRecipient;
  Wallet? get selectedWallet => walletsProvider?.selectedWallet;
}