import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/provider/account.dart';

import 'package:pweb/app/router/pages.dart';
import 'package:pweb/widgets/error/snackbar.dart';

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


class AccountLoader extends StatelessWidget {
  final Widget child;

  const AccountLoader({super.key, required this.child});

  @override
  Widget build(BuildContext context) => Consumer<AccountProvider>(builder: (context, provider, _) {
    if (provider.isLoading) return const Center(child: CircularProgressIndicator());
    if (provider.error != null) {
      postNotifyUserOfErrorX(
        context: context, 
        errorSituation: AppLocalizations.of(context)!.errorLogin, 
        exception: provider.error!,
      );
      navigateAndReplace(context, Pages.login);
    }
    if ((provider.error == null) && (provider.account == null)) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        provider.restore();
      });
      return const Center(child: CircularProgressIndicator());
    }
    return child;
  });
}

