import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/localized_values.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/service/client/base_app_client.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

class PlatformBridge {
  static const platform = const MethodChannel("HMG-Platform-Bridge");
  static PlatformBridge _shared;
  static BuildContext _context;

  // // Singleton
  // static final PlatformBridge _singleton = PlatformBridge._internal();
  // factory PlatformBridge() {
  //   return _singleton;
  // }
  // PlatformBridge._internal();

  static PlatformBridge shared() {
    if (_shared == null) {
      assert(true, "PlatformBridge is not initialized, (Initialized it by calling 'PlatformBridge.init(BuildContext))'.");
    }
    return _shared;
  }

  static void init(BuildContext context) {
    _context = context;
    _shared = PlatformBridge();

    Future<dynamic> incoming(MethodCall methodCall) async {
      switch (methodCall.method) {
        case 'localizedValue':
          String key = methodCall.arguments.toString();
          return localizedValue(key);

        case 'getGeoZones':
          return getGeoZones();

        case 'getLogGeofenceFullUrl':
          return getLogGeofenceFullUrl();

        case 'getDefaultHttpParameters':
          return getDefaultHttpParameters();

        default:
          throw MissingPluginException('notImplemented');
      }
    }

    platform.setMethodCallHandler(incoming);
  }

  //---------------------------------
  // Incoming below
  //---------------------------------
  static Future<String> localizedValue(String forKey) async {
    String currentLanguage = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    Object localized = platformLocalizedValues[forKey][currentLanguage];
    return (localized != null || (localized is String)) ? localized : forKey;
  }

  static Future<String> getGeoZones() async {
    var res = await sharedPref.getStringWithDefaultValue(HMG_GEOFENCES, "[]");
    return res;
  }

  static Future<String> getLogGeofenceFullUrl() async {
    var res = BASE_URL + LOG_GEO_ZONES;
    return res;
  }

  static Future getDefaultHttpParameters() async {
    return BaseAppClient.defaultHttpParameters();
  }

  //--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//
  //--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//

  //---------------------------------
  // Outgoing below
  //---------------------------------
  // Method Names
  static const hmg_internet_wifi_connect_method = "connectHMGInternetWifi";
  static const hmg_guest_wifi_connect_method = "connectHMGGuestWifi";
  static const is_hmg_network_available_method = "isHMGNetworkAvailable";
  static const enable_wifi_if_not = "enableWifiIfNot";
  static const show_loading_method = "loading";
  static const register_Hmg_Geofences = "registerHmgGeofences";
  static const un_register_Hmg_Geofences = "unRegisterHmgGeofences";
  static const IS_DRAW_OVER_APPS_PERMISSION_ALLOWED = "isDrawOverAppsPermissionAllowed";
  static const ASK_DRAW_OVER_APPS_PERMISSION = "askDrawOverAppsPermission";
  static const GET_INTENT = "getIntent";

  Future<Object> connectHMGInternetWifi(String ssid, username, password) {
    try {
      return platform.invokeMethod(hmg_internet_wifi_connect_method, [ssid, username,password]);
    } on PlatformException catch (e) {
      print(e);
      return Future.error(e);
    }
  }

  Future<Object> connectHMGGuestWifi(String ssid) {
    try {
      return platform.invokeMethod(hmg_guest_wifi_connect_method, ssid);
    } on PlatformException catch (e) {
      print(e);
      return Future.error(e);
    }
  }

  Future<Object> isHMGNetworkAvailable(String ssid) {
    try {
      return platform.invokeMethod(is_hmg_network_available_method, ssid);
    } on PlatformException catch (e) {
      print(e);
      return Future.error(e);
    }
  }

  Future<Object> enableWifiIfNot() async {
    try {
      return platform.invokeMethod(enable_wifi_if_not);
    } on PlatformException catch (e) {
      print(e);
      return Future.error(e);
    }
  }

  void showLoading(String message, bool show) async {
    try {
      var result = await platform.invokeMethod(show_loading_method, [message, show]);
    } on PlatformException catch (e) {
      print(e);
    }
  }

  // GEO Fences
  void registerHmgGeofences() async {
    var result = await platform.invokeMethod(register_Hmg_Geofences);
  }

  void unRegisterHmgGeofences() async {
    var result = await platform.invokeMethod(un_register_Hmg_Geofences);
  }

  Future<bool> isDrawOverAppsPermissionAllowed() async {
    var result = await platform.invokeMethod(IS_DRAW_OVER_APPS_PERMISSION_ALLOWED);
    return result as bool;
  }

  Future<bool> askDrawOverAppsPermission() async {
    var result = await platform.invokeMethod(ASK_DRAW_OVER_APPS_PERMISSION);
    return result as bool;
  }

  Future<bool> getIntentData() async {
    var result = await platform.invokeMethod(GET_INTENT);
    return result as bool;
  }
}
