import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';
import 'package:hmg_patient_app/models/pharmacy/Wishlist.dart';
import 'package:hmg_patient_app/uitl/app_shared_preferences.dart';

class WishListService extends BaseService {
  AppSharedPreferences sharedPref = AppSharedPreferences();
  bool isLogin = false;
  List<Wishlist> _wishListProducts =[];

  List<Wishlist> get wishListProducts => _wishListProducts;

//  Future<Map> getWishlist() async {
//    var isLogin = await sharedPref.getString(LOGIN_TOKEN_ID);
//    this.isLogin = isLogin != null;
////    if (!isLogin) {
////      // if not login
////    } else {
////      try {
////        await baseAppClient.get(
////            GET_WISHLIST +'/productsbyids/5308,3608,2316,963,5045,2714,1480,',//+ "272843" + "?shopping_cart_type=2",
////            onSuccess: (dynamic response, int statusCode) {
////              wishListProducts.clear();
////              response['shopping_carts'].forEach((item) {
////                wishListProducts.add(Product.fromJson(item));
////              });
////            }, onFailure: (String error, int statusCode) {
////          hasError = true;
////          super.error = error;
////        });
////      } catch (error) {
////        throw error;
////      }
////    }
//    hasError = false;
//    try {
//      await baseAppClient.getPharmacy(GET_WISHLIST+"1367368?shopping_cart_type=2",
//          onSuccess: (dynamic response, int statusCode) {
//            wishListProducts.clear();
//            response.forEach((item) {
//              wishListProducts.add(Wishlist.fromJson(response));
//            });
//          }, onFailure: (String error, int statusCode) {
//            hasError = true;
//            super.error = error;
//          });
//    } catch (error) {
//      throw error;
//    }
//  }

  Future getWishlist() async {
    //TODO we need to check why the customer id comes null
    String customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID) ?? "0";
    var customerGUID = await sharedPref.getObject(PHARMACY_CUSTOMER_GUID);
    hasError = false;
    await baseAppClient.getPharmacy(GET_WISHLIST + customerId + "/$customerGUID/" + "?shopping_cart_type=2", onSuccess: (dynamic response, int statusCode) {
      _wishListProducts.clear();
      response['shopping_carts'].forEach((item) {
        _wishListProducts.add(Wishlist.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    });
  }
}
