import 'package:bart/bart.dart' as bart;
import 'package:bart/bart/bart_model.dart';
import 'package:bart/bart/router_delegate.dart';
import 'package:bart/bart/widgets/bottom_bar/styles/bottom_bar_cupertino.dart';
import 'package:bart/bart/widgets/bottom_bar/styles/bottom_bar_custom.dart';
import 'package:bart/bart/widgets/bottom_bar/styles/bottom_bar_material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:kasy_kit/core/haptics/kasy_haptics.dart';
import 'package:kasy_kit/core/theme/theme.dart';

/// Breathing room under the top hairline so icons/labels are not glued to it.
const double kasyBottomBarTopPadding = 6;

const double _kBottomBarHeightMaterial3 = 88;
const double _kBottomBarHeightCupertino = 56;

/// [BartScaffold] requires a [bart.BartBottomBar]. This factory draws the same
/// Material / Cupertino bars as [bart.BartBottomBar.adaptive], with extra top
/// inset inside the chrome (not only a taller bar).
final class KasyPaddedSurfaceBottomBarFactory extends BartBottomBarFactory {
  const KasyPaddedSurfaceBottomBarFactory();

  bool _useCupertino(BuildContext context) {
    if (kIsWeb) {
      return false;
    }
    return switch (defaultTargetPlatform) {
      TargetPlatform.iOS => true,
      _ => false,
    };
  }

  @override
  Widget create({
    required BuildContext context,
    required List<BartMenuRoute> routes,
    required void Function(int) onTap,
    required int currentIndex,
  }) {
    final KasyColors colors = context.colors;
    final MenuRouter menuRouter = MenuRouter.of(context);
    final BorderSide topEdge = BorderSide(
      color: colors.outline.withValues(alpha: 0.35),
    );
    void handleTap(int index) {
      if (menuRouter.indexNotifier.value == index &&
          menuRouter.routingTypeNotifier.value ==
              BartMenuRouteType.bottomNavigation) {
        return;
      }
      KasyHaptics.selection(context);
      final nestedContext = menuRouter.navigationKey.currentContext;
      if (nestedContext != null) {
        Navigator.of(nestedContext)
            .pushReplacementNamed(menuRouter.routesBuilder()[index].path);
      }
    }

    if (_useCupertino(context)) {
      return Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          DecoratedBox(
            decoration: BoxDecoration(
              color: colors.surface,
              border: Border(top: topEdge),
            ),
            child: const SizedBox(
              height: kasyBottomBarTopPadding,
              width: double.infinity,
            ),
          ),
          BartCupertinoBottomBar(
            routes: routes,
            theme: CupertinoBottomBarTheme(
              bgColor: colors.surface,
              height: _kBottomBarHeightCupertino - kasyBottomBarTopPadding,
            ),
            currentIndexNotifier: menuRouter.indexNotifier,
            onTap: handleTap,
          ),
        ],
      );
    }
    return Material(
      color: colors.surface,
      elevation: 2,
      shadowColor: Colors.black.withValues(alpha: 0.12),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const SizedBox(
            height: kasyBottomBarTopPadding,
            width: double.infinity,
          ),
          Theme(
            data: Theme.of(context).copyWith(
              splashFactory: NoSplash.splashFactory,
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent,
              navigationBarTheme: NavigationBarTheme.of(context).copyWith(
                backgroundColor: Colors.transparent,
                elevation: 0,
                shadowColor: Colors.transparent,
                surfaceTintColor: Colors.transparent,
                indicatorColor: Colors.transparent,
                iconTheme: WidgetStateProperty.resolveWith((states) {
                  final selected = states.contains(WidgetState.selected);
                  return IconThemeData(
                    color: selected ? colors.primary : colors.muted,
                  );
                }),
                labelTextStyle: WidgetStateProperty.resolveWith((states) {
                  final selected = states.contains(WidgetState.selected);
                  final base = Theme.of(context).textTheme.labelMedium ??
                      const TextStyle();
                  return base.copyWith(
                    color: selected ? colors.primary : colors.muted,
                  );
                }),
              ),
            ),
            child: BartMaterial3BottomBar(
              routes: menuRouter.routesBuilder(),
              theme: Material3BottomBarTheme(
                bgColor: Colors.transparent,
                elevation: 0,
                height: _kBottomBarHeightMaterial3 - kasyBottomBarTopPadding,
              ),
              currentIndexNotifier: menuRouter.indexNotifier,
              onTap: handleTap,
            ),
          ),
        ],
      ),
    );
  }
}

bart.BartBottomBar kasyPaddedSurfaceBottomBar() {
  return bart.BartBottomBar.custom(
    bottomBarFactory: const KasyPaddedSurfaceBottomBarFactory(),
  );
}
