import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
import 'package:diplomaticquarterapp/models/pharmacy/Wishlist.dart';
import 'package:diplomaticquarterapp/models/pharmacy/locationModel.dart';
import 'package:diplomaticquarterapp/models/pharmacy/productDetailModel.dart';
import 'package:diplomaticquarterapp/services/pharmacy_services/product_detail_service.dart';
import 'package:diplomaticquarterapp/models/pharmacy/specification.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;


  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() async {
    hasError = false;
    setState(ViewState.Busy);
    await _productDetailService.getProductAvailabiltyDetail();
    if (_productDetailService.hasError) {
      error = _productDetailService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

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

  Future addToCartData(quantity, itemID) async {
    hasError = false;
    setState(ViewState.Busy);
    await _productDetailService.addToCart(quantity, itemID);
    if (_productDetailService.hasError) {
      error = _productDetailService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future addToWishlistData(itemID) async {
    hasError = false;
    setState(ViewState.Busy);
    await _productDetailService.addToWishlist(itemID);
    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 deletWishlistData(itemID) async {
    hasError = false;
    setState(ViewState.Busy);
    await _productDetailService.delteItemFromWishlist(itemID);
    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);
  }

}