import 'package:flutter/material.dart';

import 'package:provider/provider.dart';
import 'package:pshared/models/resources.dart';

import 'package:pshared/provider/account.dart';
import 'package:pshared/provider/permissions.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/payout_page/page.dart';
import 'package:pweb/pages/payout_page/wallet/edit/page.dart';
import 'package:pweb/pages/report/page.dart';
import 'package:pweb/pages/settings/profile/page.dart';
import 'package:pweb/pages/dashboard/dashboard.dart';
import 'package:pweb/providers/page_selector.dart';
import 'package:pweb/app/router/pages.dart';
import 'package:pweb/widgets/appbar/app_bar.dart';
import 'package:pweb/widgets/sidebar/destinations.dart';
import 'package:pweb/widgets/sidebar/sidebar.dart';

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


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

  void _logout(BuildContext context) {
    context.read<AccountProvider>().logout();
    navigateAndReplace(context, Pages.login);
  }

  @override
  Widget build(BuildContext context) {
    final provider = context.watch<PageSelectorProvider>();
    final permissions = context.watch<PermissionsProvider>();
    final loc = AppLocalizations.of(context)!;

    final bool restrictedAccess = !permissions.canRead(ResourceType.chainWallets);
    final allowedDestinations = restrictedAccess
      ? <PayoutDestination>{
          PayoutDestination.settings,
          PayoutDestination.methods,
          PayoutDestination.editwallet,
        }
      : PayoutDestination.values.toSet();

    final selected = allowedDestinations.contains(provider.selected)
      ? provider.selected
      : (restrictedAccess ? PayoutDestination.settings : PayoutDestination.dashboard);

    if (selected != provider.selected) {
      WidgetsBinding.instance.addPostFrameCallback((_) => provider.selectPage(selected));
    }

    Widget content;
    switch (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(
          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(
              onBack: provider.goBackFromWalletEdit,
            )
            : Center(child: Text(loc.noWalletSelected));
        break;

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

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