import 'dart:developer';

import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

import 'package:hub/components/atomic_components/shared_components_atoms/atom_image_svg_theme.dart';
import 'package:hub/features/auth/index.dart';
import 'package:hub/features/backend/index.dart';
import 'package:hub/features/notification/index.dart';
import 'package:hub/features/storage/index.dart';
import 'package:hub/flutter_flow/flutter_flow_theme.dart';
import 'package:hub/flutter_flow/flutter_flow_util.dart';
import 'package:hub/flutter_flow/flutter_flow_widgets.dart';
import 'package:hub/pages/reception_page/reception_page_model.dart';
import 'package:hub/features/local/index.dart';

import 'package:hub/shared/utils/limited_text_size.dart';
import 'package:provider/provider.dart';

class ReceptionPageWidget extends StatefulWidget {
  const ReceptionPageWidget({super.key});

  @override
  State<ReceptionPageWidget> createState() => _ReceptionPageWidgetState();
}

class _ReceptionPageWidgetState extends State<ReceptionPageWidget>
    with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    () async {
      final lifecycle = await AwesomeNotifications().getAppLifeCycle();
    }();

    FirebaseMessagingService().updateDeviceToken();
    LocalsRepositoryImpl().validateLocal(context);
  }

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

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      setState(() {
        LocalsRepositoryImpl().validateLocal(context);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    double limitedHeaderFontSize =
        LimitedFontSizeUtil.getHeaderFontSize(context);
    double limitedBodyFontSize = LimitedFontSizeUtil.getBodyFontSize(context);
    double limitedInputFontSize = LimitedFontSizeUtil.getInputFontSize(context);

    StorageHelper().context = context;
    return ChangeNotifierProvider(
      create: (context) => ReceptionPageModel(),
      child: Scaffold(
        body: Consumer<ReceptionPageModel>(
          builder: (context, ReceptionPageModel model, child) {
            return SafeArea(
              child: Center(
                child: SingleChildScrollView(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      Padding(
                        padding: const EdgeInsets.fromLTRB(45, 20, 45, 15.0),
                        child: Text(
                          FFLocalizations.of(context).getVariableText(
                              ptText:
                                  'Falta pouco para você utilizar o FRE Access Hub...',
                              enText:
                                  'You are close to using the FRE Access Hub...'),
                          textAlign: TextAlign.center,
                          style: FlutterFlowTheme.of(context)
                              .displayLarge
                              .override(
                                fontFamily: 'Plus Jakarta Sans',
                                color: FlutterFlowTheme.of(context).accent1,
                                fontSize: limitedHeaderFontSize,
                                letterSpacing: 0.0,
                                fontWeight: FontWeight.w700,
                                useGoogleFonts: GoogleFonts.asMap()
                                    .containsKey('Plus Jakarta Sans'),
                              ),
                        ),
                      ),
                      const AtomImageSvgTheme(
                          filename: 'reception', width: 180, height: 180),
                      Padding(
                        padding: const EdgeInsets.fromLTRB(70, 30, 70.0, 40),
                        child: Text(
                          FFLocalizations.of(context).getVariableText(
                              ptText:
                                  'Envie seu identificador para seu condomínio para vincularem sua conta aos nossos sistemas.',
                              enText:
                                  'Send your identifier to your condominium to link your account to our systems.'),
                          textAlign: TextAlign.center,
                          style: FlutterFlowTheme.of(context)
                              .displaySmall
                              .override(
                                fontFamily: 'Nunito Sans',
                                color: FlutterFlowTheme.of(context).primaryText,
                                fontSize: limitedBodyFontSize,
                                letterSpacing: 0.0,
                                fontWeight: FontWeight.w600,
                                useGoogleFonts: GoogleFonts.asMap()
                                    .containsKey('Plus Jakarta Sans'),
                              ),
                        ),
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          Padding(
                            padding: const EdgeInsets.fromLTRB(60, 0, 60, 10),
                            child: Tooltip(
                              message: FFLocalizations.of(context).getVariableText(
                                  ptText:
                                      'Seu identificador é utilizado para efetuar\no vínculo do seu APP com o condominio.',
                                  enText: 'My Identifier'),
                              textStyle: FlutterFlowTheme.of(context)
                                  .labelSmall
                                  .override(
                                    fontFamily: 'Nunito Sans',
                                    color: FlutterFlowTheme.of(context)
                                        .secondaryText,
                                    fontSize: limitedInputFontSize,
                                    letterSpacing: 0.0,
                                    fontWeight: FontWeight.w500,
                                    useGoogleFonts: GoogleFonts.asMap()
                                        .containsKey('Plus Jakarta Sans'),
                                  ),
                              child: FFButtonWidget(
                                onPressed: () => model.getIdenfifier(context),
                                text: FFLocalizations.of(context)
                                    .getVariableText(
                                        ptText: 'Meu Identificador',
                                        enText: 'My Identifier'),
                                options: FFButtonOptions(
                                  width: double.infinity,
                                  height: 30,
                                  padding: const EdgeInsetsDirectional.fromSTEB(
                                      0.0, 0.0, 0.0, 0.0),
                                  iconPadding:
                                      const EdgeInsetsDirectional.fromSTEB(
                                          0.0, 0.0, 0.0, 0.0),
                                  color: FlutterFlowTheme.of(context).primary,
                                  textStyle: FlutterFlowTheme.of(context)
                                      .titleSmall
                                      .override(
                                        fontFamily: 'Nunito Sans',
                                        color:
                                            FlutterFlowTheme.of(context).info,
                                        fontSize: limitedInputFontSize,
                                        letterSpacing: 0.0,
                                        fontWeight: FontWeight.w500,
                                        useGoogleFonts: GoogleFonts.asMap()
                                            .containsKey('Plus Jakarta Sans'),
                                      ),
                                  elevation: 3.0,
                                  borderSide: const BorderSide(
                                    color: Colors.transparent,
                                    width: 1.0,
                                  ),
                                  borderRadius: BorderRadius.circular(12.0),
                                ),
                                showLoadingIndicator: false,
                              ),
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.fromLTRB(60, 0, 60, 25),
                            child: FFButtonWidget(
                              onPressed: () async {
                                PhpGroup.unregisterDevice();
                                AuthenticationService.signOut(context);
                                setState(() {});
                              },
                              text: FFLocalizations.of(context).getVariableText(
                                  ptText: 'Sair da Conta', enText: 'Logout'),
                              options: FFButtonOptions(
                                width: double.infinity,
                                height: 30,
                                padding: const EdgeInsetsDirectional.fromSTEB(
                                    0.0, 0.0, 0.0, 0.0),
                                iconPadding:
                                    const EdgeInsetsDirectional.fromSTEB(
                                        0.0, 0.0, 0.0, 0.0),
                                color:
                                    FlutterFlowTheme.of(context).customColor1,
                                textStyle: FlutterFlowTheme.of(context)
                                    .titleSmall
                                    .override(
                                      fontFamily: 'Nunito Sans',
                                      color: FlutterFlowTheme.of(context)
                                          .primaryBackground,
                                      fontSize: limitedInputFontSize,
                                      letterSpacing: 0.0,
                                      fontWeight: FontWeight.w500,
                                      useGoogleFonts: GoogleFonts.asMap()
                                          .containsKey('Plus Jakarta Sans'),
                                    ),
                                elevation: 3.0,
                                borderSide: const BorderSide(
                                  color: Colors.transparent,
                                  width: 1.0,
                                ),
                                borderRadius: BorderRadius.circular(12.0),
                              ),
                              showLoadingIndicator: false,
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}
