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/storage_util.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) => 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(
                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: 'Nunito',
                      color: FlutterFlowTheme.of(context).primaryText,
                      fontSize: 15.0,
                      letterSpacing: 0.0,
                      useGoogleFonts: GoogleFonts.asMap().containsKey('Nunito'),
                    ),
              ),
              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: 7, // Assuming 7 items for simplicity
                    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 = StorageUtil().fingerprint;
        content = FFLocalizations.of(context).getVariableText(
          ptText:
              'Ative a autenticação por impressão digital para login seguro.',
          enText: 'Enable fingerprint authentication for secure login.',
        );
        break;
      case 1:
        icon = Icons.person;
        onPressed = () => model.enablePerson(context);
        isEnabled = StorageUtil().person;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Compartilhe o código de identificação remota',
          enText: 'Share the remote identification code',
        );
        break;
      case 2:
        icon = Icons.notifications;
        onPressed = () => model.toggleNotify(context);
        isEnabled = StorageUtil().notify;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Ative para receber sua notificação de acesso',
          enText: 'Enable to receive your access notification',
        );
        break;
      case 3:
        icon = Icons.lock;
        onPressed = () => model.toggleAccess(context);
        isEnabled = StorageUtil().access;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Ative para inserir uma credencial de acesso para o QRCode',
          enText: 'Enable to enter an access credential for the QRCode',
        );
        break;
      case 4:
        icon = Icons.lock_clock_sharp;
        onPressed = () => model.togglePanic(context);
        isEnabled = StorageUtil().panic;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Ative para inserir uma credencial de pânico para o QRCode',
          enText: 'Enable to enter a panic credential for the QRCode',
        );
        break;
      case 5:
        icon = Icons.landscape;
        onPressed = () {
          model.localUnlink(context);
        };
        isEnabled = false;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Desative para se desvincular do local selecionado',
          enText: 'Enable to unlink from the selected location',
        );
        break;
      case 6:
        icon = Icons.delete;
        onPressed = () => model.deleteAccount(context);
        isEnabled = false;
        content = FFLocalizations.of(context).getVariableText(
          ptText:
              'Delete sua conta e todos os dados associados permanentemente.',
          enText: 'Delete your account and all associated data permanently.',
        );
        break;
      case 7:
        icon = Icons.logout;
        onPressed = () => model.logout(context);
        isEnabled = false;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Sair da conta atual e voltar para a tela de login.',
          enText:
              'Log out of the current account and return to the login screen.',
        );
        break;
      default:
        throw Exception('Invalid index: $index');
    }

    return SizedBox(
      height: 100,
      child: GestureDetector(
        onTap: () {
          switch (index) {
            case 3:
              model.toggleAccess(context);
              break;
            case 4:
              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: 14.0,
                      letterSpacing: 0.0,
                      fontWeight: FontWeight.normal,
                      fontStyle: FontStyle.normal,
                    ),
                overflow: TextOverflow.clip,
              ),
            ),
          ],
        ),
      ),
    );
  }


}
