import 'package:bart/bart.dart' as bart;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:kasy_kit/core/bottom_menu/bottom_router.dart';
import 'package:kasy_kit/core/bottom_menu/kasy_bottom_bar_factory.dart';
import 'package:kasy_kit/core/sidebar/kasy_sidebar.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/core/widgets/responsive_layout.dart';

/// This bottom menu is powered by Bart packages
/// https://pub.dev/packages/bart
/// It allows you to create a bottom menu with a router and handle
/// all tabs navigation separately.
/// See the bottom_router.dart file to add tabs or subpages to show on tabs
class BottomMenu extends StatelessWidget {
  final String? initialRoute;

  const BottomMenu({super.key, this.initialRoute});

  @override
  Widget build(BuildContext context) {
    final String? resolvedInitialRoute = _resolveInitialRoute(initialRoute);
    final bool showBottomBarOnStart = _showBottomBarOnStart(
      resolvedInitialRoute,
    );
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: switch (Theme.brightnessOf(context)) {
        Brightness.dark => SystemUiOverlayStyle.light,
        Brightness.light => SystemUiOverlayStyle.dark,
      },
      child: ResponsiveLayout(
        small: bart.BartScaffold(
          routesBuilder: subRoutes,
          bottomBar: kasyPaddedSurfaceBottomBar(),
          initialRoute: resolvedInitialRoute,
          showBottomBarOnStart: showBottomBarOnStart,
          scaffoldOptions: bart.ScaffoldOptions(
            backgroundColor: context.colors.background,
          ),
          onRouteChanged: (route) {
            // If you want to log tab events to analytics
            // MixpanelAnalyticsApi.instance().logEvent('home/$route', {});
          },
        ),
        // medium (768–1024 px): icon-only collapsed rail
        medium: bart.BartScaffold(
          routesBuilder: subRoutes,
          bottomBar: kasyPaddedSurfaceBottomBar(),
          initialRoute: resolvedInitialRoute,
          showBottomBarOnStart: showBottomBarOnStart,
          scaffoldOptions: bart.ScaffoldOptions(
            backgroundColor: context.colors.background,
          ),
          sideBarOptions: bart.CustomSideBarOptions(
            sideBarBuilder: kasySidebarCollapsedBuilder,
          ),
          onRouteChanged: (route) {
            // If you want to log tab events to analytics
            // MixpanelAnalyticsApi.instance().logEvent('home/$route', {});
          },
        ),
        // large (1024 px+): full expanded sidebar
        large: bart.BartScaffold(
          routesBuilder: subRoutes,
          bottomBar: kasyPaddedSurfaceBottomBar(),
          initialRoute: resolvedInitialRoute,
          showBottomBarOnStart: showBottomBarOnStart,
          scaffoldOptions: bart.ScaffoldOptions(
            backgroundColor: context.colors.background,
          ),
          sideBarOptions: bart.CustomSideBarOptions(
            sideBarBuilder: kasySidebarBuilder,
          ),
          onRouteChanged: (route) {
            // If you want to log tab events to analytics
            // MixpanelAnalyticsApi.instance().logEvent('home/$route', {});
          },
        ),
      ),
    );
  }

  String? _resolveInitialRoute(String? route) {
    if (route != null) {
      return route;
    }
    if (!kIsWeb) {
      return null;
    }
    final path = _initialWebPath(Uri.base);
    final segments = Uri.parse(path).pathSegments;
    if (segments.length < 2) {
      return null;
    }
    return '/${segments.join('/')}';
  }

  String _initialWebPath(Uri uri) {
    final path = uri.path;
    if (path != '/' && path.isNotEmpty) {
      return path;
    }
    final fragment = uri.fragment;
    if (fragment.isNotEmpty) {
      final cleanFragment = fragment.startsWith('!/')
          ? fragment.substring(1)
          : fragment;
      final fragmentPath = cleanFragment.startsWith('/')
          ? cleanFragment
          : '/$cleanFragment';
      return Uri.parse(fragmentPath).path;
    }
    return path;
  }

  bool _showBottomBarOnStart(String? route) {
    if (route == null) {
      return true;
    }
    final segments = Uri.parse(route).pathSegments;
    if (segments.length < 2) {
      return true;
    }
    return false;
  }
}
