import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:go_router/go_router.dart';

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

import 'package:pweb/pages/loader.dart';
import 'package:pweb/utils/logout.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/app/router/payout_routes.dart';


class PageSelector extends StatelessWidget {
  final Widget child;
  final GoRouterState routerState;

  const PageSelector({
    super.key,
    required this.child,
    required this.routerState,
  });

  @override
  Widget build(BuildContext context) => PageViewLoader(
    child: Builder(builder: (BuildContext context) {
      final permissions = context.read<PermissionsProvider>();
      if (!permissions.isReady) return Center(child: CircularProgressIndicator());

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

      final routeDestination = _destinationFromState(routerState);
      final selected = routeDestination != null && allowedDestinations.contains(routeDestination)
        ? routeDestination
        : fallbackDestination;

      if (selected != routeDestination) {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          context.goToPayout(selected);
        });
      }

      return Scaffold(
        appBar: PayoutAppBar(
          title: Text(selected.localizedLabel(context)),
          onAddFundsPressed: () {},
          onLogout: () => logoutUtil(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: context.goToPayout,
                onLogout: () => logoutUtil(context),
              ),
              Expanded(child: child),
            ],
          ),
        ),
      );
    },
  ));

  PayoutDestination? _destinationFromState(GoRouterState state) {
    final byName = PayoutRoutes.destinationFor(state.name);
    if (byName != null) return byName;

    final location = state.uri.path;
    if (location.startsWith(PayoutRoutes.editWalletPath)) {
      return PayoutDestination.editwallet;
    }
    if (location.startsWith(PayoutRoutes.walletTopUpPath)) {
      return PayoutDestination.walletTopUp;
    }
    if (location.startsWith(PayoutRoutes.methodsPath)) {
      return PayoutDestination.methods;
    }
    if (location.startsWith(PayoutRoutes.paymentPath)) {
      return PayoutDestination.payment;
    }
    if (location.startsWith(PayoutRoutes.editRecipientPath)) {
      return PayoutDestination.addrecipient;
    }
    if (location.startsWith(PayoutRoutes.addRecipientPath)) {
      return PayoutDestination.addrecipient;
    }
    if (location.startsWith(PayoutRoutes.recipientsPath)) {
      return PayoutDestination.recipients;
    }
    if (location.startsWith(PayoutRoutes.invitationsPath)) {
      return PayoutDestination.invitations;
    }
    if (location.startsWith(PayoutRoutes.settingsPath)) {
      return PayoutDestination.settings;
    }
    if (location.startsWith(PayoutRoutes.reportsPath)) {
      return PayoutDestination.reports;
    }
    if (location.startsWith(PayoutRoutes.dashboardPath)) {
      return PayoutDestination.dashboard;
    }
    return null;
  }
}
