import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/pharmacies/ShoppingCart.dart';
import 'package:hmg_patient_app/core/model/pharmacies/ShoppingCartResponse.dart';
import 'package:hmg_patient_app/core/viewModels/base_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/pharmacyModule/OrderPreviewViewModel.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/services/pharmacy_services/product_detail_service.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/navigation_service.dart';
import 'package:provider/provider.dart';

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

class ProductDetailViewModel extends BaseViewModel {
  ProductDetailService _productDetailService = locator<ProductDetailService>();

  List<ProductDetail> get productDetailService => _productDetailService.productDetailList;

  List<LocationModel> get productLocationService => _productDetailService.productLocationList;

  List<Wishlist> get wishListItems => _productDetailService.wishListProducts;

  List<SpecificationModel> get productSpecification => _productDetailService.productSpecification;

  bool hasError = false;

  num get stockQuantity => _productDetailService.stockQuantity!;

  String get stockAvailability => _productDetailService.stockAvailability!;

  String get stockAvailabilityn => _productDetailService.stockAvailabilityn!;

  bool get isStockAvailable => _productDetailService.isStockAvailable!;

  Future getProductReviewsData(productID) async {
    hasError = false;
    setState(ViewState.Busy);
    await _productDetailService.getProductReviews(productID);
    if (_productDetailService.hasError) {
      error = _productDetailService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getProductLocationData(String productSKU) async {
    hasError = false;
    setState(ViewState.BusyLocal);
    await _productDetailService.getProductAvailabiltyDetail(productSKU);
    if (_productDetailService.hasError) {
      error = _productDetailService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future notifyMe(customerId, itemID) async {
    hasError = false;
    setState(ViewState.BusyLocal);
    await _productDetailService.notifyMe(customerId, itemID);
    if (_productDetailService.hasError) {
      error = _productDetailService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future addToCartData(quantity, itemID, context) async {
    hasError = false;
    setState(ViewState.BusyLocal);
    var resp = await _productDetailService.addToCart(quantity, itemID, context);
    ShoppingCartResponse object = _handleGetShoppingCartResponse(resp);
    if (_productDetailService.hasError) {
      error = _productDetailService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
      Provider.of<OrderPreviewViewModel>(locator<NavigationService>().navigatorKey.currentContext!, listen: false).setShoppingCartResponse(object);
    }
  }

  ShoppingCartResponse _handleGetShoppingCartResponse(Map res) {
    ShoppingCartResponse cartResponse = ShoppingCartResponse();

    if (res == null) {
      error = "response is null";
      setState(ViewState.Error);
      return ShoppingCartResponse();
    }
    print(res);
    cartResponse.itemCount = res["item_count"];
    cartResponse.quantityCount = res["quantity_count"];
    cartResponse.subtotal = res["subtotal"];
    cartResponse.subtotalWithVat = res["subtotal_with_vat"];
    cartResponse.subtotalVatAmount = res["subtotal_vat_amount"];
    cartResponse.subtotalVatRate = res["subtotal_vat_rate"];
    cartResponse.shoppingCarts =[];
    res["shopping_carts"].forEach((item) {
      ShoppingCart shoppingCart = ShoppingCart.fromJson(item);
      cartResponse.shoppingCarts!.add(shoppingCart);
    });
    return cartResponse;
  }

  Future addToWishlistData(itemID, context) async {
    hasError = false;
    setState(ViewState.BusyLocal);
    GifLoaderDialogUtils.showMyDialog(locator<NavigationService>().navigatorKey.currentContext!);
    await _productDetailService.addToWishlist(itemID, context);
    GifLoaderDialogUtils.hideDialog(locator<NavigationService>().navigatorKey.currentContext!);

    if (_productDetailService.hasError) {
      error = _productDetailService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future checkWishlistData() async {
    hasError = false;
    setState(ViewState.Busy);
    await _productDetailService.getWishlistItems();
    if (_productDetailService.hasError) {
      error = _productDetailService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future deleteWishlistData(itemID, context) async {
    hasError = false;
    setState(ViewState.BusyLocal);
    GifLoaderDialogUtils.showMyDialog(locator<NavigationService>().navigatorKey.currentContext!);
    await _productDetailService.deleteItemFromWishlist(itemID, context);
    GifLoaderDialogUtils.hideDialog(locator<NavigationService>().navigatorKey.currentContext!);

    if (_productDetailService.hasError) {
      error = _productDetailService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future productSpecificationData(itemID) async {
    hasError = false;
    setState(ViewState.Busy);
    await _productDetailService.productSpecificationData(itemID);
    if (_productDetailService.hasError) {
      error = _productDetailService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  clearReview() {
    productDetailService.clear();
  }
}
