import 'package:flutter/material.dart';
import 'package:flutter_gutter/flutter_gutter.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:moneymgr_mobile/providers/auth_state.dart';
import 'package:moneymgr_mobile/routes/login/base_auth_page.dart';
import 'package:moneymgr_mobile/services/api/api_token.dart';
import 'package:moneymgr_mobile/services/router/routes_list.dart';
import 'package:moneymgr_mobile/utils/extensions.dart';
import 'package:moneymgr_mobile/widgets/app_button.dart';

class ManualAuthScreen extends HookConsumerWidget {
  const ManualAuthScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    var apiUrlController = useTextEditingController();
    var tokenIdController = useTextEditingController();
    var tokenValueController = useTextEditingController();

    Future<void> onSubmit() async {
      try {
        await ref
            .read(currentAuthStateProvider.notifier)
            .setAuthToken(
              ApiToken(
                apiUrl: apiUrlController.text,
                tokenId: int.tryParse(tokenIdController.text) ?? 1,
                tokenValue: tokenValueController.text,
              ),
            );

        if (context.mounted) {
          if (context.canPop()) context.pop();
          context.replace(profilePage);
        }
      } catch (e, s) {
        Logger.root.severe("Failed to authenticate user! $e $s");
        if (context.mounted) {
          context.showTextSnackBar("Failed to authenticate user! $e");
        }
      }
    }

    return BaseAuthPage(
      title: "Manual authentication",
      showSettings: false,
      children: [
        Gutter(scaleFactor: 3),
        Text(
          "On this screen you can manually enter authentication information.",
        ),
        Gutter(),
        TextField(
          controller: apiUrlController,
          keyboardType: TextInputType.url,
          decoration: const InputDecoration(
            labelText: 'API URL',
            helperText: "Format: http://moneymgr.corp.com/api",
          ),
          textInputAction: TextInputAction.next,
        ),
        Gutter(),
        TextField(
          controller: tokenIdController,
          keyboardType: TextInputType.number,
          decoration: const InputDecoration(
            labelText: 'Token ID',
            helperText: "The ID of the token",
          ),
          textInputAction: TextInputAction.next,
        ),
        Gutter(),
        TextField(
          controller: tokenValueController,
          keyboardType: TextInputType.text,
          decoration: const InputDecoration(
            labelText: 'Token value',
            helperText: "The value of the token itself",
          ),
          textInputAction: TextInputAction.done,
        ),
        Gutter(),
        AppButton(onPressed: onSubmit, label: "Submit"),
        Gutter(scaleFactor: 3),
      ],
    );
  }
}
