import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:garage/auth/widget/login_page.dart';
import 'package:provider/provider.dart';

import '../../home_page.dart';
import '../../user/model/user_provider.dart';

class AuthPage extends StatelessWidget {
  const AuthPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder<User?>(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (BuildContext context, AsyncSnapshot<User?> snapshot) {
          if (snapshot.hasData) {
            // user is logged in
            final UserProvider userProvider = context.watch<UserProvider>();
            if (userProvider.currentUser == null) {
              print("AuthPage is fetching user");
              userProvider.updateUser();
              return _buildFakePage(context);
            }
            return HomePage();
          }
          return LoginPage();
        },
      ),
    );
  }

  Widget _buildFakePage(context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.primary,
        centerTitle: true,
        title: Text("Moje Garáž",
            style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white)),
        actions: <Widget>[
          IconButton(
              icon: const Icon(
                Icons.notifications,
                color: Colors.white,
              ),
              onPressed: () {})
        ],
      ),
    );
  }
}
