import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:wifi_iot/wifi_iot.dart';

class HmgConnectionProvider extends ChangeNotifier {
  bool isConnectedToHMG = false;

  Future<bool> checkHmgNetworkConnectivity() async {
    if (await WiFiForIoTPlugin.getSSID() == AppState().getMohemmWifiSSID) {
      isConnectedToHMG = true;
    } else {
      isConnectedToHMG = false;
    }
    AppState().isConnectedToHMG = isConnectedToHMG;
    return isConnectedToHMG;
  }

  void connectWithHmgNetwork() async {
    try {
      bool isConnected = await WiFiForIoTPlugin.connect("MOHEMM-CONNECT", password: "0987654321", joinOnce: Platform.isIOS ? false : true, security: NetworkSecurity.WPA, withInternet: false);

      if (isConnected) {
        await WiFiForIoTPlugin.forceWifiUsage(true);
        await Future.delayed(const Duration(seconds: 2));
        isConnectedToHMG=true;

      }
    } catch (e) {
      isConnectedToHMG = false;
      AppState().isConnectedToHMG = isConnectedToHMG;
      print("----------------o----");
      print(e);
    }
  }

  Future<bool> closeWifiRequest() async {
    if (Platform.isAndroid) {
      await WiFiForIoTPlugin.forceWifiUsage(false);
    }
    isConnectedToHMG = false;
    AppState().isConnectedToHMG = isConnectedToHMG;
    return await WiFiForIoTPlugin.disconnect();
  }
}
