import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy_module/Manufacturer.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy_module/PharmacyImageObject.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy_module/PharmacyProduct.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';

class PharmacyModuleService extends BaseService {
  bool isFinished = true;
  bool hasError = false;
  String errorMsg = '';

  List<PharmacyImageObject> bannerItems = List();
  List<Manufacturer> manufacturerList = List();
  List<PharmacyProduct> bestSellerProducts = List();

  Future getBannerListList() async {
    hasError = false;
    try {
      await baseAppClient.get(GET_PHARMACY_BANNER,
          onSuccess: (dynamic response, int statusCode) {
        bannerItems.clear();
        response['images'].forEach((item) {
          bannerItems.add(PharmacyImageObject.fromJson(item));
        });
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      });
    } catch (error) {
      throw error;
    }
  }

  Future getTopManufacturerList() async {
    Map<String, String> queryParams = {'page': '1', 'limit': '8'};
    try {
      await baseAppClient.get(GET_PHARMACY_TOP_MANUFACTURER,
          onSuccess: (dynamic response, int statusCode) {
        manufacturerList.clear();
        response['manufacturer'].forEach((item) {
          manufacturerList.add(Manufacturer.fromJson(item));
        });
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, queryParams: queryParams);
    } catch (error) {
      throw error;
    }
  }

  Future getBestSellerProducts() async {
    Map<String, String> queryParams = {
      'fields':
          'id,discount_ids,name,namen,localized_names,display_order,short_description,full_description,full_descriptionn,sku,order_minimum_quantity,order_maximum_quantity,price,old_price,images,is_rx,rx_message,rx_messagen,discount_name,discount_namen,approved_rating_sum,approved_total_reviews,allow_back_in_stock_subscriptions,stock_quantity,stock_availability,stock_availabilityn,discount_percentage,reviews',
    };
    try {
      await baseAppClient.get(GET_PHARMACY_BEST_SELLER_PRODUCT,
          onSuccess: (dynamic response, int statusCode) {
        bestSellerProducts.clear();
        response['products'].forEach((item) {
          bestSellerProducts.add(PharmacyProduct.fromJson(item));
        });
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, queryParams: queryParams);
    } catch (error) {
      throw error;
    }
  }
}
