import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy_module/Manufacturer.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy_module/PharmacyImageObject.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy_module/PharmacyProduct.dart';
import 'package:diplomaticquarterapp/core/service/parmacyModule/parmacy_module_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';

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

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

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

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

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

  Future getPharmacyHomeData() 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 {
      _getBestSellerProducts();
    }
  }

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