import 'package:test_sa/controllers/api_routes/urls.dart';
import 'package:test_sa/models/user.dart';

import 'api_client.dart';

class UserApiClient {
  static final UserApiClient _instance = UserApiClient._internal();

  /// ### This instance will be [NULL] until the login or registration process completed successfully by calling [login] or [register] functions
  User? user;

  UserApiClient._internal();

  factory UserApiClient() => _instance;

  /// - [user] object have to contains username & password
  /// - If the request completed successfully the [UserApiClient.user] object inside [UserApiClient] class won't be null and will contains the data comes from the response.
  /// - Returns exception of type [APIException] if any error happened.
  ///
  ///#### lib\client\login_api_client.dart
  Future login({required User user}) async {
    return await ApiClient().postJsonForObject(
      (json) {
        this.user = User.fromJson(json[0]);
      },
      "${URLs.host1}${URLs.login}",
      await user.toLoginJson(), //body
      isFormData: true,
    );
  }

  /// - [newUser] object have to contains the new user data [email, phone, ...]
  /// - If the request completed successfully the [user] object inside [UserApiClient] class won't be null and will contains the data comes from the response.
  /// - Returns exception of type [APIException] if any error happened.
  ///
  ///#### lib\client\login_api_client.dart
  Future register({required User newUser}) async {
    return await ApiClient().postJsonForObject(
      (json) {
        user = User.fromJson(json);
      },
      "${URLs.host1}${URLs.register}",
      await newUser.toRegisterJson(), //body
    );
  }

  /// [updatedUser] have to contains the new data for the current user
  /// - If the request completed successfully the [user] object inside [UserApiClient] class won't be null and will contains the data comes from the response.
  /// - Returns exception of type [APIException] if any error happened.
  ///
  ///#### lib\client\login_api_client.dart
  Future updateProfile({required User updatedUser}) async {
    return await ApiClient().postJsonForObject(
      (json) {
        user = User.fromJson(json);
      },
      "${URLs.host1}${URLs.updateProfile}",
      updatedUser.toUpdateProfileJson(), //body
    );
    // Map<String, dynamic> jsonObject = {};
    // jsonObject["uid"] = user.id;
    // jsonObject["token"] = user.token;
    // if (user.department?.id != user.department?.id) {
    //   jsonObject["department"] = user.department?.id;
    // }
    // if (user.whatsApp != user.whatsApp) {
    //   jsonObject["whatsapp"] = user.whatsApp;
    // }
    // if (user.phoneNumber != user.phoneNumber) {
    //   jsonObject["phone"] = user.phoneNumber;
    // }
    // final response = await ApiClient().postJsonForResponse(URLs.updateProfile, jsonObject);
    //
    // if (response.statusCode >= 200 && response.statusCode < 300) {
    //   // client's request was successfully received
    //   this.user = User.fromJson(jsonDecode(utf8.decode(response.bodyBytes))[0]);
    //   this.user?.hospital = user.hospital;
    //   this.user?.department = user.department;
    // }
  }
}
