import 'dart:convert';
import 'dart:developer';

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/cupertino.dart';
import 'package:flutter/foundation.dart';

import '../../../locator.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));
      });

      if (kDebugMode || testZones) addTestingGeoZones(zones);

      _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);
    debugPrint("Finished Fetching GEO ZONES from HMG service...");
    debugPrint("GEO ZONES saved to AppPreferences with key '$HMG_GEOFENCES'");

    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;
  }

  addTestingGeoZones(List zones) {
    // zones.add({"GEOF_ID": 12, "Description": "ZiK Home", "Latitude": "24.691136", "Longitude": "46.650116", "Radius": 100, "Type": 1});
    // zones.add({"GEOF_ID": 13, "Description": "CS Office", "Latitude": "24.7087913", "Longitude": "46.6656461", "Radius": 100, "Type": 1});
    // zones.add({"GEOF_ID": 14, "Description": "Mahmoud Shrouf Home", "Latitude": "24.777577", "Longitude": "46.652675", "Radius": 100, "Type": 1});
    // zones.add({"GEOF_ID": 14, "Description": "Panorama Mall", "Latitude": "24.692453", "Longitude": "46.669168", "Radius": 450, "Type": 1});
    // zones.add({"GEOF_ID": 16, "Description": "Saudi Architects Crossing", "Latitude": "24.698375", "Longitude": "46.668567", "Radius": 140, "Type": 1});
  }
}
