
import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/core/model/pharmacies/PharmacyProduct.dart';
import 'package:hmg_patient_app/core/model/pharmacies/order_detail.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';
import 'package:hmg_patient_app/models/Authentication/authenticated_user.dart';
import 'package:hmg_patient_app/services/authentication/auth_provider.dart';
import 'package:hmg_patient_app/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<OrderDetailModel> get orderList => _orderList;


  Future getOrderDetails(OrderId, orderGUID) async {
    var customerGUID = await sharedPref.getObject(PHARMACY_CUSTOMER_GUID);
    hasError = false;
    await baseAppClient.getPharmacy(GET_ORDER_DETAILS+OrderId + "/$orderGUID",
        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.postPharmacy("$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);
  }
}