import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pweb/pages/address_book/form/page.dart';
import 'package:pweb/pages/address_book/page/page.dart';
import 'package:pweb/pages/payment_methods/page.dart';
import 'package:pweb/pages/payment_page/page.dart';
import 'package:pweb/pages/payment_page/wallet/edit/page.dart';
import 'package:pweb/pages/report/page.dart';
import 'package:pweb/pages/settings/profile/page.dart';
import 'package:pweb/providers/page_selector.dart';
import 'package:pweb/widgets/appbar/app_bar.dart';
import 'package:pweb/pages/dashboard/dashboard.dart';
import 'package:pweb/widgets/sidebar/destinations.dart';
import 'package:pweb/widgets/sidebar/sidebar.dart';


class PageSelector extends StatelessWidget {
  const PageSelector({super.key});

  @override
  Widget build(BuildContext context) {
    final provider = context.watch<PageSelectorProvider>();

    Widget content;
    switch (provider.selected) {
      case PayoutDestination.dashboard:
        content = DashboardPage(
          onRecipientSelected: (recipient) =>
              provider.selectRecipient(recipient),
          onGoToPaymentWithoutRecipient: provider.startPaymentWithoutRecipient,
        );
        break;

      case PayoutDestination.recipients:
        content = RecipientAddressBookPage(
          onRecipientSelected: (recipient) =>
              provider.selectRecipient(recipient, fromList: true),
          onAddRecipient: provider.goToAddRecipient,
          onEditRecipient: provider.editRecipient,
        );
        break;

      case PayoutDestination.addrecipient:
        final recipient = provider.recipientProvider?.selectedRecipient;
        content = AdressBookRecipientForm(
          recipient: recipient,
          onSaved: (_) => provider.selectPage(PayoutDestination.recipients),
        );
        break;

      case PayoutDestination.payment:
        content = PaymentPage(
          type: provider.type,
          onBack: (_) => provider.goBackFromPayment(),
        );
        break;

      case PayoutDestination.settings:
        content = ProfileSettingsPage();
        break;

      case PayoutDestination.reports:
        content = OperationHistoryPage();
        break;

      case PayoutDestination.methods:
        content = PaymentConfigPage(
          onWalletTap: provider.selectWallet,
        );
        break;

      case PayoutDestination.editwallet:
        final wallet = provider.walletsProvider?.selectedWallet;
        content = wallet != null
            ? WalletEditPage(
              wallet: wallet, 
              onBack: () => provider.goBackFromPayment(),
            )
            : const Center(child: Text('No wallet selected')); //TODO Localize
        break;

      default:
        content = Text(provider.selected.name);
    }

    return Scaffold(
      appBar: PayoutAppBar(
        title: Text(provider.selected.localizedLabel(context)),
        onAddFundsPressed: () {},
        onLogout: () => debugPrint('Logout clicked'),
      ),
      body: Padding(
        padding: const EdgeInsets.only(left: 200, top: 40, right: 200),
        child: Row(
          spacing: 40,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            PayoutSidebar(
              selected: provider.selected,
              onSelected: provider.selectPage,
              onLogout: () => debugPrint('Logout clicked'),
            ),
            Expanded(child: content),
          ],
        ),
      ),
    );
  }
}