import 'dart:convert';
import 'dart:developer';

import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/packages_offers/requests/OffersCategoriesRequestModel.dart';
import 'package:diplomaticquarterapp/core/model/packages_offers/requests/OffersProductsRequestModel.dart';
import 'package:diplomaticquarterapp/core/model/packages_offers/responses/OfferCategoriesResponseModel.dart';
import 'package:diplomaticquarterapp/core/model/packages_offers/responses/OfferProductsResponseModel.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/core/service/client/base_app_client.dart';

import '../../../locator.dart';

class OffersAndPackagesServices extends BaseService {
  List<OfferCategoriesResponseModel> categoryList = List();

  Future<List<OfferCategoriesResponseModel>> getAllCategories(OffersCategoriesRequestModel request) async {
    hasError = false;
    var url = EXA_CART_API_BASE_URL + PACKAGES_CATEGORIES;
    await baseAppClient.simpleGet(url, onSuccess: (dynamic stringResponse, int statusCode) {
      if (statusCode == 200) {
        var jsonResponse = json.decode(stringResponse);
        jsonResponse['categories'].forEach((json) {
          categoryList.add(OfferCategoriesResponseModel().fromJson(json));
        });
      }
    }, onFailure: (String error, int statusCode) {
      log(error);
    }, queryParams: request.toFlatMap());

    return categoryList;
  }

  List<OfferProductsResponseModel> productList = List();
  Future<List<OfferProductsResponseModel>> getAllProducts(OffersProductsRequestModel request) async {
    hasError = false;
    var url = EXA_CART_API_BASE_URL + PACKAGES_PRODUCTS;
    await baseAppClient.simpleGet(url, onSuccess: (dynamic stringResponse, int statusCode) {
      if (statusCode == 200) {
        var jsonResponse = json.decode(stringResponse);
        jsonResponse['products'].forEach((json) {
          productList.add(OfferProductsResponseModel().fromJson(json));
        });
      }
    }, onFailure: (String error, int statusCode) {
      log(error);
    }, queryParams: request.toFlatMap());

    return productList;
  }
}
