import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/viewModels/base_view_model.dart';
import 'package:hmg_patient_app/models/pharmacy/brandModel.dart';
import 'package:hmg_patient_app/models/pharmacy/topBrandsModel.dart';
import 'package:hmg_patient_app/services/pharmacy_services/brands_service.dart';


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

class  BrandsViewModel extends BaseViewModel{
  BrandsService _brandsService = locator<BrandsService>();
  List<Brand> get brandsListList => _brandsService.brandsList;

  BrandsService _topBrandsService = locator<BrandsService>();
  List<TopBrand> get topBrandsListList => _topBrandsService.topBrandsList;

  List<Brand> get searchList =>
      _brandsService.searchList;

  bool hasError = false;


  Future getBrandsData() async {
    hasError = false;
    setState(ViewState.Busy);

    await _brandsService.getBrands();
    if (_brandsService.hasError) {
      error = _brandsService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getTopBrandsData() async {
    hasError = false;
    setState(ViewState.Busy);
    await _topBrandsService.getTopBrands();
    if (_topBrandsService.hasError) {
      error = _topBrandsService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }


  Future searchProducts({required String productName}) async {
    hasError = false;
    _brandsService.clearSearchList();
    setState(ViewState.Busy);
    await _brandsService.searchProducts(productName: productName);
    if (_brandsService.hasError) {
      error = _brandsService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }
}