import 'package:dio/dio.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/data/api/atmob_api.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';

import '../data/api/atmob_file_api.dart';
import '../data/api/atmob_stream_api.dart';
import '../data/consts/constants.dart';
import '../utils/stream_dio_log_interceptor.dart';
import '../data/consts/build_config.dart';

@module
abstract class NetworkModule {
  @singleton
  @Named("defaultDio")
  Dio createDefaultDio() {
    Dio dio = Dio(BaseOptions());
    dio.interceptors.add(
      PrettyDioLogger(
        requestHeader: true,
        requestBody: true,
        responseBody: true,
        responseHeader: false,
        error: true,
        compact: true,
        enabled: BuildConfig.isDebug,
      ),
    );
    return dio;
  }

  @singleton
  @Named("fileDio")
  Dio createFileDio() {
    Dio dio = Dio(BaseOptions(
      sendTimeout: const Duration(seconds: 15),
      receiveTimeout: const Duration(seconds: 15),
    ));
    dio.interceptors.add(
      PrettyDioLogger(
        requestHeader: true,
        requestBody: true,
        responseBody: true,
        responseHeader: false,
        error: true,
        compact: true,
        enabled: BuildConfig.isDebug,
      ),
    );
    return dio;
  }

  @singleton
  @Named("streamDio")
  Dio createStreamDio() {
    Dio streamDio = Dio(
      BaseOptions(
        responseType: ResponseType.stream,
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'text/event-stream, application/json',
        },
      ),
    );
    streamDio.interceptors.add(StreamDioLogInterceptor());
    return streamDio;
  }

  @singleton
  AtmobApi provideAtmobApi(@Named("defaultDio") Dio dio) {
    return AtmobApi(dio, baseUrl: Constants.baseUrl);
  }

  @singleton
  AtmobFileApi provideAtmobFileApi(@Named("fileDio") Dio dio) {
    return AtmobFileApi(dio, baseUrl: Constants.baseUrl);
  }

  @singleton
  AtmobStreamApi provideAtmobStreamApi(@Named("streamDio") Dio streamDio) {
    return AtmobStreamApi(streamDio, baseUrl: Constants.baseUrl);
  }
}
