import 'package:siro_driver/constant/currency.dart';
import 'dart:io';

import 'package:siro_driver/constant/links.dart';
import 'package:siro_driver/controller/functions/crud.dart';
import 'package:siro_driver/views/widgets/mydialoug.dart';
import 'package:flutter/material.dart';
import 'package:flutter_confetti/flutter_confetti.dart';
import 'package:flutter_overlay_window/flutter_overlay_window.dart';
import 'package:get/get.dart';
import 'package:location/location.dart';

import '../../constant/box_name.dart';
import '../../main.dart';
import '../auth/captin/login_captin_controller.dart';
import '../home/payment/captain_wallet_controller.dart';

Future<void> getPermissionOverlay() async {
  if (Platform.isAndroid) {
    final bool status = await FlutterOverlayWindow.isPermissionGranted();
    if (status == false) {
      MyDialog().getDialog(
        'Allow overlay permission'.tr,
        'To display orders instantly, please grant permission to draw over other apps.'
            .tr,
        () async {
          Get.back();
          await FlutterOverlayWindow.requestPermission();
        },
      );
    }
  }
}

Future<void> showDriverGiftClaim(BuildContext context) async {
  if (box.read(BoxName.is_claimed).toString() == '0' ||
      box.read(BoxName.is_claimed) == null) {
    MyDialog().getDialog(
        'You have gift 300 ${CurrencyHelper.currency}'.tr, 'This for new registration'.tr, () async {
      Get.back();
      var res = await CRUD().post(link: AppLink.updateDriverClaim, payload: {
        'driverId': box.read(BoxName.driverID),
      });
      if (res != 'failure') {
        Get.find<CaptainWalletController>()
            .addDriverWallet('new driver', '300', '300');
        Confetti.launch(
          context,
          options:
              const ConfettiOptions(particleCount: 100, spread: 70, y: 0.6),
        );
        box.write(BoxName.is_claimed, '1');
      }
    });
  }
}

Future<void> closeOverlayIfFound() async {
  if (Platform.isAndroid) {
    bool isOverlayActive = await FlutterOverlayWindow.isActive();
    if (isOverlayActive) {
      await FlutterOverlayWindow.closeOverlay();
    }
  }
}

final location = Location();
Future<void> getLocationPermission() async {
  bool serviceEnabled;
  PermissionStatus permissionGranted;

  // Check if location services are enabled
  serviceEnabled = await location.serviceEnabled();
  if (!serviceEnabled) {
    serviceEnabled = await location.requestService();
    if (!serviceEnabled) {
      // Location services are still not enabled, handle the error
      return;
    }
  }

  // Check if the app has permission to access location
  permissionGranted = await location.hasPermission();
  if (permissionGranted == PermissionStatus.denied) {
    permissionGranted = await location.requestPermission();
    if (permissionGranted != PermissionStatus.granted) {
      // Location permission is still not granted, handle the error
      permissionGranted = await location.requestPermission();
      return;
    }
  }
  if (permissionGranted.toString() == 'PermissionStatus.granted') {
    box.write(BoxName.locationPermission, 'true');
    Get.find<LoginDriverController>().update();
  }
  // update();
}

Future<void> getOverLay(String myListString) async {
  bool isOverlayActive = await FlutterOverlayWindow.isActive();
  if (isOverlayActive) {
    await FlutterOverlayWindow.closeOverlay();
  }
  await FlutterOverlayWindow.showOverlay(
    enableDrag: true,
    flag: OverlayFlag.focusPointer,
    visibility: NotificationVisibility.visibilityPublic,
    positionGravity: PositionGravity.auto,
    height: 700,
    width: WindowSize.matchParent,
    startPosition: const OverlayPosition(0, -150),
  );

  await FlutterOverlayWindow.shareData(myListString);
}
