import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hub/features/home/presentation/widgets/drawer_widget.dart';
import 'package:hub/features/local/index.dart';
import 'package:hub/features/menu/index.dart';
import 'package:hub/flutter_flow/flutter_flow_icon_button.dart';
import 'package:hub/flutter_flow/flutter_flow_theme.dart';
import 'package:hub/flutter_flow/flutter_flow_util.dart';

class HomePageWidget extends StatefulWidget {
  const HomePageWidget(this.update, {super.key});
  final Future<bool> Function(BuildContext context)? update;

  @override
  State<HomePageWidget> createState() => _HomePageWidgetState();
}

class _HomePageWidgetState extends State<HomePageWidget>
    with WidgetsBindingObserver {
  final scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await LocalsRepositoryImpl().check(context);
      if (widget.update != null) {
        await widget.update!(context);
      }
    });
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Builder(
      builder: (context) {
        // LocalsRepositoryImpl.license.add(false);
        // context.read<LocalProfileBloc>().add(LocalProfileEvent());
        // context.read<MenuBloc>().add(MenuEvent());
        LocalsRepositoryImpl.license.add(true);

        return Scaffold(
          key: scaffoldKey,
          backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
          drawerEnableOpenDragGesture: true,
          drawerDragStartBehavior: DragStartBehavior.start,
          drawer: DrawerWidget(),
          appBar: buildAppBar(context),
          body: buildPage(context),
        );
      },
    );
  }

  AppBar buildAppBar(BuildContext context) {
    return AppBar(
      backgroundColor: FlutterFlowTheme.of(context).primary,
      automaticallyImplyLeading: false,
      leading: FlutterFlowIconButton(
        borderRadius: 20.0,
        borderWidth: 1.0,
        buttonSize: 40.0,
        fillColor: FlutterFlowTheme.of(context).primary,
        icon: const Icon(
          Icons.menu_rounded,
          color: Colors.white,
          size: 28.0,
        ),
        onPressed: () async {
          scaffoldKey.currentState!.openDrawer();
        },
      ),
      title: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(8.0),
            child: Image.asset(
              'assets/images/logo.png',
              width: 15.0,
              height: 15.0,
              fit: BoxFit.cover,
            ),
          ),
          Text(
            'FRE ACCESS',
            style: FlutterFlowTheme.of(context).bodyMedium.override(
                  fontFamily: FlutterFlowTheme.of(context).bodyMediumFamily,
                  color: FlutterFlowTheme.of(context).info,
                  letterSpacing: 0.0,
                  useGoogleFonts: GoogleFonts.asMap().containsKey(
                      FlutterFlowTheme.of(context).bodyMediumFamily),
                ),
          ),
        ].divide(const SizedBox(width: 8.0)),
      ),
      actions: const [],
      centerTitle: true,
      elevation: 0.0,
    );
  }

  Container buildPage(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: FlutterFlowTheme.of(context).primary,
      ),
      child: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Wrap(
              spacing: 0.0,
              runSpacing: 0.0,
              alignment: WrapAlignment.start,
              crossAxisAlignment: WrapCrossAlignment.start,
              direction: Axis.horizontal,
              runAlignment: WrapAlignment.start,
              verticalDirection: VerticalDirection.down,
              clipBehavior: Clip.none,
              children: [
                buildLocal(),
                buildBody(),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget buildBody() {
    return Container(
      color: FlutterFlowTheme.of(context).primaryBackground,
      child: Padding(
        padding: const EdgeInsets.only(bottom: 40),
        child: Menufactory(
          entry: MenuEntry.getEntriesByType(MenuEntryType.Home),
          item: EnumMenuItem.button,
          view: MenuView.list_grid,
        ),
      ),
    );
  }

  Widget buildLocal() {
    return LocalProfileComponentWidget();
  }
}
