
import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyProduct.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/order_detail.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/order_model.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/models/Authentication/authenticated_user.dart';
import 'package:diplomaticquarterapp/services/authentication/auth_provider.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';


class OrderDetailsService extends BaseService{

  AppSharedPreferences sharedPref = AppSharedPreferences();
  AppGlobal appGlobal = new AppGlobal();

  AuthenticatedUser authUser = new AuthenticatedUser();
  AuthProvider authProvider = new AuthProvider();
  List<OrderDetailModel> _orderList = List();
  List<OrderDetailModel> get orderList => _orderList;


  Future getOrderDetails(OrderId) async {
    hasError = false;
    await baseAppClient.getPharmacy(GET_ORDER_DETAILS+OrderId,
        onSuccess: (dynamic response, int statusCode) {
          _orderList.clear();
          response['orders'].forEach((item) {
            _orderList.add(OrderDetailModel.fromJson(item));
            print(response);
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        });
  }

  Future makeReview(PharmacyProduct product, double rating, String reviewText) async {
    hasError = false;
    super.error = "";

    var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);


    Map<String, dynamic> body = Map();
    Map<String, dynamic> reviewBody = Map();
    reviewBody["created_on_utc"] = DateTime.now().toString();
    reviewBody["customer_id"] = customerId;
    reviewBody["helpful_no_total"] = 0;
    reviewBody["helpful_yes_total"] = 0;
    reviewBody["id"] = 0;
    reviewBody["is_approved"] = false;
    reviewBody["position"] = 0;
    reviewBody["product_id"] = product.id;
    reviewBody["rating"] = rating;
    reviewBody["reply_text"] = reviewText;
    reviewBody["store_id"] = 2;
    reviewBody["title"] = "";
    body['review'] = reviewBody;
    await baseAppClient.post("$PHARMACY_MAKE_REVIEW",
        onSuccess: (response, statusCode) async {
          /*
          "success": {
    "SuccessEndUserMsg": "Your request has been completed successfuly.",
    "SuccessEndUserMsgN": "تمت العملية بنجاح"
  }
           */
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: body);
  }
}