import 'package:cims_apps/core/route/route.dart';
import 'package:cims_apps/features/auth/registration/view/initial_registration_step.dart';
import 'package:cims_apps/features/dashboard/dashboard_public/model/login_gmail_model.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';

class DashboardPublicViewModel extends ChangeNotifier {
  String emailGoogle = '';
  final GoogleSignIn googleSignIn = GoogleSignIn(
    scopes: [
      'email',
      'https://www.googleapis.com/auth/contacts.readonly',
    ],
  );
  Future<LoginGmailModel?> _getGmail() async {
    LoginGmailModel? loginGmailModel;
    try {
      final signInResult = await googleSignIn.signIn();
      if (signInResult != null) {
        emailGoogle = signInResult.email;
        final signInAuth = await signInResult.authentication;
        final accessToken = signInAuth.accessToken;
        final idToken = signInAuth.idToken;
        if (idToken != null && accessToken != null) {
          loginGmailModel = LoginGmailModel(
            accessToken: accessToken,
            idToken: idToken,
          );
        }
      }
    } catch (e) {
      debugPrint('catch error $e');
    }
    return loginGmailModel;
  }

  Future<bool> loginGoogle(BuildContext context) async {
    bool loginSuccess = false;
    loginSuccess = await _getGmail().then((payload) async {
      bool result = false;
      if (payload != null) {
        debugPrint('objectzz ${payload.toJson()}');
        routePush(context, page: InitialRegistrationStep());
        googleSignIn.disconnect();
      }
      return result;
    });
    return loginSuccess;
  }
}
