import 'dart:developer';

import 'package:easy_localization/easy_localization.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';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
import 'package:mc_common_app/models/shipping_models/shipping_status_model.dart';

import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/utils.dart';

abstract class ShippingRepo {
  Future<List<ShippingRequestModel>> getShippingRequestListByStatus({ShippingRequestStatusEnum? shippingStatusEnum, int? requestId});

  Future<GenericRespModel> updateShippingRequestStatus({required ShippingRequestStatusEnum shippingStatusEnum, required int shippingRequestId, String? comment});
}

class ShippingRepoImp extends ShippingRepo {
  ApiClient apiClient = injector.get<ApiClient>();
  AppState appState = injector.get<AppState>();

  @override
  Future<List<ShippingRequestModel>> getShippingRequestListByStatus({ShippingRequestStatusEnum? shippingStatusEnum, int? requestId}) async {
    String token = appState.getUser.data!.accessToken ?? "";
    Map queryParameters = {
      "requestID": "${requestId ?? 0}",
      "shippingStatus": "${shippingStatusEnum != null ? shippingStatusEnum.getIdFromShippingStatusEnum() : 0}",
    };

    GenericRespModel genericRespModel = await apiClient.postJsonForObject(
      (json) => GenericRespModel.fromJson(json),
      ApiConsts.shippingRequestStatusGet,
      queryParameters,
      token: token,
    );

    if (genericRespModel.messageStatus != 1 || genericRespModel.data == null) {
      Utils.showToast(genericRespModel.message ?? LocaleKeys.somethingWrong.tr());
      return [];
    }

    List<ShippingRequestModel> list = List.generate(genericRespModel.data.length, (index) => ShippingRequestModel.fromJson(genericRespModel.data[index]));

    return list;
  }

  @override
  Future<GenericRespModel> updateShippingRequestStatus({required ShippingRequestStatusEnum shippingStatusEnum, required int shippingRequestId, String? comment}) async {
    String token = appState.getUser.data!.accessToken ?? "";
    Map<String, String> queryParameters = {
      "id": "$shippingRequestId",
      "shippingStatus": "${shippingStatusEnum.getIdFromShippingStatusEnum()}",
      "comment": comment ?? "",
    };

    GenericRespModel genericRespModel = await apiClient.postJsonForObject(
      (json) => GenericRespModel.fromJson(json),
      ApiConsts.shippingRequestStatusUpdate,
      queryParameters,
      token: token,
    );

    if (genericRespModel.messageStatus != 1 || genericRespModel.data == null) {
      Utils.showToast(genericRespModel.message ?? LocaleKeys.somethingWrong.tr());
    }

    return genericRespModel;
  }
}
