import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/models/pharmacy/Wishlist.dart';
import 'package:diplomaticquarterapp/models/pharmacy/addToCartModel.dart';
import 'package:diplomaticquarterapp/models/pharmacy/locationModel.dart';
import 'package:diplomaticquarterapp/models/pharmacy/productDetailModel.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/models/pharmacy/specification.dart';

class ProductDetailService extends BaseService {
  bool isLogin = false;

  List<ProductDetail> _productDetailList = List();
  List<ProductDetail> get productDetailList => _productDetailList;

  List<LocationModel> _productLocationList = List();
  List<LocationModel> get productLocationList => _productLocationList;

  List<Wishlist> _addToCartModel = List();
  List<Wishlist> get addToCartModel => _addToCartModel;

  List<Wishlist> _wishListProducts = List();
  List<Wishlist> get wishListProducts => _wishListProducts;

  List<SpecificationModel> _productSpecification = List();
  List<SpecificationModel> get productSpecification => _productSpecification;



  Future getProductReviews(productID) async {
    hasError = false;
    await baseAppClient.getPharmacy(GET_PRODUCT_DETAIL+productID+"?fields=reviews",
        onSuccess: (dynamic response, int statusCode) {
          _productDetailList.clear();
          response['products'].forEach((item) {
            _productDetailList.add(ProductDetail.fromJson(item));
            print(response);
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        });
  }

  Future getProductAvailabiltyDetail() async {
    hasError = false;
    Map<String, dynamic> request;

    request = {
      "Channel": 3,
      "DeviceTypeID": 2,
      "IPAdress": "10.20.10.20",
      "LanguageID": 2,
      "PatientOutSA": 0,
      "SKU": "6720020025",
      "SessionID": null,
      "VersionID": 5.6,
      "generalid": "Cs2020@2016\$2958",
      "isDentalAllowedBackend": false
    };
    await baseAppClient.post(GET_LOCATION,
        onSuccess: (dynamic response, int statusCode) {
          _productLocationList.clear();
          response['PharmList'].forEach((item) {
            _productLocationList.add(LocationModel.fromJson(item));
            print(_productLocationList);
            print(response);
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: request);
  }

  Future addToCart(quantity, itemID) async {
    var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);
    hasError = false;
    Map<String, dynamic> request;

    request = {
      "shopping_cart_item":
      {
        "quantity": quantity,
        "shopping_cart_type": "1",
        "product_id": itemID,
        "customer_id": customerId,
        "language_id": 1
      }
    };
    await baseAppClient.pharmacyPost(GET_SHOPPING_CART, isExternal: false,
        onSuccess: (dynamic response, int statusCode) {
          _addToCartModel.clear();
          response['shopping_carts'].forEach((item) {
            _addToCartModel.add(Wishlist.fromJson(item));
          });
          AppToast.showSuccessToast(message: 'You have added a product to the cart');
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
          AppToast.showErrorToast(message: 'something went wrong please try again');
        }, body: request);
  }

  Future notifyMe(customerId, itemID) async {
    hasError = false;
    await baseAppClient.getPharmacy(SUBSCRIBE_PRODUCT + "SinceId=$customerId&ProductId=$itemID", onSuccess: (dynamic response, int statusCode) {
      AppToast.showSuccessToast(message: 'You will be notified when product available');
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
      AppToast.showErrorToast(message: 'something went wrong please try again');
    });
  }

  Future addToWishlist(itemID) async {
    var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);
    hasError = false;
    Map<String, dynamic> request;

    request = {
      "shopping_cart_item": {"quantity": 1, "shopping_cart_type": "Wishlist", "product_id": itemID, "customer_id": customerId, "language_id": 1}
    };
    await baseAppClient.pharmacyPost(GET_SHOPPING_CART,
        onSuccess: (dynamic response, int statusCode) {
          _wishListProducts.clear();
          response['shopping_carts'].forEach((item) {
            _wishListProducts.add(Wishlist.fromJson(item));
            AppToast.showSuccessToast(message: 'You have added a product to the Wishlist');
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
          AppToast.showErrorToast(message: 'something went wrong please try again');
        }, body: request);
  }

  Future getWishlistItems() async {
    var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);
    hasError = false;
    await baseAppClient.getPharmacy(GET_WISHLIST+customerId+"?shopping_cart_type=2",
        onSuccess: (dynamic response, int statusCode) {
          _wishListProducts.clear();
          response['shopping_carts'].forEach((item) {
            _wishListProducts.add(Wishlist.fromJson(item));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        });
  }
  Future delteItemFromWishlist(itemID) async {
    var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);
    hasError = false;
    await baseAppClient.getPharmacy(DELETE_WISHLIST+customerId+"+&product_id="+itemID+"&cart_type=Wishlist",
        onSuccess: (dynamic response, int statusCode) {
          _wishListProducts.clear();
          response['shopping_carts'].forEach((item) {
            _wishListProducts.add(Wishlist.fromJson(item));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        });
  }

  Future productSpecificationData(itemID) async {
    hasError = false;

    await baseAppClient.getPharmacy(GET_SPECIFICATION+itemID,
        onSuccess: (dynamic response, int statusCode) {
          _productSpecification.clear();
          response['specification'].forEach((item) {
            _productSpecification.add(SpecificationModel.fromJson(item));
            print(_productSpecification);
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        });
  }

}
