import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/categorise_parent_model.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/parent_products_model.dart';
import 'package:diplomaticquarterapp/core/model/pharmacy/pharmacy_categorise.dart';

import 'base_service.dart';

class PharmacyCategoriseService extends BaseService {
  //service one
  List<PharmacyCategorise> _categoriseList = List();
  List<PharmacyCategorise> get categoriseList => _categoriseList;

  //service two
  List<CategoriseParentModel> _parentCategoriseList = List();
  List<CategoriseParentModel> get parentCategoriseList => _parentCategoriseList;

  //service three
  List<ParentProductsModel> _parentProductsList = List();
  List<ParentProductsModel> get parentProductsList => _parentProductsList;

  Future getCategorise() async {
    hasError = false;
    _categoriseList.clear();
    await baseAppClient.get(
      GET_PHARMACY_CATEGORISE,
      onSuccess: (dynamic response, int statusCode) {
        response['categories'].forEach((item) {
          _categoriseList.add(PharmacyCategorise.fromJson(item));
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
    );
  }

  Future getCategoriseParent({String id}) async {
    hasError = false;
    _parentCategoriseList.clear();
    String endPoint =
        id != null ? GET_CATEGORISE_PARENT + "$id" : GET_CATEGORISE_PARENT + "";
    await baseAppClient.get(
      endPoint,
      onSuccess: (dynamic response, int statusCode) {
        response['categories'].forEach((item) {
          _parentCategoriseList.add(CategoriseParentModel.fromJson(item));
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
    );
  }

  Future getParentProducts() async {
    hasError = false;
    _parentProductsList.clear();
    await baseAppClient.get(
      GET_PARENT_PRODUCTS,
      onSuccess: (dynamic response, int statusCode) {
        response['products'].forEach((item) {
          _parentProductsList.add(ParentProductsModel.fromJson(item));
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
    );
  }
}
