import 'package:flutter/material.dart';
import 'package:hub/backend/schema/enums/enums.dart';
import 'package:hub/components/atomic_components/menu_button_item/menu_button_item_widget.dart';
import 'package:hub/components/atomic_components/menu_card_item/menu_card_item.dart';
import 'package:hub/components/molecular_components/menu_item/menu_item.dart';

import '/components/organism_components/menu_list_view_component/menu_list_view_component_widget.dart';
import '/components/organism_components/menu_staggered_view_component/menu_staggered_view_component_widget.dart';
import '/flutter_flow/flutter_flow_util.dart';
import 'menu_component_model.dart';

export 'menu_component_model.dart';

class MenuComponentWidget extends StatefulWidget {
  const MenuComponentWidget({
    Key? key,
    required this.style,
    required this.item,
    required this.expandable,
  });
  final MenuView style;
  final MenuItem item;
  final bool expandable;
  @override
  State<MenuComponentWidget> createState() => _MenuComponentWidgetState();
}

class _MenuComponentWidgetState extends State<MenuComponentWidget> {
  late MenuComponentModel _model;

  @override
  void setState(VoidCallback callback) {
    super.setState(callback);
    _model.onUpdate();
  }

  @override
  void initState() {
    super.initState();
    _model = createModel(context, () => MenuComponentModel());
  }

  @override
  void dispose() {
    _model.maybeDispose();

    super.dispose();
  }

  // MenuButtonWidget(icon: FFIcons.kvector2, action: () async {setState(() {});}, title: FFLocalizations.of(context).getVariableText(enText:'Link\nCondominum' , ptText:'' ,),),
  // MenuButtonWidget(icon: FFIcons.kpets, action: () async {setState(() {});}, title: FFLocalizations.of(context).getVariableText(enText:'Register\Pet' , ptText:'' ,),),

  @override
  Widget build(BuildContext context) {
    final options = () {
      if (widget.item == MenuItem.button) {
        if (_model.isGrid == true)
          return <MenuEntry>[
            MenuButtonWidget(
              icon: FFIcons.kvector1,
              action: () async {
                await _model.showSchedules(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Schedule\nVisit',
                ptText: 'Agendar\nVisita',
              ),
            ),
            MenuButtonWidget(
              icon: FFIcons.khome,
              action: () async {
                await _model.openVisitorsRegister(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Register\nVisitor',
                ptText: 'Cadastro\nde Visitante',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.qr_code,
              action: () async {
                await _model.openQRCodeScanner(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'QRCode\nAccess',
                ptText: 'QRCode\nde Acesso',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.pets,
              action: () async {
                await _model.openPetsRegister(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Pets\nRegister',
                ptText: 'Cadastro\nde Pet',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.people,
              action: () async {
                await _model.openPoepleOnTheProperty(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'People on\nthe Property',
                ptText: 'Pessoas na\nPropriedade',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.history_sharp,
              action: () async {
                await _model.showHistories(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Consult\nHistories',
                ptText: 'Consultar\nHistóricos',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.settings,
              action: () async {
                await _model.openPreferencesSettings(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Preferences\nSettings',
                ptText: 'Opções\ndo Sistema',
              ),
            ),
          ];
        else
          return <MenuEntry>[
            MenuButtonWidget(
              icon: Icons.engineering_outlined,
              action: () async {
                await _model.openProvisionalSchedule(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Schedule\nProviders',
                ptText: 'Agendar\nPrestadores',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.sports_motorsports_outlined,
              action: () async {
                await _model.openDeliverySchedule(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Schedule\nDeliveries',
                ptText: 'Agendar\nEntregas',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.attach_email_outlined,
              action: () async {
                await _model.openFastPassSchedule(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                ptText: 'Convidar\nVisitante',
                enText: 'Invite\nVisitor',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.event,
              action: () async {
                await _model.openCompleteSchedule(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Complete\nSchedule',
                ptText: 'Agenda\nCompleta',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.inventory_2_outlined,
              action: () async {
                await _model.openMyOrders(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                ptText: 'Minhas\nEncomendas',
                enText: 'My\nOrders',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.event_available,
              action: () async {
                await _model.openReservations(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                ptText: 'Reservas',
                enText: 'Reservations',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.person_add_alt_1_outlined,
              action: () async {
                await _model.openVisitorsRegister(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Register\nVisitor',
                ptText: 'Cadastrar\nVisitante',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.qr_code,
              action: () async {
                await _model.openQRCodeScanner(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'QRCode\nAccess',
                ptText: 'QRCode\nde Acesso',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.pets,
              action: () async {
                await _model.openPetsRegister(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Pets\nRegister',
                ptText: 'Cadastrar\nPet',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.transfer_within_a_station_outlined,
              action: () async {
                await _model.openAccessHistory(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Access\nHistory',
                ptText: 'Consultar\nAcessos',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.how_to_reg_outlined,
              action: () async {
                await _model.openLiberationsHistory(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Liberations\nHistory',
                ptText: 'Consultar\nLiberações',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.chat_outlined,
              action: () async {
                await _model.openMessagesHistory(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'Message\nHistory',
                ptText: 'Consultar\nMensagens',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.groups,
              action: () async {
                await _model.openPoepleOnTheProperty(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'People on\nthe Property',
                ptText: 'Pessoas na\nPropriedade',
              ),
            ),
            MenuButtonWidget(
              icon: Icons.settings,
              action: () async {
                await _model.openPreferencesSettings(context);
                setState(() {});
              },
              title: FFLocalizations.of(context).getVariableText(
                enText: 'System\n Settings',
                ptText: 'Opções\ndo Sistema',
              ),
            ),
          ];
      }
      if (widget.item == MenuItem.card) {
        return <MenuEntry>[
          MenuCardItem(
            icon: FFIcons.kvector1,
            action: () async {
              await _model.showSchedules(context);
              setState(() {});
            },
            title: FFLocalizations.of(context).getVariableText(
              enText: 'Schedule\nVisit',
              ptText: 'Agendar\nVisita',
            ),
          ),
          MenuCardItem(
            icon: FFIcons.khome,
            action: () async {
              await _model.openVisitorsRegister(context);
              setState(() {});
            },
            title: FFLocalizations.of(context).getVariableText(
              enText: 'Register\nVisitor',
              ptText: 'Cadastro\nde Visitante',
            ),
          ),
          MenuCardItem(
            icon: Icons.qr_code,
            action: () async {
              await _model.openQRCodeScanner(context);
              setState(() {});
            },
            title: FFLocalizations.of(context).getVariableText(
              enText: 'QRCode\nAccess',
              ptText: 'QRCode\nde Acesso',
            ),
          ),
          MenuCardItem(
            icon: Icons.people,
            action: () async {
              await _model.openPoepleOnTheProperty(context);
              setState(() {});
            },
            title: FFLocalizations.of(context).getVariableText(
              enText: 'People on\nthe Property',
              ptText: 'Pessoas\nna Propriedade',
            ),
          ),
          MenuCardItem(
            icon: Icons.history_sharp,
            action: () async {
              await _model.showHistories(context);
              setState(() {});
            },
            title: FFLocalizations.of(context).getVariableText(
              enText: 'Consult\nHistories',
              ptText: 'Consultar\nHistoricos',
            ),
          ),
          MenuCardItem(
            icon: Icons.inventory_2_rounded,
            action: () async {
              await _model.openMyOrders(context);
              setState(() {});
            },
            title: FFLocalizations.of(context).getVariableText(
              enText: 'Orders',
              ptText: 'Encomendas',
            ),
          ),
          MenuButtonWidget(
            icon: Icons.pets,
            action: () async {
              await _model.openPetsRegister(context);
              setState(() {});
            },
            title: FFLocalizations.of(context).getVariableText(
              enText: 'Pets\nRegister',
              ptText: 'Cadastrar\nPet',
            ),
          ),
          MenuCardItem(
            icon: Icons.event_available,
            action: () async {
              await _model.openReservations(context);
              setState(() {});
            },
            title: FFLocalizations.of(context).getVariableText(
              enText: 'Reservations',
              ptText: 'Reservas',
            ),
          ),
          MenuCardItem(
            icon: Icons.settings,
            action: () async {
              await _model.openPreferencesSettings(context);
              setState(() {});
            },
            title: FFLocalizations.of(context).getVariableText(
              enText: 'Preferences\nSettings',
              ptText: 'Preferências\nde Configuração',
            ),
          ),
        ];
      }
      // if (MenuItem.tile)
      return <MenuEntry>[
        MenuCardItem(
          icon: Icons.engineering_outlined,
          action: () async {
            await _model.openProvisionalSchedule(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'Schedule Providers',
            ptText: 'Agendar Prestadores',
          ),
        ),
        MenuCardItem(
          icon: Icons.sports_motorsports_outlined,
          action: () async {
            await _model.openDeliverySchedule(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'Schedule Deliveries',
            ptText: 'Agendar Entregas',
          ),
        ),
        MenuCardItem(
          icon: Icons.attach_email_outlined,
          action: () async {
            await _model.openFastPassSchedule(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            ptText: 'Convidar Visitante',
            enText: 'Invite Visitor',
          ),
        ),
        MenuCardItem(
          icon: Icons.event,
          action: () async {
            await _model.openCompleteSchedule(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'Complete Schedule',
            ptText: 'Agenda Completa',
          ),
        ),
        MenuCardItem(
          icon: Icons.inventory_2_outlined,
          action: () async {
            await _model.openMyOrders(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            ptText: 'Minhas Encomendas',
            enText: 'My Orders',
          ),
        ),
        MenuCardItem(
          icon: Icons.event_available,
          action: () async {
            await _model.openReservations(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            ptText: 'Reservas',
            enText: 'Reservations',
          ),
        ),
        MenuCardItem(
          icon: Icons.person_add_alt_1_outlined,
          action: () async {
            await _model.openVisitorsRegister(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'Register Visitor',
            ptText: 'Cadastrar Visitante',
          ),
        ),
        MenuCardItem(
          icon: Icons.qr_code,
          action: () async {
            await _model.openQRCodeScanner(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'QRCode Access',
            ptText: 'QRCode de Acesso',
          ),
        ),
        MenuCardItem(
          icon: Icons.pets,
          action: () async {
            await _model.openPetsRegister(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'Pets Register',
            ptText: 'Cadastro de Pet',
          ),
        ),
        MenuCardItem(
          icon: Icons.transfer_within_a_station_outlined,
          action: () async {
            await _model.openAccessHistory(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'Access History',
            ptText: 'Consultar Acessos',
          ),
        ),
        MenuCardItem(
          icon: Icons.how_to_reg_outlined,
          action: () async {
            await _model.openLiberationsHistory(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'Liberations History',
            ptText: 'Consultar Liberações',
          ),
        ),
        MenuCardItem(
          icon: Icons.chat_outlined,
          action: () async {
            await _model.openMessagesHistory(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'Message History',
            ptText: 'Consultar Mensagens',
          ),
        ),
        MenuCardItem(
          icon: Icons.groups,
          action: () async {
            await _model.openPoepleOnTheProperty(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'People on the Property',
            ptText: 'Pessoas na Propriedade',
          ),
        ),
        MenuCardItem(
          icon: Icons.settings,
          action: () async {
            await _model.openPreferencesSettings(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'System Settings',
            ptText: 'Opções do Sistema',
          ),
        ),
        MenuCardItem(
          icon: Icons.logout,
          action: () async {
            await _model.Logout(context);
            setState(() {});
          },
          title: FFLocalizations.of(context).getVariableText(
            enText: 'Logout Account',
            ptText: 'Sair da Conta',
          ),
        ),
      ];
    }();

    return Builder(
      builder: (context) {
        if (widget.style == MenuView.list_grid && widget.expandable == true && widget.item == MenuItem.button) {
          if (_model.isGrid == true) {
            return wrapWithModel(
              model: _model.menuStaggeredViewComponentModel,
              updateCallback: () => setState(() {}),
              updateOnChange: true,
              child: MenuStaggeredViewComponentWidget(
                options: options,
                expandable: widget.expandable,
                item: widget.item,
                changeMenuStyle: () async {
                  await _model.changeMenuStyle(context);
                  setState(() {});
                },
                isGrid: _model.isGrid,
              ),
            );
          } else {
            return wrapWithModel(
              model: _model.menuStaggeredViewComponentModel,
              updateCallback: () => setState(() {}),
              updateOnChange: true,
              child: MenuStaggeredViewComponentWidget(
                options: options,
                expandable: widget.expandable,
                item: widget.item,
                changeMenuStyle: () async {
                  await _model.changeMenuStyle(context);
                  setState(() {});
                },
                isGrid: _model.isGrid,
              ),
            );
          }
        }
        if (widget.style == MenuView.list && widget.expandable == false && widget.item == MenuItem.tile) {
          return wrapWithModel(
            model: _model.menuListViewComponentModel,
            updateCallback: () => setState(() {}),
            updateOnChange: true,
            child: MenuListViewComponentWidget(
              options: options,
              expandable: widget.expandable,
              item: widget.item,
              changeMenuStyle: () async {
                await _model.changeMenuStyle(context);
                setState(() {});
              },
            ),
          );
        }
        return const SizedBox();
      },
    );
  }
}
