import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:twonly/locator.dart';
import 'package:twonly/src/services/backup.service.dart';
import 'package:twonly/src/services/user.service.dart';
import 'package:twonly/src/utils/misc.dart';
import 'package:twonly/src/visual/components/alert.dialog.dart';
import 'package:twonly/src/visual/components/snackbar.dart';
import 'package:twonly/src/visual/elements/my_button.element.dart';
import 'package:twonly/src/visual/views/settings/backup/components/backup_setup.comp.dart';

class SetupBackupView extends StatefulWidget {
  const SetupBackupView({
    this.callBack,
    super.key,
  });

  // in case a callback is defined the callback
  // is called instead of the Navigator.pop()
  final VoidCallback? callBack;

  @override
  State<SetupBackupView> createState() => _SetupBackupViewState();
}

class _SetupBackupViewState extends State<SetupBackupView> {
  bool _isLoading = false;
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _repeatedController = TextEditingController();

  Future<void> _updateBackupPassword() async {
    if (!await isSecurePassword(_passwordController.text)) {
      if (!mounted) return;
      final ignore = await showAlertDialog(
        context,
        context.lang.backupInsecurePassword,
        context.lang.backupInsecurePasswordDesc,
        customCancel: context.lang.backupInsecurePasswordOk,
        customOk: context.lang.backupInsecurePasswordCancel,
      );
      if (ignore) {
        return;
      }
    }
    setState(() {
      _isLoading = true;
    });

    if (!mounted) return;
    final verified = await authenticateUser(
      context.lang.backupChangePasswordAuthReason,
    );
    if (!mounted) return;
    if (!verified) {
      showSnackbar(
        context,
        context.lang.backupChangePasswordAuthFailed,
      );
      return;
    }

    await Future.delayed(const Duration(milliseconds: 100));
    await BackupService.updateBackupPassword(_passwordController.text);
    await UserService.update((u) => u.isBackupEnabled = true);

    if (!mounted) return;
    setState(() {
      _isLoading = false;
    });

    if (widget.callBack != null) {
      widget.callBack!();
    } else {
      Navigator.pop(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusScope.of(context).unfocus(),
      child: Scaffold(
        appBar: AppBar(
          title: Text(context.lang.settingsBackup),
          actions: [
            IconButton(
              onPressed: () => showBackupExplanation(context),
              icon: const FaIcon(FontAwesomeIcons.circleInfo),
              iconSize: 18,
            ),
          ],
        ),
        body: Padding(
          padding: const EdgeInsetsGeometry.symmetric(
            vertical: 40,
            horizontal: 40,
          ),
          child: ListView(
            children: [
              Text(
                context.lang.backupSelectStrongPassword,
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 30),
              BackupPasswordTextField(
                controller: _passwordController,
                labelText: context.lang.password,
                onChanged: (value) => setState(() {}),
              ),
              PasswordRequirementText(
                text: context.lang.backupPasswordRequirement,
                showError:
                    _passwordController.text.length < 8 &&
                    _passwordController.text.isNotEmpty,
              ),
              const SizedBox(height: 5),
              BackupPasswordTextField(
                controller: _repeatedController,
                labelText: context.lang.passwordRepeated,
                onChanged: (value) => setState(() {}),
              ),
              PasswordRequirementText(
                text: context.lang.passwordRepeatedNotEqual,
                showError:
                    _passwordController.text != _repeatedController.text &&
                    _repeatedController.text.isNotEmpty,
              ),
              const SizedBox(height: 10),
              Center(
                child: MyButton(
                  variant: MyButtonVariant.primaryMiddle,
                  onPressed:
                      (!_isLoading &&
                          (_passwordController.text ==
                                      _repeatedController.text &&
                                  _passwordController.text.length >= 8 ||
                              !kReleaseMode))
                      ? _updateBackupPassword
                      : null,
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      if (_isLoading)
                        const SizedBox(
                          height: 12,
                          width: 12,
                          child: CircularProgressIndicator.adaptive(
                            strokeWidth: 1,
                          ),
                        )
                      else
                        const Icon(Icons.lock_clock_rounded),
                      const SizedBox(width: 8),
                      Text(
                        userService.currentUser.isBackupEnabled
                            ? context.lang.backupChangePassword
                            : context.lang.backupEnableBackup,
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
