import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/pharmacies/PharmacyProduct.dart';
import 'package:hmg_patient_app/core/model/pharmacy/categorise_parent_model.dart';
import 'package:hmg_patient_app/core/model/pharmacy/pharmacy_categorise.dart';
import 'package:hmg_patient_app/core/model/pharmacy/scan_qr_model.dart';
import 'package:hmg_patient_app/core/service/pharmacy_categorise_service.dart';
import 'package:hmg_patient_app/locator.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:flutter/cupertino.dart';

import 'base_view_model.dart';

class PharmacyCategoriseViewModel extends BaseViewModel {
  bool hasError = false;

  int firstSubsetIndex = 0;
  int inPatientPageSize = 20;
  int lastSubsetIndex = 20;

  List<PharmacyProduct> filteredInPatientItems = [];
  List<PharmacyProduct> filteredMyInPatientItems = [];

  PharmacyCategoriseService _pharmacyCategoriseService = locator<PharmacyCategoriseService>();

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

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

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

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

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

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

  List<CategoriseParentModel> 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({String? id}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getBrands(id: id!);
    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, int? pageIndex, bool? isLoading, BuildContext? context}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    // GifLoaderDialogUtils.showMyDialog(context);
    await _pharmacyCategoriseService.getCategoriseParent(id: i!);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error!;
      setState(ViewState.ErrorLocal);
    } else
      await getBrands(id: i);
    await getParentProducts(i: i, pageIndex: pageIndex, isLoading: isLoading, context: context);
    // GifLoaderDialogUtils.showMyDialog(context);
  }

  Future getParentProducts({String? i, int? pageIndex, bool? isLoading, BuildContext? context}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.BusyLocal);
    GifLoaderDialogUtils.showMyDialog(context!);
    await _pharmacyCategoriseService.getParentProducts(id: i, pageNumber: pageIndex, isLoading: isLoading!);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
    GifLoaderDialogUtils.hideDialog(context);
  }

  Future getSubCategorise({String? i, int? pageIndex, bool isLoading = false, BuildContext? context}) 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, pageIndex: pageIndex, isLoading: isLoading, context: context);
  }

  Future getSubProducts({String? i, int? pageIndex, bool isLoading = false, BuildContext? context}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.BusyLocal);
    GifLoaderDialogUtils.showMyDialog(context!);
    await _pharmacyCategoriseService.getSubProducts(id: i, pageIndex: pageIndex, isLoading: isLoading);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
    GifLoaderDialogUtils.hideDialog(context!);
  }

  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 getFilteredProducts({String? categoryId, String? brandId, String? min, String? max}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getFilteredProducts(categoryId: categoryId, brandId: brandId, max: max, min: min);
    if (_pharmacyCategoriseService.hasError) {
      error = _pharmacyCategoriseService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getFilteredSubProducts({String? categoryId, String? brandId, String? min, String? max}) async {
    hasError = false;
    // _insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _pharmacyCategoriseService.getFilteredSubProducts(categoryId: categoryId, brandId: brandId, max: max, min: min);
    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);
    }
  }

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

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

  addOnFilteredList() {
    if (lastSubsetIndex < parentProducts.length) {
      firstSubsetIndex = firstSubsetIndex + (parentProducts.length - lastSubsetIndex < inPatientPageSize - 1 ? parentProducts.length - lastSubsetIndex : inPatientPageSize - 1);
      lastSubsetIndex = lastSubsetIndex + (parentProducts.length - lastSubsetIndex < inPatientPageSize - 1 ? parentProducts.length - lastSubsetIndex : inPatientPageSize - 1);
      filteredInPatientItems.addAll(parentProducts.sublist(firstSubsetIndex, lastSubsetIndex));
      setState(ViewState.Idle);
    }
  }

  removeOnFilteredList() {
    if (lastSubsetIndex - inPatientPageSize - 1 > 0) {
      filteredInPatientItems.removeAt(lastSubsetIndex - inPatientPageSize - 1);
      setState(ViewState.Idle);
    }
  }
}
