import 'package:driverapp/config/config.dart';
import 'package:driverapp/core/model/authentication/authenticated_user.dart';
import 'package:driverapp/core/model/authentication/login_request.dart';
import 'package:driverapp/core/service/base_service.dart';

class AuthenticationService extends BaseService {
  String selectedClinicName;
  bool isLogin = false;
  bool isLoading = true;
  AuthenticatedUser authenticatedUser;
  String token;
  String loginTokenId;
  String tokenID;
  int userID;

  AuthenticationService() {
//    getUserAuthentication();
  }

  login(LoginRequest loginRequest) async {
    hasError = false;
    try {
      await baseAppClient.post(LOGIN,
          onSuccess: (dynamic response, int statusCode) {
        authenticatedUser =
            AuthenticatedUser.fromJson(response['PatientER_DriverFile']);
        token = response['LogInTokenID'];
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: loginRequest.toJson());
    } catch (error) {
      hasError = true;
      super.error = error;
      throw error;
    }
  }

  getOpt(int driverId) async {
    hasError = false;
    userID = driverId;
    try {
      await baseAppClient.post(GET_OPT,
          onSuccess: (dynamic response, int statusCode) {
        loginTokenId = response['LogInTokenID'];
        print(response['ActivationCode']);
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: {"DriverID": driverId});
    } catch (error) {
      hasError = true;
      super.error = error;
      throw error;
    }
  }

  changePassword(String password, String confirmPassword) async {
    hasError = false;
    try {
      await baseAppClient.post(CHANGE_FORGOT_PASSWORD,
          onSuccess: (dynamic response, int statusCode) {
        loginTokenId = response['LogInTokenID'];
        print(response['ActivationCode']);
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: {
        "TokenID": tokenID,
        "NewPassword": password,
        "ConfirmPassword": confirmPassword,
        "UserID": userID
      });
    } catch (error) {
      hasError = true;
      super.error = error;
      throw error;
    }
  }

  checkActivationCode(int activationCode) async {
    hasError = false;
    try {
      await baseAppClient.post(CHECK_ACTIVATION_CODE,
          onSuccess: (dynamic response, int statusCode) {
        tokenID = response['TokenID'];
        print(response['ActivationCode']);
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: {
        "LogInTokenID": loginTokenId,
        "activationCode": activationCode
      });
    } catch (error) {
      hasError = true;
      super.error = error;
      throw error;
    }
  }
}
