import 'package:SEFER/constant/box_name.dart'; import 'package:SEFER/constant/colors.dart'; import 'package:SEFER/constant/links.dart'; import 'package:SEFER/controller/auth/captin/login_captin_controller.dart'; import 'package:SEFER/controller/functions/crud.dart'; import 'package:SEFER/main.dart'; import 'package:SEFER/views/auth/captin/cards/sms_signup.dart'; import 'package:SEFER/views/home/on_boarding_page.dart'; import 'package:get/get.dart'; import 'package:google_sign_in/google_sign_in.dart'; import '../../onbording_page.dart'; import '../../views/auth/captin/ai_page.dart'; class GoogleSignInHelper { static final GoogleSignIn _googleSignIn = GoogleSignIn( scopes: [ 'email', 'profile', ], ); // Method to handle Google Sign-In static Future signIn() async { try { final GoogleSignInAccount? googleUser = await _googleSignIn.signIn(); if (googleUser != null) { await _handleSignUp(googleUser); if (box.read(BoxName.countryCode) == 'Egypt') { Get.to(() => SmsSignupEgypt()); } else if (box.read(BoxName.countryCode) == 'Jordan') { Get.to(() => AiPage()); } } return googleUser; } catch (error) { return null; } } // static Future signInFromLogin() async { // // try { // // final GoogleSignInAccount? googleUser = await _googleSignIn.signIn(); // // if (googleUser != null) { // // await _handleSignUp(googleUser); // // // if (box.read(BoxName.countryCode) == 'Egypt') { // // await Get.find().loginUsingCredentials( // // box.read(BoxName.driverID).toString(), // // box.read(BoxName.emailDriver).toString(), // // ); // // // } else if (box.read(BoxName.countryCode) == 'Jordan') { // // // // Get.to(() => AiPage()); // // // } // // } // // return googleUser; // // } catch (error) { // // return null; // // } // try { // final GoogleSignInAccount? googleUser = await _googleSignIn.signIn(); // if (googleUser != null) { // // Handle sign-up logic // await _handleSignUp(googleUser); // // // Get country code // final String countryCode = box.read(BoxName.countryCode).toString(); // final String driverID = box.read(BoxName.driverID).toString(); // final String emailDriver = box.read(BoxName.emailDriver).toString(); // // // Log-in using credentials based on country code // if (countryCode == 'Egypt') { // await Get.find() // .loginUsingCredentials(driverID, emailDriver); // } else if (countryCode == 'Jordan') { // // Add logic for Jordan if needed, e.g., navigate to AiPage // // Get.to(() => AiPage()); // } // } // return googleUser; // } catch (error) { // Get.snackbar('Google Sign-In error', '$error', // backgroundColor: AppColor.redColor); // // Log error details // print('Google Sign-In error: $error'); // return null; // } // } Future signInFromLogin() async { try { final GoogleSignInAccount? googleUser = await _googleSignIn.signIn(); if (googleUser != null) { await _handleSignUp(googleUser); // Add detailed debug print statements final driverID = box.read(BoxName.driverID)?.toString() ?? 'Unknown ID'; final emailDriver = box.read(BoxName.emailDriver)?.toString() ?? 'Unknown Email'; // print('Driver ID: $driverID'); // print('Email Driver: $emailDriver'); await Get.find() .loginUsingCredentials(driverID, emailDriver); } return googleUser; } catch (error) { Get.snackbar('Google Sign-In error', '$error', backgroundColor: AppColor.redColor); addError(error.toString(), 'GoogleSignInAccount?> signInFromLogin()'); print('Google Sign-In error: $error'); return null; } } addError(String error, where) async { CRUD().post(link: AppLink.addError, payload: { 'error': error.toString(), // Example error description 'userId': box.read(BoxName.driverID) ?? box.read(BoxName.passengerID), // Example user ID 'userType': box.read(BoxName.driverID) != null ? 'Driver' : 'passenger', // Example user type 'phone': box.read(BoxName.phone) ?? box.read(BoxName.phoneDriver), // Example phone number 'device': where }); } // Method to handle Google Sign-Out static Future signOut() async { try { await _googleSignIn.signOut(); await _handleSignOut(); } catch (error) {} } static Future _handleSignOut() async { // Clear stored driver information box.remove(BoxName.driverID); box.remove(BoxName.emailDriver); box.remove(BoxName.lang); box.remove(BoxName.nameDriver); box.remove(BoxName.passengerID); box.remove(BoxName.phoneDriver); box.remove(BoxName.tokenFCM); box.remove(BoxName.tokens); box.remove(BoxName.carPlate); box.remove(BoxName.lastNameDriver); box.remove(BoxName.agreeTerms); box.remove(BoxName.tokenDriver); box.remove(BoxName.countryCode); box.remove(BoxName.accountIdStripeConnect); box.remove(BoxName.phoneVerified); Get.offAll(OnBoardingPage()); // Perform any additional sign-out tasks or API calls here // For example, you can notify your server about the user sign-out } // Method to get the current signed-in user static GoogleSignInAccount? getCurrentUser() { return _googleSignIn.currentUser; } // Method to handle sign-up process static Future _handleSignUp(GoogleSignInAccount user) async { // Store driver information box.write(BoxName.driverID, user.id); box.write(BoxName.emailDriver, user.email); // box.write(BoxName.nameDriver, user.displayName); // box.write(BoxName.driverPhotoUrl, user.photoUrl); print(box.read(BoxName.driverID).toString()); print( box.read(BoxName.emailDriver).toString(), ); // Perform any additional sign-up tasks or API calls here // For example, you can send the user data to your server for registration } }