import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hub/app_state.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/internationalization.dart';
import 'package:hub/flutter_flow/nav/nav.dart';
import 'package:hub/pages/preferences_settings_page/preferences_settings_model.dart';
import 'package:provider/provider.dart';

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

  @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: 'Preferences',
                  ptText: 'Preferências',
                ),
                style: FlutterFlowTheme.of(context).headlineMedium.override(
                      fontFamily: 'Nunito',
                      color: FlutterFlowTheme.of(context).primaryText,
                      fontSize: 17.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(
                    // gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    //   crossAxisCount: 3,
                    //   crossAxisSpacing: 12.0,
                    //   mainAxisSpacing: 12.0,
                    //   childAspectRatio: 1.0,
                    //   mainAxisExtent: 100.0,
                    // ),
                    itemCount: 7, // Assuming 4 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); // Disable if fingerprint is false
        isEnabled = FFAppState().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 = FFAppState().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 = FFAppState().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;
        // onLongPress = model.togglePass(context, model);
        isEnabled = FFAppState().pass;
        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;
        // onLongPress = model.togglePass(context, model);
        isEnabled = FFAppState().panic;
        content = FFLocalizations.of(context).getVariableText(
          ptText: 'Ative para inserir uma credencial de pânico para o QRCode',
          enText: 'Enable to enter an panic credential for the QRCode',
        );
        break;
      case 5:
        icon = Icons.landscape;
        onPressed = () => model.localUnlink(context);
        isEnabled = true;
        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 = true;
        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;
      default:
        throw Exception('Invalid index: $index');
    }

    return SizedBox(
      height: 100,
      child: GestureDetector(
        onTap: () {
          switch (index) {
            case 3:
              model.togglePass(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,
                // icon: Icon(icon, color: isEnabled ? FlutterFlowTheme.of(context).primaryBackground : FlutterFlowTheme.of(context).primary, size: 40.0),
                // onPressed: index != 3 ? onPressed : () {model.togglePass(context);},
                // borderRadius: 20.0,
                // borderWidth: 1.0,
                // buttonSize: 40.0,
                // fillColor: isEnabled ? FlutterFlowTheme.of(context).primary : FlutterFlowTheme.of(context).alternate,
                // disabledColor: FlutterFlowTheme.of(context).alternate,
                // disabledIconColor: FlutterFlowTheme.of(context).primary,
              ),
            ),
            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,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
