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

class ProductDetailService extends BaseService {
  bool isLogin = false;

  num? _stockQuantity;

  num? get stockQuantity => _stockQuantity;

  String? _stockAvailability;
  String? _stockAvailabilityn;

  String? get stockAvailability => _stockAvailability;
  String? get stockAvailabilityn => _stockAvailabilityn;

  bool? _isStockAvailable;

  bool? get isStockAvailable => _isStockAvailable;

  List<ProductDetail> _productDetailList =[];

  List<ProductDetail> get productDetailList => _productDetailList;

  List<LocationModel> _productLocationList =[];

  List<LocationModel> get productLocationList => _productLocationList;

  List<Wishlist> _addToCartModel =[];

  List<Wishlist> get addToCartModel => _addToCartModel;

  List<Wishlist> _wishListProducts =[];

  List<Wishlist> get wishListProducts => _wishListProducts;

  List<SpecificationModel> _productSpecification =[];

  List<SpecificationModel> get productSpecification => _productSpecification;

  Future getProductReviews(productID) async {
    hasError = false;
    await baseAppClient.getPharmacy(
        GET_PRODUCT_DETAIL +
            productID +
            "?fields=reviews,stock_quantity,stock_availability,stock_availabilityn,IsStockAvailable",
        onSuccess: (dynamic response, int statusCode) {
      _productDetailList.clear();
      response['products'].forEach((item) {
        _productDetailList.add(ProductDetail.fromJson(item));
        print(response);
      });
      _stockQuantity = response['products'][0]['stock_quantity'];
      _stockAvailability = response['products'][0]['stock_availability'];
      _stockAvailabilityn = response['products'][0]['stock_availabilityn'];

      _isStockAvailable = response['products'][0]['IsStockAvailable'];
      // _isStockAvailable = _stockAvailability == "In stock" ? true : false;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    });
  }

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

    request = {
      // "Channel": 3,
      // "DeviceTypeID": 2,
      // "IPAdress": "10.20.10.20",
      // "LanguageID": 2,
      // "PatientOutSA": 0,
      "SKU": productSKU,
      // "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<Map> addToCart(quantity, itemID, context) 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
      }
    };
    dynamic localRes;

    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: TranslationBase.of(context).addToCartMsg
          // 'You have added a product to the cart'
          );
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
      AppToast.showErrorToast(
          message: error ?? Utils.generateContactAdminMessage());
    }, body: request);

    return Future.value(localRes);
  }

  Future notifyMe(customerId, itemID) async {
    hasError = false;
    await baseAppClient.getPharmacy(
        SUBSCRIBE_PRODUCT + "SinceId=$customerId&ProductId=$itemID",
        onSuccess: (dynamic response, int statusCode) {
      AppToast.showSuccessToast(
          message: TranslationBase.of(AppGlobal.context).notifyMeMsg
          // TranslationBase.of(context).notifyMeMsg
          //'You will be notified when product available'
          );
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
      AppToast.showErrorToast(
          message: error ?? Utils.generateContactAdminMessage());
    });
  }

  Future addToWishlist(itemID, context) 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: TranslationBase.of(context).addToWishlistMsg
          // 'You have added a product to the Wishlist'
          );
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
      AppToast.showErrorToast(
          message: error ?? Utils.generateContactAdminMessage());
    }, body: request);
  }

  Future getWishlistItems() async {
    var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);
    var custGUID = await sharedPref.getObject(PHARMACY_CUSTOMER_GUID);
    hasError = false;
    await baseAppClient.getPharmacy(
        GET_WISHLIST + customerId + "/$custGUID" + "?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 deleteItemFromWishlist(itemID, context) async {
    var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);
    var custGUID = await sharedPref.getObject(PHARMACY_CUSTOMER_GUID);
    hasError = false;
    await baseAppClient.getPharmacy(
        DELETE_WISHLIST +
            customerId +
            "/$custGUID" +
            "+&product_id=" +
            itemID +
            "&cart_type=Wishlist",
        onSuccess: (dynamic response, int statusCode) {
      _wishListProducts.clear();
      response['shopping_carts'].forEach((item) {
        _wishListProducts.add(Wishlist.fromJson(item));
      });
      AppToast.showSuccessToast(
          message: TranslationBase.of(context).removeFromWishlistMsg
          // 'You have removed a product from the Wishlist'
          );
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
      AppToast.showErrorToast(
          message: error ?? Utils.generateContactAdminMessage());
    });
  }

  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;
    });
  }
}
