import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/uitl/app_shared_preferences.dart';
import 'package:hmg_patient_app/uitl/penguin_method_channel.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';

import '../pages/BookAppointment/Search.dart';
import 'app-permissions.dart';

enum DeepLinkNavigation {
  APPOINTMENT("/appointment", false),
  LIVE_CARE("/livecare", true),
  NAVIGATE("/navigate", false);

  final String name;
  final bool isLoginRequired;

  const DeepLinkNavigation(this.name, this.isLoginRequired);
}

class DeepLinkHandler {
  final projectID = "projectID";
  final clinicId = "clinicId";

  DeepLinkNavigation? navigationType;
  Uri? uri;

  DeepLinkHandler._();

  static final DeepLinkHandler _instance = DeepLinkHandler._();

  static DeepLinkHandler get instance => _instance;

  void handleDeepLinkAndNavigation(
      NavigatorState? currentState, Uri uri, BuildContext context) async {
    var url = uri.toString().replaceFirst(
        "https://hmg.com/Pages/PageNotFoundError.aspx?requestUrl=", "");

    uri = Uri.parse(url);
    this.uri = uri;
    print(
        "the uri contains ${uri.path.toLowerCase() == DeepLinkNavigation.NAVIGATE.name}");
    print("the uri.path ${uri.path}");
    if (uri.path.toLowerCase() == DeepLinkNavigation.NAVIGATE.name) {
      await Future.delayed(Duration(seconds: 1));
      navigationType = DeepLinkNavigation.NAVIGATE;
      var queryParam = uri.queryParameters;
      print("the query params are $queryParam");
      initPenguinSDK(
          queryParam[projectID] ?? "", queryParam[clinicId] ?? "", context);
      removeDeepLinkData();
      return;
    }
    if (uri.path.toLowerCase() == DeepLinkNavigation.APPOINTMENT.name) {
      navigationType = DeepLinkNavigation.NAVIGATE;
      currentState?.push(FadePage(page: Search()));
      removeDeepLinkData();
      return;
    }
    if (uri.path.toLowerCase() == DeepLinkNavigation.LIVE_CARE.name) {
      var viewModel = Provider.of(context);
      if (viewModel.isLogin) {
        currentState?.push(FadePage(page: Search()));
        removeDeepLinkData();
        return;
      } else {}
    }
  }

  void removeDeepLinkData() {
    navigationType = null;
    uri = null;
  }

  initPenguinSDK(
      String projectId, String clinicID, BuildContext context) async {
    AppSharedPreferences sharedPref = AppSharedPreferences();
    NavigationClinicDetails data = NavigationClinicDetails();
    data.clinicId = clinicID;

    data.projectId = projectId;
    final bool permited = await AppPermission.askPenguinPermissions();
    if (!permited) {
      Map<Permission, PermissionStatus> statuses = await [
        Permission.location,
        Permission.bluetooth,
        Permission.bluetoothConnect,
        Permission.bluetoothScan,
        Permission.activityRecognition,
      ].request().whenComplete(() async {
        final currentLanguage =
            await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
        PenguinMethodChannel()
            .launch("penguin", currentLanguage, "null", details: data);
      });
    }
  }
}
