import 'package:fcs/domain/entities/setting.dart';
import 'package:fcs/domain/entities/user.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/signin/invitation_request_page.dart';
import 'package:fcs/pages/signin/signup_page.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';

navigateAfterAuthVerified(BuildContext context) async {
  User user = Provider.of<MainModel>(context, listen: false).user;
  Setting setting = Provider.of<MainModel>(context, listen: false).setting;

  if (setting == null) return;

  if (user != null && (user.joined || user.requested)) {
    Navigator.pushNamedAndRemoveUntil(context, "/home", (r) => false);
  } else {
    if (setting.inviteRequired) {
      bool invited =
          await Provider.of<MainModel>(context, listen: false).hasInvite();
      if (!invited) {
        await Navigator.of(context).pushAndRemoveUntil(
            CupertinoPageRoute(builder: (context) => RequestInvitationPage()),
            (r) => false);
        return;
      }
    }
    await Navigator.of(context).pushAndRemoveUntil(
        CupertinoPageRoute(builder: (context) => SignupPage()), (r) => false);
  }
}
