import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyImageObject.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/recommendedProduct_model.dart';

//import 'package:diplomaticquarterapp/core/model/prescriptions/perscription_pharmacy.dart';
import 'package:diplomaticquarterapp/core/service/parmacyModule/parmacy_module_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/services/pharmacy_services/recommendedProduct_service.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/navigation_service.dart';

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

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

  RecommendedProductService _recommendedProductService = locator<RecommendedProductService>();

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

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

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

  Future verifyCustomer() async {
    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.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

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

  Future getBannerList() async {
    if (bannerList.isEmpty) {
      GifLoaderDialogUtils.showMyDialog(locator<NavigationService>().navigatorKey.currentContext);
      await _pharmacyService.getBannerListList();
      GifLoaderDialogUtils.hideDialog(locator<NavigationService>().navigatorKey.currentContext);
      if (_pharmacyService.hasError) {
        error = _pharmacyService.error;
        setState(ViewState.Error);
      } else
        setState(ViewState.Idle);
    }
  }

  List<String> getBannerImagesUrl(ProjectViewModel projectViewModel) {
    List<String> images = List();
    images.add('assets/images/pharmacy_module/pharmacy_logo_green.png');

    if (projectViewModel.isArabic)
      images.add('assets/images/pharmacy_module/offers_banner_ar.png');
    else
      images.add('assets/images/pharmacy_module/offers_banner.png');

    _pharmacyService.bannerItems.forEach((element) {
      images.add(element.src);
    });

    return images;
  }

  getRecommendedProducts(productId) async {
    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;
    }
  }
}
