import 'package:mc_common_app/models/appointments_models/schedule_model.dart';
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
import 'package:mc_common_app/models/provider_branches_models/profile/services.dart';
import 'package:mc_common_app/api/api_client.dart';
import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/config/dependency_injection.dart';

abstract class ScheduleRepo {
  Future<Services> getAllServices(String branchId);

  Future<GenericRespModel> createSchedule(Map map);

  Future<GenericRespModel> checkServiceGroupInBranchSchedule(Map map);

  Future<GenericRespModel> addServicesInSchedule(Map map);

  Future<GenericRespModel> updateSchedule(Map map);

  Future<Schedule> getSchedules(String branchId);

  Future<GenericRespModel> updateServicesInSchedule(Map map);

  Future<GenericRespModel> setScheduleInActive(int scheduleId, int branchId);
}

class ScheduleRepoImp implements ScheduleRepo {
  @override
  Future<Services> getAllServices(String branchId) async {
    Map<String, dynamic> map = {"ProviderBranchID": branchId};
    String t = AppState().getUser.data!.accessToken ?? "";
    return await injector.get<ApiClient>().getJsonForObject((json) => Services.fromJson(json), ApiConsts.getServicesOfBranch, token: t, queryParameters: map);
  }

  @override
  Future<GenericRespModel> createSchedule(Map map) async {
    String t = AppState().getUser.data!.accessToken ?? "";
    return await injector.get<ApiClient>().postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.createSchedule, map, token: t);
  }

  @override
  Future<GenericRespModel> checkServiceGroupInBranchSchedule(Map map) async {
    String t = AppState().getUser.data!.accessToken ?? "";
    return await injector.get<ApiClient>().postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.checkGroupServiceInBranchSchedule, map, token: t);
  }

  @override
  Future<GenericRespModel> addServicesInSchedule(Map map) async {
    String t = AppState().getUser.data!.accessToken ?? "";
    return await injector.get<ApiClient>().postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.createGroup, map, token: t);
  }

  @override
  Future<Schedule> getSchedules(String branchId) async {
    Map<String, dynamic> map = {"ServiceProviderBranchID": branchId};
    String t = AppState().getUser.data!.accessToken ?? "";
    return await injector.get<ApiClient>().getJsonForObject((json) => Schedule.fromJson(json), ApiConsts.getSchedule, token: t, queryParameters: map);
  }

  @override
  Future<GenericRespModel> setScheduleInActive(int scheduleId, int branchId) async {
    Map<String, dynamic> map = {
      "id": scheduleId,
      "serviceProviderBranchID": branchId,
      "isActive": false,
    };
    String t = AppState().getUser.data!.accessToken ?? "";
    return injector.get<ApiClient>().postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.setScheduleInactive, token: t, map);
  }

  @override
  Future<GenericRespModel> updateSchedule(Map map) async {
    String t = AppState().getUser.data!.accessToken ?? "";
    return await injector.get<ApiClient>().postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.updateSchedule, map, token: t);
  }

  @override
  Future<GenericRespModel> updateServicesInSchedule(Map map) async {
    String t = AppState().getUser.data!.accessToken ?? "";
    return await injector.get<ApiClient>().postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.updateGroup, map, token: t);
  }
}
