import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyProduct.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/brands_model.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/categorise_parent_model.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/final_products_model.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/parent_products_model.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/pharmacy_categorise.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/scan_qr_model.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/sub_categories_model.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/sub_products_model.dart';
import 'package:diplomaticquarterapp/core/model/search_products_model.dart';

import 'package:diplomaticquarterapp/core/service/pharmacy_categorise_service.dart';
import 'package:diplomaticquarterapp/locator.dart';

import 'base_view_model.dart';

class PharmacyCategoriseViewModel extends BaseViewModel {
  bool hasError = false;
  PharmacyCategoriseService _pharmacyCategoriseService =
      locator<PharmacyCategoriseService>();

  List<PharmacyCategorise> get categorise =>
      _pharmacyCategoriseService.categoriseList;

  List<CategoriseParentModel> get categoriseParent =>
      _pharmacyCategoriseService.parentCategoriseList;

  List<PharmacyProduct> get parentProducts =>
      _pharmacyCategoriseService.parentProductsList;

  List<SubCategoriesModel> get subCategorise =>
      _pharmacyCategoriseService.subCategoriseList;

  List<PharmacyProduct> get subProducts =>
      _pharmacyCategoriseService.subProductsList;

  List<PharmacyProduct> get finalProducts =>
      _pharmacyCategoriseService.finalProducts;
  List<BrandsModel> get brandsList => _pharmacyCategoriseService.brandsList;

  List<PharmacyProduct> get searchList => _pharmacyCategoriseService.searchList;

  List<ScanQrModel> get scanList => _pharmacyCategoriseService.scanList;

  Future getCategorise() async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getCategorise();
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getBrands() async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getBrands();
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future scanQr({String id}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.scanQr(id: id);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  clearSearchList() {
    _pharmacyCategoriseService.clearSearchList();
  }

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

  Future getCategoriseParent({String i}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getCategoriseParent(id: i);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.ErrorLocal);
    } else
      await getParentProducts(i: i);
    await getBrands();
  }

  Future getParentProducts({String i}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getParentProducts(id: i);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getSubCategorise({String i}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getSubCategorise(id: i);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.ErrorLocal);
    } else
      getSubProducts(i: i);
  }

  Future getSubProducts({String i}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getSubProducts(id: i);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getFinalProducts({String i}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getFinalProducts(id: i);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getManufacturerProducts(String id) async {
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getManufacturerProducts(id);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getLastVisitedProducts() async {
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getLastVisitedProducts();
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getBestSellerProducts() async {
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getBestSellerProducts();
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }
}
