import 'package:client_settings/src/presentation/navigation/client_settings_navigator.dart';
import 'package:core_localization/core_localization.dart';
import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_modular/flutter_modular.dart';
import '../../blocs/client_settings_bloc.dart';

/// A widget that displays the primary actions for the settings page.
class SettingsActions extends StatelessWidget {
  /// Creates a [SettingsActions].
  const SettingsActions({super.key});

  @override
  /// Builds the settings actions UI.
  Widget build(BuildContext context) {
    // Get the translations for the client settings profile.
    final TranslationsClientSettingsProfileEn labels =
        t.client_settings.profile;

    return SliverPadding(
      padding: const EdgeInsets.symmetric(horizontal: UiConstants.space5),
      sliver: SliverList(
        delegate: SliverChildListDelegate(<Widget>[
          const SizedBox(height: UiConstants.space5),

           /// TODO: FEATURE_NOT_YET_IMPLEMENTED
           // Edit profile is not yet implemented

          // Hubs button
          UiButton.primary(
            text: labels.hubs,
            onPressed: () => Modular.to.pushHubs(),
          ),
          const SizedBox(height: UiConstants.space4),

          // Log out button
          BlocBuilder<ClientSettingsBloc, ClientSettingsState>(
            builder: (BuildContext context, ClientSettingsState state) {
              return UiButton.secondary(
                text: labels.log_out,
                onPressed: state is ClientSettingsLoading
                    ? null
                    : () => _showSignOutDialog(context),
              );
            },
          ),
        ]),
      ),
    );
  }

  /// Shows a confirmation dialog for signing out.
  Future<void> _showSignOutDialog(BuildContext context) {
    return showDialog(
      context: context,
      builder: (BuildContext context) => AlertDialog(
        backgroundColor: UiColors.bgPopup,
        elevation: 0,
        shape: RoundedRectangleBorder(borderRadius: UiConstants.radiusLg),
        title: Text(
          t.client_settings.profile.log_out,
          style: UiTypography.headline3m.textPrimary,
        ),
        content: Text(
          'Are you sure you want to log out?',
          style: UiTypography.body2r.textSecondary,
        ),
        actions: <Widget>[
          // Log out button
          UiButton.secondary(
            text: t.client_settings.profile.log_out,
            onPressed: () {
              Modular.to.pop();
              BlocProvider.of<ClientSettingsBloc>(
                context,
              ).add(const ClientSettingsSignOutRequested());
            },
          ),

          // Cancel button
          UiButton.secondary(
            text: t.common.cancel,
            onPressed: () => Modular.to.pop(),
          ),
        ],
      ),
    );
  }
}
