import 'package:alert_dialog/alert_dialog.dart';
import 'package:confirm_dialog/confirm_dialog.dart';
import 'package:flutter/material.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/services/api/api_client.dart';
import 'package:moneymgr_mobile/services/router/routes_list.dart';
import 'package:moneymgr_mobile/services/storage/prefs.dart';

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

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final data = ref.watch(prefsProvider);
    final api = ref.watch(apiServiceProvider);
    if (data.value == null) return CircularProgressIndicator();

    final profile = data.value?.authInfo();

    void onSettingsPressed() => context.push(settingsPage);

    handleSignOut() async {
      try {
        if (!await confirm(
          context,
          content: Text("Do you really want to sign out of your account?"),
        )) {
          return;
        }

        await ref.read(currentAuthStateProvider.notifier).logout();
      } catch (e, s) {
        Logger.root.warning("Failed to sign out! $e $s");

        if (context.mounted) {
          await alert(context, content: Text("Failed to sign you out! $e"));
        }
      }
    }

    return Scaffold(
      appBar: AppBar(
        title: Text("Profile"),
        actions: [
          IconButton(
            onPressed: onSettingsPressed,
            icon: const Icon(Icons.settings),
          ),
        ],
      ),
      body: ListView(
        children: [
          ListEntry(
            title: "Server URL",
            value: api?.token.apiUrl,
            icon: Icons.link,
          ),
          ListEntry(
            title: "Token ID",
            value: api?.token.tokenId.toString(),
            icon: Icons.key,
          ),
          ListEntry(
            title: "User ID",
            value: profile?.id.toString(),
            icon: Icons.perm_contact_calendar_outlined,
          ),
          ListEntry(
            title: "User name",
            value: profile?.name,
            icon: Icons.person,
          ),
          ListEntry(title: "User mail", value: profile?.mail, icon: Icons.mail),
          Divider(),
          ListEntry(
            title: "Sign out",
            icon: Icons.logout,
            onTap: handleSignOut,
          ),
        ],
      ),
    );
  }
}

class ListEntry extends StatelessWidget {
  final String title;
  final String? value;
  final IconData icon;
  final Function()? onTap;

  const ListEntry({
    super.key,
    required this.title,
    this.value,
    required this.icon,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(title),
      subtitle: value != null ? Text(value!) : null,
      leading: Icon(icon),
      onTap: onTap,
    );
  }
}
