import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.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';
import 'package:hub/flutter_flow/nav/nav.dart';
import 'package:hub/pages/preferences_settings_page/preferences_settings_model.dart';
import 'package:hub/shared/utils/limited_text_size.dart';
import 'package:material_symbols_icons/symbols.dart';
import 'package:provider/provider.dart';

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

  @override
  _PreferencesPageWidgetState createState() => _PreferencesPageWidgetState();
}

class _PreferencesPageWidgetState extends State<PreferencesPageWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<PreferencesPageModel>(
      create: (_) => PreferencesPageModel(),
      child: Consumer<PreferencesPageModel>(builder: (context, model, child) {
        return GestureDetector(
          onTap: () => model.unfocusNode.canRequestFocus
              ? FocusScope.of(context).requestFocus(model.unfocusNode)
              : FocusScope.of(context).unfocus(),
          child: Scaffold(
            backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
            appBar: AppBar(
              backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
              automaticallyImplyLeading: false,
              forceMaterialTransparency: true,
              leading: FlutterFlowIconButton(
                key: ValueKey<String>('BackNavigationAppBar'),
                borderColor: Colors.transparent,
                borderRadius: 30.0,
                borderWidth: 1.0,
                buttonSize: 60.0,
                icon: Icon(
                  Icons.keyboard_arrow_left,
                  color: FlutterFlowTheme.of(context).primaryText,
                  size: 30.0,
                ),
                onPressed: () async {
                  context.pop();
                },
              ),
              title: Text(
                FFLocalizations.of(context).getVariableText(
                  enText: 'System Settings',
                  ptText: 'Opções do Sistema',
                ),
                style: FlutterFlowTheme.of(context).headlineMedium.override(
                      fontFamily:
                          FlutterFlowTheme.of(context).headlineMediumFamily,
                      color: FlutterFlowTheme.of(context).primaryText,
                      fontSize: 16.0,
                      fontWeight: FontWeight.bold,
                      letterSpacing: 0.0,
                      useGoogleFonts: GoogleFonts.asMap().containsKey(
                          FlutterFlowTheme.of(context).headlineMediumFamily),
                    ),
              ),
              centerTitle: true,
              elevation: 0.0,
            ),
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: [
                Container(),
                Expanded(
                  flex: 2,
                  child: ListView.builder(
                    itemCount: 9,
                    padding: const EdgeInsets.symmetric(horizontal: 20.0),
                    physics: const AlwaysScrollableScrollPhysics(),
                    itemBuilder: (BuildContext context, int index) {
                      return _buildIconButton(context, index, model);
                    },
                  ),
                ),
              ],
            ),
          ),
        );
      }),
    );
  }

  Widget _buildIconButton(
      BuildContext context, int index, PreferencesPageModel model) {
    IconData icon;
    Function() onPressed = () => {};
    bool isEnabled;
    String content;

    switch (index) {
      case 0:
        icon = Icons.fingerprint;
        onPressed = () => model.toggleFingerprint(context);
        isEnabled = model.isFingerprint;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Autenticar com impressão digital no QRCode',
          enText: 'Authenticate with fingerprint on the QRCode',
        );
        break;
      case 1:
        icon = Icons.share;
        onPressed = () => model.enablePerson(context);
        isEnabled = model.isPerson;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Compartilhar o código de identificação',
          enText: 'Share the identification code',
        );
        break;
      case 2:
        icon = Icons.transfer_within_a_station_outlined;
        onPressed = () => model.toggleIdentification(context);
        isEnabled = false;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Habilitar dispositivo para acesso',
          enText: 'Enable device for access',
        );
        break;
      case 3:
        icon = Icons.notifications;
        onPressed = () => model.toggleNotify(context);
        isEnabled = model.isNotify;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Receber notificação de acesso',
          enText: 'Receive access notification',
        );
        break;
      case 4:
        icon = Icons.lock;
        onPressed = () => model.toggleAccess(context);
        isEnabled = model.isAccess;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Registrar senha de acesso para o QRCode',
          enText: 'Register access password for the QRCode',
        );
        break;
      case 5:
        icon = Icons.lock_clock_sharp;
        onPressed = () => model.togglePanic(context);
        isEnabled = model.isPanic;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Registrar senha de pânico para o QRCode',
          enText: 'Register panic password for the QRCode',
        );
        break;
      case 6:
        icon = Symbols.digital_out_of_home;
        onPressed = () {
          model.localUnlink(context);
        };
        isEnabled = false;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Desvincular do local selecionado',
          enText: 'Unlink from selected location',
        );
        break;
      case 7:
        icon = Icons.delete;
        onPressed = () => model.deleteAccount(context);
        isEnabled = false;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Deletar conta permanentemente',
          enText: 'Delete account permanently',
        );
        break;
      case 8:
        icon = Icons.info_outline;
        onPressed = () => model.navAboutSystem(context);
        isEnabled = false;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Sobre o Sistema',
          enText: 'About the System',
        );
        break;
      default:
        throw Exception('Invalid index: $index');
    }

    return SizedBox(
      height: 66,
      child: GestureDetector(
        onTap: () {
          switch (index) {
            case 4:
              model.toggleAccess(context);
              break;
            case 5:
              model.togglePanic(context);
              break;
            default:
              onPressed();
          }
        },
        child: Row(
          children: [
            CircleAvatar(
              backgroundColor: isEnabled
                  ? FlutterFlowTheme.of(context).primary
                  : FlutterFlowTheme.of(context).alternate,
              child: Icon(
                icon,
                color: isEnabled
                    ? FlutterFlowTheme.of(context).primaryBackground
                    : FlutterFlowTheme.of(context).primary,
              ),
            ),
            const SizedBox(width: 8.0),
            Expanded(
              child: Text(
                content,
                style: FlutterFlowTheme.of(context).bodySmall.override(
                      fontFamily: 'Nunito',
                      color: FlutterFlowTheme.of(context).primaryText,
                      fontSize: LimitedFontSizeUtil.getInputFontSize(context),
                      letterSpacing: 0.0,
                      fontWeight: FontWeight.normal,
                      fontStyle: FontStyle.normal,
                    ),
                overflow: TextOverflow.clip,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
