import 'package:flutter/material.dart';

import 'package:pweb/app/router/pages.dart';
import 'package:pweb/widgets/vspacer.dart';
import 'package:pweb/utils/error/handler.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';


class ErrorPage extends StatelessWidget {
  final String title;
  final String errorMessage;
  final String errorHint;
  final Widget? action;

  const ErrorPage({
    super.key,
    required this.title,
    required this.errorMessage,
    required this.errorHint,
    this.action,
  });

  @override
  Widget build(BuildContext context) => Center(
    child: Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            Icons.error_outline,
            size: 72,
            color: Theme.of(context).colorScheme.error,
          ),
          const VSpacer(),
          Text(
            title,
            style: Theme.of(context).textTheme.titleLarge?.copyWith(
              color: Theme.of(context).colorScheme.error,
            ),
            textAlign: TextAlign.center,
          ),
          const VSpacer(multiplier: 0.5),
          ListTile(
            title: Text(
              errorMessage,
              textAlign: TextAlign.center,
              style: Theme.of(context).textTheme.titleLarge,
            ),
            subtitle: Text(
              errorHint,
              textAlign: TextAlign.center,
              style: Theme.of(context).textTheme.bodySmall,
            ),
          ),
          const VSpacer(multiplier: 1.5),
          if (action != null) ...[action!, const VSpacer(multiplier: 0.5)],
          TextButton(
            onPressed: () => navigate(context, Pages.root),
            child: Text(AppLocalizations.of(context)!.goToMainPage),
          ),
        ],
      ),
    ),
  );
}

Widget exceptionToErrorPage({
  required BuildContext context,
  required String title,
  required String errorMessage,
  required Object exception,
  Widget? action,
}) => ErrorPage(
  title: title,
  errorMessage: errorMessage,
  errorHint: ErrorHandler.handleError(context, exception),
  action: action,
);
