import 'package:cargocalculaterapp/features/home/data/models/banner_response.dart';
import 'package:cargocalculaterapp/features/home/data/models/comment_request.dart';
import 'package:cargocalculaterapp/features/home/data/models/comment_response.dart';
import 'package:cargocalculaterapp/features/home/data/models/notification_response.dart';
import 'package:cargocalculaterapp/features/home/data/models/order_single_response.dart';
import 'package:cargocalculaterapp/features/home/data/models/orders_list_response.dart';
import 'package:cargocalculaterapp/features/home/data/models/read_notification_request.dart';
import 'package:cargocalculaterapp/features/home/data/models/read_notification_response.dart';
import 'package:dio/dio.dart';
import '../../../../../constants/constants.dart';
import '../../../../../core/error/exceptions.dart';
import '../../../../../core/local_source/local_source.dart';
import '../../../../../injector_container.dart';
import 'home_remote_data_source.dart';

class HomeRemoteDataSourceImpl extends HomeRemoteDataSource {
  final Dio dio;

  HomeRemoteDataSourceImpl(this.dio);

  @override
  Future<OrdersListResponse> ordersList(Map<String, dynamic> request) async {
    try {
      final Response response = await dio.get(
        Constants.baseUrl + Urls.orderList,
        options: DioConstants.options
          ..headers = {
            "Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
          },
        queryParameters: request,
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        return OrdersListResponse.fromJson(response.data);
      }
      throw ServerException.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException.fromJson(e.response?.data);
    } on FormatException {
      throw ServerException(message: Validations.someThingWentWrong);
    }
  }

  @override
  Future<OrderSingleResponse> orderSingle(String orderId) async {
    try {
      final Response response = await dio.get(
        "${Constants.baseUrl}${Urls.orderList}/$orderId",
        options: DioConstants.options
          ..headers = {
            "Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
          },
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        return OrderSingleResponse.fromJson(response.data);
      }
      throw ServerException.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException.fromJson(e.response?.data);
    } on FormatException {
      throw ServerException(message: Validations.someThingWentWrong);
    }
  }

  @override
  Future<CommentResponse> comment(CommentRequest request) async {
    try {
      final Response response = await dio.post(
        "${Constants.baseUrl}${Urls.comment}",
        options: DioConstants.options
          ..headers = {
            "Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
          },
        data: request,
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        return CommentResponse.fromJson(response.data);
      }
      throw ServerException.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException.fromJson(e.response?.data);
    } on FormatException {
      throw ServerException(message: Validations.someThingWentWrong);
    }
  }

  @override
  Future<NotificationResponse> notification(
    Map<String, dynamic> request,
  ) async {
    try {
      final Response response = await dio.get(
        "${Constants.baseUrl}${Urls.notification}",
        options: DioConstants.options
          ..headers = {
            "Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
          },
        queryParameters: request,
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        return NotificationResponse.fromJson(response.data);
      }
      throw ServerException.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException.fromJson(e.response?.data);
    } on FormatException {
      throw ServerException(message: Validations.someThingWentWrong);
    }
  }

  @override
  Future<ReadNotificationResponse> notificationRead(
    ReadNotificationRequest request,
  ) async {
    try {
      final Response response = await dio.put(
        "${Constants.baseUrl}${Urls.notificationRead}",
        options: DioConstants.options
          ..headers = {
            "Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
          },
        data: request,
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        return ReadNotificationResponse.fromJson(response.data);
      }
      throw ServerException.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException.fromJson(e.response?.data);
    } on FormatException {
      throw ServerException(message: Validations.someThingWentWrong);
    }
  }

  @override
  Future<List<BannerResponse>> bannersResponse() async {
    try {
      final Response response = await dio.get(
        "${Constants.baseUrl}${Urls.banners}",
        options: DioConstants.options
          ..headers = {
            "Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
          },
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        final List<BannerResponse> images = (response.data as List)
            .map((json) => BannerResponse.fromJson(json))
            .toList();
        return images;
      }
      throw ServerException.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException.fromJson(e.response?.data);
    } on FormatException {
      throw ServerException(message: Validations.someThingWentWrong);
    }
  }
}
