import 'dart:developer';
import 'dart:io';

import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/service/client/base_app_client.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/uitl/PlatformBridge.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/alert_dialog.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/confirm_dialog.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wifi/wifi.dart';

import 'gif_loader_dialog_utils.dart';

class HMGNetworkConnectivity {
  final BuildContext context;
  final Function callBack;

  final String SSID = "HMG-MobileApp";

  HMGNetworkConnectivity(this.context, this.callBack);

  void checkAndConnectIfNoInternet() async {
    String pingUrl = "$BASE_URL$PING_SERVICE";
    // pingUrl = "https://captive.apple.com";

    String ssid = await Wifi.ssid.catchError((error) {
      log(error.toString());
    });

    bool alreadyConnected = ssid == SSID;

    BaseAppClient().simpleGet(pingUrl, onSuccess: (dynamic response, int statusCode) {
      log("Having internet with status code: $statusCode");
    }, onFailure: (String error, int statusCode) {
      if (alreadyConnected)
        showFailDailog(TranslationBase.of(context).failedToAccessHmgServices);
      else {
        confirmFromUser();
      }
    });
  }

  void confirmFromUser() {
    TranslationBase translator = TranslationBase.of(context);

    void doIt() {
      ConfirmDialog(
          context: context,
          confirmMessage: translator.wantToConnectWithHmgNetwork,
          okText: translator.yes,
          okFunction: () {
            ConfirmDialog.closeAlertDialog(context);
            callBack();
          },
          cancelText: translator.no,
          cancelFunction: () {
            ConfirmDialog.closeAlertDialog(context);
          }).showAlertDialog(context);
    }

    if (Platform.isAndroid)
      Wifi.list(SSID).then((value) {
        if (!value.indexWhere((element) => element.ssid == SSID).isNegative) doIt();
      });
    else
      doIt();
  }

  void showFailDailog(String message) {
    TranslationBase translator = TranslationBase.of(context);
    AlertDialogBox(
        context: context,
        okText: translator.ok,
        confirmMessage: message,
        okFunction: () {
          AlertDialogBox.closeAlertDialog(context);
        }).showAlertDialog(context);
  }

  // void next() {
  //   if (Platform.isIOS) {
  //     confirmFromUser_iOS();
  //   } else if (Platform.isAndroid) {
  //     confirmFromUser();
  //   }
  // }

  // void confirmFromUser_iOS() {
  //   String message = "You don't have internet access, Do you want to connect with HMG Network to use App.\n\nMake sure you are in HMG Network range";
  //   ConfirmDialog(
  //       context: context,
  //       confirmMessage: message,
  //       okText: "Yes",
  //       okFunction: () {
  //         ConfirmDialog.closeAlertDialog(context);
  //         callBack();
  //       },
  //       cancelText: "No",
  //       cancelFunction: () {
  //         ConfirmDialog.closeAlertDialog(context);
  //       }).showAlertDialog(context);
  // }

  // void connect_Android() {
  //   // enable the Wifi if its off
  //   PlatformBridge()
  //       .enableWifiIfNot()
  //       .then((value) => {
  //             if (value == true)
  //               {
  //                 callBack()
  //                 // Wifi.list(SSID)
  //                 //     .then((value1) => {
  //                 //           if (value1.length > 0) {callBack()} else {log("No HMG network in range")}
  //                 //         })
  //                 //     .catchError((e) {
  //                 //   log(e);
  //                 // })
  //               }
  //           })
  //       .catchError((error) {
  //     showFailDailog(error);
  //   });
  // }
}
