import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/Manufacturer.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyImageObject.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyProduct.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/Prescriptions.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/recommendedProduct_model.dart';
import 'package:diplomaticquarterapp/core/service/medical/prescriptions_service.dart';
//import 'package:diplomaticquarterapp/core/model/prescriptions/perscription_pharmacy.dart';
import 'package:diplomaticquarterapp/core/service/parmacyModule/parmacy_module_service.dart';
import 'package:diplomaticquarterapp/core/service/parmacyModule/prescription_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
import 'package:diplomaticquarterapp/services/pharmacy_services/recommendedProduct_service.dart';


import '../../../locator.dart';

class PharmacyModuleViewModel extends BaseViewModel {
  PharmacyModuleService _pharmacyService = locator<PharmacyModuleService>();

  PrescriptionService _prescriptionService = locator<PrescriptionService>();


  RecommendedProductService _recommendedProductService = locator<RecommendedProductService>();

  List<PharmacyImageObject> get bannerList => _pharmacyService.bannerItems;

  List<Manufacturer> get manufacturerList => _pharmacyService.manufacturerList;

  List<PharmacyProduct> get bestSellerProduct =>
      _pharmacyService.bestSellerProducts;

  List<PharmacyProduct> get lastVisitedProducts =>
      _pharmacyService.lastVisitedProducts;


  List<PharmacyProduct> get recommendedProductList =>
      _recommendedProductService.recommendedList;

  List<Prescriptions> get prescriptionsList =>
      _prescriptionService.prescriptionsList;

  bool hasError = false;
//  List<PharmacyProduct> get pharmacyPrescriptionsList => PharmacyProduct.pharmacyPrescriptionsList ;

  Future getPharmacyHomeData() async {
    if(authenticatedUserObject.isLogin)
      await generatePharmacyToken();

    var data = await sharedPref.getObject(USER_PROFILE);
    var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);

    setState(ViewState.Busy);
    if (authenticatedUserObject.isLogin && data != null && customerId == null) {
      await _pharmacyService.makeVerifyCustomer(data);
      if (_pharmacyService.hasError) {
        error = _pharmacyService.error;
        setState(ViewState.Error);
      } else {
        await getBannerList();
      }
    } else {
      await getBannerList();
    }
  }

  Future createUser() async {
    setState(ViewState.Busy);
    await _pharmacyService.createUser();
    if (_pharmacyService.hasError) {
      error = _pharmacyService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }
  Future generatePharmacyToken() async {
    setState(ViewState.Busy);
    await _pharmacyService.generatePharmacyToken();
    if (_pharmacyService.hasError) {
      error = _pharmacyService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getBannerList() async {
    setState(ViewState.Busy);
    await _pharmacyService.getBannerListList();
    if (_pharmacyService.hasError) {
      error = _pharmacyService.error;
      setState(ViewState.Error);
    } else {
      _getTopManufacturerList();
    }
  }

  List<String> getBannerImagesUrl() {
    List<String> images = List();
    images.add('assets/images/pharmacy_module/pharmacy_logo_green.png');
    _pharmacyService.bannerItems.forEach((element) {
      images.add(element.src);
    });

    return images;
  }

  _getTopManufacturerList() async {
    await _pharmacyService.getTopManufacturerList();
    if (_pharmacyService.hasError) {
      error = _pharmacyService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
      _getBestSellerProducts();
    }
  }

  _getBestSellerProducts() async {
    await _pharmacyService.getBestSellerProducts();
    if (_pharmacyService.hasError) {
      error = _pharmacyService.error;
      setState(ViewState.Error);
    } else {
      _getLastVisitedProducts();
    }
  }

  _getLastVisitedProducts() async {
    await _pharmacyService.getLastVisitedProducts();
    if (_pharmacyService.hasError) {
      error = _pharmacyService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  /////////////RecommendedProducts
   getRecommendedProducts(productId) async {
     hasError = false;
     setState(ViewState.Busy);
    await _recommendedProductService.getRecommendedProducts(productId);
    if (_recommendedProductService.hasError) {
      error = _recommendedProductService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future<bool> checkUserIsActivated() async {
    if (authenticatedUserObject.isLogin) {
      var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);
      if (customerId != null) {
        return true;
      } else {
        return false;
      }
    } else {
      // go to login page
      return false;
    }
  }

  getPrescription() async {
    print("Print PRESCRIPTION url");
    setState(ViewState.Busy);
    await _prescriptionService.getPrescription();
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }


  ///////////////////////or
//  getPrescriptions() async {
//    setState(ViewState.Busy);
//    await _prescriptionsService.getPrescriptions();
//    if (_prescriptionsService.hasError) {
//      error = _prescriptionsService.error;
//      setState(ViewState.Error);
//    } else {
//      _filterList();
//      await _getPrescriptionsOrders();
//      setState(ViewState.Idle);
//    }
//  }

}
