import 'package:dio/dio.dart';
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
import 'package:syncrow_app/services/api/http_interceptor.dart';
import 'package:syncrow_app/services/locator.dart';

class HTTPService {
  final Dio client = serviceLocator.get<Dio>();

  // final navigatorKey = GlobalKey<NavigatorState>();

  String certificateString = "";

  static Dio setupDioClient() {
    Dio client = Dio(
      BaseOptions(
        baseUrl: ApiEndpoints.baseUrl,
        receiveDataWhenStatusError: true,
        followRedirects: false,
        connectTimeout: const Duration(seconds: 30),
        receiveTimeout: const Duration(seconds: 30),
      ),
    );

    client.interceptors.add(serviceLocator.get<HTTPInterceptor>());
    return client;
  }

  Future<T> get<T>({
    required String path,
    Map<String, dynamic>? queryParameters,
    required T Function(dynamic) expectedResponseModel,
    bool showServerMessage = true,
  }) async {
    try {
      final response = await client.get(
        path,
        queryParameters: queryParameters,
      );
      return expectedResponseModel(response.data);
    } catch (error) {
      rethrow;
    }
  }

  Future<T> post<T>(
      {required String path,
      Map<String, dynamic>? queryParameters,
      Options? options,
      dynamic body,
      bool showServerMessage = true,
      String? accessToken,
      required T Function(dynamic) expectedResponseModel}) async {
    try {
      final authOptions = options ??
          Options(
            headers: accessToken != null
                ? {
                    'Authorization': 'Bearer $accessToken',
                    'Content-Type': 'application/json',
                  }
                : {
                    'Content-Type': 'application/json',
                  },
          );

      final response = await client.post(
        path,
        data: body,
        queryParameters: queryParameters,
        options: authOptions,
      );
      return expectedResponseModel(response.data);
    } catch (error) {
      rethrow;
    }
  }

  Future<T> patch<T>(
      {required String path,
      Map<String, dynamic>? queryParameters,
      dynamic body,
      required T Function(dynamic) expectedResponseModel}) async {
    try {
      final response = await client.patch(
        path,
        data: body,
        queryParameters: queryParameters,
      );
      return expectedResponseModel(response.data);
    } catch (error) {
      rethrow;
    }
  }

  Future<T> put<T>(
      {required String path,
      Map<String, dynamic>? queryParameters,
      dynamic body,
      required T Function(dynamic) expectedResponseModel}) async {
    try {
      final response = await client.put(
        path,
        data: body,
        queryParameters: queryParameters,
      );
      return expectedResponseModel(response.data);
    } catch (error) {
      rethrow;
    }
  }

  Future<T> download<T>(
      {required String path,
      required String savePath,
      Map<String, dynamic>? queryParameters,
      required T Function(dynamic) expectedResponseModel}) async {
    try {
      final response = await client.download(
        path,
        savePath,
        onReceiveProgress: (current, total) {},
      );

      return expectedResponseModel(response.data);
      // return expectedResponseModel(response.data);
    } catch (error) {
      rethrow;
    }
  }

  Future<T> delete<T>({
    required String path,
    Map<String, dynamic>? queryParameters,
    required T Function(dynamic) expectedResponseModel,
    bool showServerMessage = true,
  }) async {
    try {
      final response = await client.delete(
        path,
        queryParameters: queryParameters,
      );
      return expectedResponseModel(response.data);
    } catch (error) {
      rethrow;
    }
  }
}
