import 'dart:convert';

import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/geofencing/requests/GeoZonesRequestModel.dart';
import 'package:diplomaticquarterapp/core/model/geofencing/requests/LogGeoZoneRequestModel.dart';
import 'package:diplomaticquarterapp/core/model/geofencing/responses/GeoZonesResponseModel.dart';
import 'package:diplomaticquarterapp/core/model/geofencing/responses/LogGeoZoneResponseModel.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:flutter/foundation.dart';

class GeofencingServices extends BaseService {
  List<GeoZonesResponseModel> geoZones = List();
  bool testZones = true;

  Future<List<GeoZonesResponseModel>> getAllGeoZones(GeoZonesRequestModel request) async {
    hasError = false;

    var _zonesJsonString;
    await baseAppClient.post(GET_GEO_ZONES, onSuccess: (dynamic response, int statusCode) {
      var zones = response['GeoF_PointsList'];
      zones.forEach((json) {
        geoZones.add(GeoZonesResponseModel().fromJson(json));
      });

      _zonesJsonString = json.encode(zones);
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      return Future.error(error);
    }, body: request.toFlatMap());

    AppSharedPreferences pref = AppSharedPreferences();
    await pref.setString(HMG_GEOFENCES, _zonesJsonString);
    return geoZones;
  }

  LogGeoZoneResponseModel logResponse;

  Future<LogGeoZoneResponseModel> logGeoZone(LogGeoZoneRequestModel request) async {
    hasError = false;
    await baseAppClient.post(LOG_GEO_ZONES, onSuccess: (dynamic response, int statusCode) {
      logResponse = LogGeoZoneResponseModel().fromJson(response);
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      return Future.error(error);
    }, body: request.toFlatMap());
    return logResponse;
  }
}
