import 'dart:convert';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:hl_lieferservice/feature/authentication/exceptions.dart';
import 'package:hl_lieferservice/util.dart';
import 'package:http/http.dart';

import '../../../dto/basic_response.dart';
import '../../../dto/car_add_response.dart';
import '../../../dto/car_get_response.dart';
import '../../../model/car.dart';

class CarService {
  CarService();

  Future<Car> addCar(String plate, int teamId) async {
    try {
      debugPrint(jsonEncode({"team_id": teamId.toString(), "plate": plate}));

      var response = await post(
        urlBuilder("_web_addCar"),
        headers: getSessionOrThrow(),
        body: {"team_id": teamId.toString(), "plate": plate},
      );

      if (response.statusCode == HttpStatus.unauthorized) {
        throw UserUnauthorized();
      }

      Map<String, dynamic> responseJson = jsonDecode(response.body);
      CarAddResponseDTO responseDto = CarAddResponseDTO.fromJson(responseJson);

      if (responseDto.succeeded == true) {
        return Car(
          id: int.parse(responseDto.car.id),
          plate: responseDto.car.plate,
        );
      } else {
        throw responseDto.message;
      }
    } catch (e, st) {
      debugPrint("ERROR WHILE ADDING CAR");
      debugPrint(e.toString());
      debugPrint(st.toString());

      rethrow;
    }
  }

  Future<void> editCar(Car car) async {
    try {
      var response = await post(
        urlBuilder("_web_editCar"),
        headers: getSessionOrThrow(),
        body: {"id": car.id.toString(), "plate": car.plate},
      );

      if (response.statusCode == HttpStatus.unauthorized) {
        throw UserUnauthorized();
      }

      Map<String, dynamic> responseJson = jsonDecode(response.body);
      BasicResponseDTO responseDto = BasicResponseDTO.fromJson(responseJson);

      if (responseDto.succeeded == true) {
        return;
      } else {
        throw responseDto.message;
      }
    } catch (e, st) {
      debugPrint("ERROR WHILE EDITING CAR ${car.id}");
      debugPrint("$e");
      debugPrint(st.toString());

      rethrow;
    }
  }

  Future<void> removeCar(int carId, int teamId) async {
    try {
      var response = await post(
        urlBuilder("_web_removeCar"),
        headers: getSessionOrThrow(),
        body: {"team_id": teamId.toString(), "id": carId.toString()},
      );

      if (response.statusCode == HttpStatus.unauthorized) {
        throw UserUnauthorized();
      }

      Map<String, dynamic> responseJson = jsonDecode(response.body);
      BasicResponseDTO responseDto = BasicResponseDTO.fromJson(responseJson);

      if (responseDto.succeeded == true) {
        return;
      } else {
        throw responseDto.message;
      }
    } catch (e, st) {
      debugPrint("ERROR WHILE REMOVING CAR");
      debugPrint(e.toString());
      debugPrint(st.toString());

      rethrow;
    }
  }

  Future<List<Car>> getCars(int teamId) async {
    try {
      debugPrint(teamId.toString());

      var response = await post(
        urlBuilder("_web_getCars"),
        headers: getSessionOrThrow(),
        body: {"team_id": teamId.toString()},
      );

      if (response.statusCode == HttpStatus.unauthorized) {
        throw UserUnauthorized();
      }

      Map<String, dynamic> responseJson = jsonDecode(response.body);
      CarGetResponseDTO responseDto = CarGetResponseDTO.fromJson(responseJson);

      if (responseDto.succeeded == true) {
        return responseDto.cars!
            .map((carDto) => Car(id: int.parse(carDto.id), plate: carDto.plate))
            .toList();
      } else {
        throw responseDto.message;
      }
    } catch (e, st) {
      debugPrint("ERROR WHILE FETCHING CARS");
      debugPrint(e.toString());
      debugPrint(st.toString());

      rethrow;
    }
  }
}
