import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/Addresses.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/LacumAccountInformation.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyProduct.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/ShoppingCartResponse.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/order_detail.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/payment-checkout-data.dart';
import 'package:diplomaticquarterapp/core/service/parmacyModule/order-preview-service.dart';
import 'package:diplomaticquarterapp/services/pharmacy_services/cancelOrder_service.dart';
import 'package:diplomaticquarterapp/services/pharmacy_services/orderDetails_service.dart';
import 'package:diplomaticquarterapp/services/pharmacy_services/order_service.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/order_model.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/orders_model.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import '../../../locator.dart';
import '../base_view_model.dart';

class OrderModelViewModel extends BaseViewModel {
  OrderService _orderService = locator<OrderService>();
  List<Orders> get orders => _orderService.orderList;

  OrderDetailsService _orderDetailsService = locator<OrderDetailsService>();
  List<OrderDetailModel> get orderListModel => _orderDetailsService.orderList;

  CancelOrderService _cancelOrderService = locator<CancelOrderService>();
  List<OrderDetailModel> get cancelOrder => _cancelOrderService.cancelOrderList;


  OrderPreviewService _orderServices = locator<OrderPreviewService>();
  List<Addresses> get addresses => _orderServices.addresses;
  LacumAccountInformation get lacumInformation =>
      _orderServices.lacumInformation;
  ShoppingCartResponse cartResponse = ShoppingCartResponse();
  PaymentCheckoutData paymentCheckoutData = PaymentCheckoutData();
  double totalAdditionalShippingCharge = 0;



  Future getOrder(customerId, pageID) async {
    print("this is customer id" + customerId);
    setState(ViewState.Busy);
    await _orderService.getOrder(customerId, pageID);
    if (_orderService.hasError) {
      error = _orderService.error;
      setState(ViewState.Error);
    } else {
      //order = _orderService.orderList;
      setState(ViewState.Idle);
    }
  }

  Future<OrderDetailModel> getOrderDetails(OrderId) async {
    setState(ViewState.Busy);
    await _orderDetailsService.getOrderDetails(OrderId);
    if (_orderDetailsService.hasError) {
      error = _orderDetailsService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
    return _orderDetailsService.orderList.first;
  }

  Future getProductReview() async {
    setState(ViewState.Busy);
    // await _orderService.getProductReview();
    if (_orderService.hasError) {
      error = _orderService.error;
      setState(ViewState.Error);
    } else {
      //order = _orderService.orderList;
      setState(ViewState.Idle);
    }
  }

  Future getCanceledOrder(order, context) async {
    print("this is order id" + order);
    setState(ViewState.Busy);
    dynamic res;
    await _cancelOrderService.getCanceledOrder(order).then((value) {
      res = value['success']['SuccessEndUserMsg'];
      print(value['success']['SuccessEndUserMsg']);
      AppToast.showSuccessToast(message: "Request Sent Successfully");
//      Navigator.pop(context);
    });
    if (_cancelOrderService.hasError) {
      error = _cancelOrderService.error;
      setState(ViewState.Error);
      AppToast.showErrorToast(message: error);
    } else {
      setState(ViewState.Idle);
//            AppToast.showSuccessToast(message: "Request Sent Successfully");
//          Navigator.push(context,
//          MaterialPageRoute(builder: (context) => OrderPage()));

    }

    return res;
  }

  Future makeReview(PharmacyProduct product, double rating, String reviewText) async {
    setState(ViewState.Busy);
    await _orderDetailsService.makeReview(product, rating, reviewText);
    if (_orderDetailsService.hasError) {
      error = _orderDetailsService.error;
      setState(ViewState.Error);
      AppToast.showErrorToast(message: error);
    } else {
      setState(ViewState.Idle);
      AppToast.showSuccessToast(
          message: "Your review has been Submitted successfully");
    }
  }

  Future makeOrder() async {
   /* setState(ViewState.Busy);
    await _orderServices.makeOrder(paymentCheckoutData, cartResponse.shoppingCarts);
    if (_orderServices.hasError) {
      error = _orderServices.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }*/
  }

}
