import 'package:flutter/material.dart';
import 'package:flutter_overlay_window_sdk34/flutter_overlay_window_sdk34.dart';

class BubbleService {
  static final BubbleService _instance = BubbleService._internal();
  factory BubbleService() => _instance;
  BubbleService._internal();

  final ValueNotifier<bool> isActive = ValueNotifier(false);

  Future<void> show() async {
    isActive.value = true;

    // ✅ Cek izin overlay
    final granted = await FlutterOverlayWindow.isPermissionGranted();
    if (granted != true) {
      final requestGranted = await FlutterOverlayWindow.requestPermission();
      if (requestGranted != true) return;
    }

    // ✅ Tampilkan overlay bubble
    await FlutterOverlayWindow.showOverlay(
      height: 100,
      width: 100,
      alignment: OverlayAlignment.topRight,
      enableDrag: true,
      positionGravity: PositionGravity.auto,
      overlayTitle: "Bubble Chat",
      overlayContent: "Bubble aktif",
      startPosition:const OverlayPosition(100, 100),
      // flag: OverlayFlag.focusPointer, // ✅ tambahkan ini
      );
  }

  // ✅ Fungsi untuk mengubah ukuran bubble
  Future<void> resize(int width, int height, {bool animated = true}) async {
    try {
      await FlutterOverlayWindow.resizeOverlay(width, height, animated);
    } catch (e) {
      debugPrint("Resize error: $e");
    }
  }

  

  // ✅ Sembunyikan overlay
  Future<void> hide() async {
    isActive.value = false;
    await FlutterOverlayWindow.closeOverlay();
  }



  // untuk method channel
}
