import 'package:flutter/material.dart';
import 'package:initial_folder/providers/firebase_authentication_provider.dart';
import 'package:initial_folder/providers/incomplete_profile_provider.dart';
import 'package:initial_folder/providers/login_provider.dart';
import 'package:initial_folder/providers/theme_provider.dart';
import 'package:initial_folder/screens/home/home_screen.dart';
import 'package:initial_folder/screens/login/components/get_user_data.dart';
import 'package:initial_folder/screens/login/login_with_email/login_email_screen.dart';
import 'package:initial_folder/screens/profile/account_sign_in/incomplete_profile_screen.dart';
import 'package:initial_folder/screens/registrasi/registrasi_with_email/registrasi_email_screen.dart';
import 'package:initial_folder/screens/splash/splash_screen_login.dart';
import 'package:provider/provider.dart';

import '../../../size_config.dart';
import '../../../theme.dart';

class LoginGoogle {
  Future handleLoginGoogle(BuildContext context) async {
    bool googleLogged = false;
    bool isAccountNotRegistered = false;
    final provider =
        Provider.of<FirebaseAuthenticationProvider>(context, listen: false);
         final themeProvider = Provider.of<ThemeProvider>(context, listen: false);

    try {
      googleLogged = await provider.googleLogin();
    } catch (e) {
      if (e.toString() == 'Exception: Akun anda belum terdaftar') {
        isAccountNotRegistered = true;
        showDialog(
          context: context,
          builder: (context) => AlertDialog(
            backgroundColor: Theme.of(context).colorScheme.background,
            surfaceTintColor: Colors.transparent,
            insetPadding:
                const EdgeInsets.symmetric(horizontal: 25, vertical: 20),
            content: Container(
              margin: EdgeInsets.only(top: 10, left: 15, right: 15),
              height: 70,
              alignment: AlignmentDirectional.topStart,
              child: Text(
                'Akun anda belum terdaftar, silahkan daftar terlebih dahulu',
                style: thirdTextStyle.copyWith(
                  fontSize: getProportionateScreenWidth(12),
                  color: Theme.of(context).colorScheme.onBackground,
                ),
              ),
            ),
            actions: [
              Container(
                margin: EdgeInsets.only(right: 25),
                alignment: AlignmentDirectional.topEnd,
                child: GestureDetector(
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => RegistrationEmail(),
                      ),
                    );
                  },
                  child: Text(
                    'Daftar',
                    style: primaryTextStyle.copyWith(
                      fontSize: getProportionateScreenWidth(12),
                      letterSpacing: 1,
                      color: primaryColor,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      } else {
        print('Error: ${e.toString()}');
      }
      Provider.of<LoginProvider>(context, listen: false)
          .loadGoogleActive(false);
    }

    if (googleLogged) {
      await getUserData(context);

      Condition.loginFirebase = true;

      IncompleteProfileProvider userInfoIncomplete =
          Provider.of<IncompleteProfileProvider>(context, listen: false);

      // [!CHECKPOINT!]:
      await userInfoIncomplete.getUserInfoIncomplete();

      // [!CHECKPOINT!]:
      if (userInfoIncomplete.isUserInfoComplete == null) {
        Navigator.of(context).pushNamedAndRemoveUntil(
            LoginEmail.routeName, (Route<dynamic> route) => false);
        return;
      }

      if (userInfoIncomplete.isUserInfoComplete!) {
        Navigator.of(context).pushNamedAndRemoveUntil(
            HomeScreen.routeName, (Route<dynamic> route) => false);
      } else {
        Navigator.of(context).pushNamedAndRemoveUntil(
            IncompleteProfile.routeName, (Route<dynamic> route) => false);
      }
    } else if (!isAccountNotRegistered) {
      void fn() => showDialog(
            context: context,
            builder: (context) => AlertDialog(
              backgroundColor: Theme.of(context).colorScheme.background,
              surfaceTintColor: Colors.transparent,
              insetPadding:
                  const EdgeInsets.symmetric(horizontal: 25, vertical: 20),
              content: Container(
                margin: EdgeInsets.only(top: 10, left: 15, right: 15),
                height: 70,
                alignment: AlignmentDirectional.topStart,
                child: Text(
                  'Ada kendala dalam login, coba lagi atau periksa akun anda',
                  style: thirdTextStyle.copyWith(
                    fontSize: getProportionateScreenWidth(12),
                    color: Theme.of(context).colorScheme.onBackground,
                  ),
                ),
              ),
              actions: [
                Container(
                  margin: EdgeInsets.only(right: 25),
                  alignment: AlignmentDirectional.topEnd,
                  child: GestureDetector(
                    onTap: () {
                      Navigator.pop(context);
                    },
                    child: Text(
                      'Tutup',
                      style: primaryTextStyle.copyWith(
                        fontSize: getProportionateScreenWidth(12),
                        letterSpacing: 1,
                        color: themeProvider.themeData == ThemeClass.darkmode
                              ?primaryColor : primaryColorligtmode,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          );
      fn();
    }
    Provider.of<LoginProvider>(context, listen: false).loadGoogleActive(false);
  }
}
