import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:responsive_framework/responsive_framework.dart' as rf;
import 'package:salespro_admin/Route/static_string.dart';
import 'package:salespro_admin/Screen/Widgets/Constant%20Data/constant.dart';
import 'package:salespro_admin/top_bar/top_bar.dart';

import 'fotter.dart';
import 'global_side_bar.dart';

class ShellRouteWrapper extends StatefulWidget {
  const ShellRouteWrapper({
    super.key,
    required this.child,
  });

  final Widget child;

  @override
  State<ShellRouteWrapper> createState() => _ShellRouteWrapperState();
}

class _ShellRouteWrapperState extends State<ShellRouteWrapper> {
  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
  bool isLargeSidebarExpanded = false;

  bool _shouldHideFooter(BuildContext context) {
    final currentRoute = GoRouterState.of(context).uri.path;
    return currentRoute.startsWith('/sales/pos-sales') ||
        currentRoute.startsWith('/edit-pos-sale') ||
        currentRoute.startsWith('/edit-purchase') ||
        currentRoute.startsWith('/purchase/pos-purchase');
  }

  @override
  Widget build(BuildContext context) {
    final bool isLaptop = rf.ResponsiveBreakpoints.of(context).largerThan(BreakpointName.MD.name);
    final ThemeData theme = Theme.of(context);

    return Scaffold(
      key: scaffoldKey,
      // backgroundColor: isDark ? AcnooAppColors.kDark1 : AcnooAppColors.kPrimary50,
      drawer: isLaptop ? null : buildSidebar(isLargeSidebarExpanded), // Drawer for mobile
      bottomNavigationBar: isLaptop ? null : const FooterWidget(),
      body: rf.ResponsiveRowColumn(
        layout: rf.ResponsiveRowColumnType.ROW,
        rowCrossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Laptop & Desktop Sidebar
          if (isLaptop)
            rf.ResponsiveRowColumnItem(
              columnFit: FlexFit.loose,
              child: buildSidebar(isLargeSidebarExpanded),
            ),

          // Main Content
          rf.ResponsiveRowColumnItem(
            rowFit: FlexFit.tight,
            child: rf.ResponsiveRowColumn(
              layout: rf.ResponsiveRowColumnType.COLUMN,
              children: [
                // Static Topbar
                rf.ResponsiveRowColumnItem(
                  child: buildTopbar(isLaptop),
                ),

                // Route Pages
                rf.ResponsiveRowColumnItem(
                  columnFit: FlexFit.tight,
                  child: widget.child,
                ),

                // Footer
                if (isLaptop)
                  rf.ResponsiveRowColumnItem(
                    child: _shouldHideFooter(context) ? SizedBox.shrink() : FooterWidget(),
                  ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget buildTopbar(bool isLaptop) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        TopBarWidget(
          onMenuTap: () {
            if (isLaptop) {
              // Toggle sidebar expansion for desktop
              setState(() {
                isLargeSidebarExpanded = !isLargeSidebarExpanded;
              });
            } else {
              // Open drawer for mobile
              if (scaffoldKey.currentState != null && scaffoldKey.currentState!.isDrawerOpen == false) {
                scaffoldKey.currentState!.openDrawer();
              }
            }
          },
        ),
        Divider(
          height: 1,
          color: kNeutral200,
          thickness: 1,
        ),
      ],
    );
  }

  Widget buildSidebar(bool iconOnly) {
    return GlobalSideBar(
      rootScaffoldKey: scaffoldKey,
      iconOnly: iconOnly,
      // iconOnly: false,
    );
  }
}
