import { NativeModules, Platform } from 'react-native';

const LINKING_ERROR =
  `The package 'react-native-wifi-connect' doesn't seem to be linked. Make sure: \n\n` +
  Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
  '- You rebuilt the app after installing the package\n' +
  '- You are not using Expo Go\n';

const WifiConnect = NativeModules.WifiConnect
  ? NativeModules.WifiConnect
  : new Proxy(
      {},
      {
        get() {
          throw new Error(LINKING_ERROR);
        },
      }
    );

const LocalOnlyHotspot = NativeModules.LocalOnlyHotspot
  ? NativeModules.LocalOnlyHotspot
  : new Proxy(
      {},
      {
        get() {
          throw new Error(LINKING_ERROR);
        },
      }
    );

// Android Only
export function startHS() {
  if (Platform.OS === 'android') {
    return LocalOnlyHotspot.start();
  } else if (Platform.OS === 'ios') {
    return Promise.resolve('Not Built for iOS');
  }
}

// Android Only
export function stopHS() {
  if (Platform.OS === 'android') {
    return LocalOnlyHotspot.stop();
  } else if (Platform.OS === 'ios') {
    return Promise.resolve('Not Built for iOS');
  }
}

export function connect(ssid: String, pass: String) {
  if (Platform.OS === 'android') {
    return WifiConnect.connect(ssid, pass);
  } else if (Platform.OS === 'ios') {
    return Promise.resolve('Not Built for iOS');
  }
}

// Android Only
export function wifiSwitch() {
  if (Platform.OS === 'android') {
    WifiConnect.switch();
  } else if (Platform.OS === 'ios') {
    console.log('Not Built for iOS');
  }
}

// Android Only
export function getSSID() {
  if (Platform.OS === 'android') {
    return WifiConnect.getSSID();
  } else if (Platform.OS === 'ios') {
    return Promise.resolve('Not Built for iOS');
  }
}

// Android Only
export function isWifiOn() {
  if (Platform.OS === 'android') {
    return WifiConnect.isOn();
  } else if (Platform.OS === 'ios') {
    return Promise.resolve('Not Built for iOS');
  }
}
