import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import '../../app/config/env_loader.dart';
import 'interceptor/auth_interceptor.dart';
import 'interceptor/exception_interceptor.dart';
import 'interceptor/logger_interceptor.dart';
import 'interceptor/request_interceptor.dart';

const int connectTimeout = 30;
const int receiveTimeout = 30;

class DioHttpService {
  DioHttpService._internal();
  static final DioHttpService _instance = DioHttpService._internal();
  factory DioHttpService() => _instance;
  Dio get dio => _dio;

  late final Dio _dio =
      Dio(
          BaseOptions(
            baseUrl: AppConfig.current.baseUrl,
            connectTimeout: const Duration(seconds: connectTimeout),
            receiveTimeout: const Duration(seconds: receiveTimeout),
            contentType: 'application/json',
            responseType: ResponseType.json,
            validateStatus: (_) => true,
            receiveDataWhenStatusError: true,
          ),
        )
        ..interceptors.add(RequestInterceptor())
        ..interceptors.addAll(kReleaseMode ? const [] : [LoggerInterceptor()])
        ..interceptors.add(AuthInterceptor())
        ..interceptors.add(ExceptionInterceptor());

  void setDefaultHeaders(Map<String, dynamic> headers) {
    dio.options.headers.addAll(headers);
  }
}