import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/models/pharmacy/brandModel.dart';
import 'package:diplomaticquarterapp/models/pharmacy/topBrandsModel.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';

class BrandsService extends BaseService {
  AppSharedPreferences sharedPref = AppSharedPreferences();
  bool isLogin = false;

  List<Brand> _brandsList = List();
  List<Brand> get brandsList => _brandsList;

  List<TopBrand> _topBrandsList = List();
  List<TopBrand> get topBrandsList => _topBrandsList;

  List<Brand> _searchList = List();
  List<Brand> get searchList => _searchList;

  clearSearchList() {
    _searchList.clear();
  }

  Future getBrands() async {
    hasError = false;
    await baseAppClient.getPharmacy(GET_BRANDS,
        onSuccess: (dynamic response, int statusCode) {
      _brandsList.clear();
      response['manufacturer'].forEach((item) {
        _brandsList.add(Brand.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    });
  }

  Future getTopBrands() async {
    hasError = false;
    await baseAppClient.getPharmacy(GET_TOP_BRANDS,
        onSuccess: (dynamic response, int statusCode) {
          _topBrandsList.clear();
      response['manufacturer'].forEach((item) {
        _topBrandsList.add(TopBrand.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    });
  }

  Future searchProducts({String productName}) async {
    RegExp exp = new RegExp(productName.toUpperCase());
    hasError = false;
    _searchList.clear();
    await baseAppClient.getPharmacy(
      GET_BRANDS,
      onSuccess: (dynamic response, int statusCode) {
        response['manufacturer'].forEach((item) {
          if(exp.hasMatch(item['name'])){
            _searchList.add(Brand.fromJson(item));
          }else{
          }
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
    );
  }

}
