import 'dart:io';

import 'package:guru_utils/log/log.dart';
import 'package:guru_utils/vibration/vibrate_model.dart';
import 'package:vibration/vibration.dart';

/// Created by Haoyi on 2023/1/25

class VibrateUtils {
  static bool _hasVibrationCapabilities = false;
  static bool _hasCustomVibrationsSupport = false;
  static bool _hasAmplitudeControl = false;

  static bool _canVibrate = true;

  static Future initialize(int state) async {
    _hasVibrationCapabilities = await Vibration.hasVibrator() ?? false;
    _hasCustomVibrationsSupport = await Vibration.hasCustomVibrationsSupport() ?? false;
    _hasAmplitudeControl = await Vibration.hasAmplitudeControl() ?? false;
    Log.w(
        "VibrateUtils ensureInitialized! $_hasVibrationCapabilities $_hasCustomVibrationsSupport $_hasAmplitudeControl");
    _canVibrate = state == VibrationState.on;
  }

  static void enableVibrate() {
    _canVibrate = true;
  }

  static void disableVibrate() {
    _canVibrate = false;
  }

  static void vibrate(VibrateEffect effect) async {
    if (_canVibrate && _hasVibrationCapabilities) {
      if (_hasCustomVibrationsSupport) {
        if (_hasAmplitudeControl) {
          Vibration.vibrate(
              pattern: effect.pattern,
              intensities: effect.intensities,
              amplitude: effect.amplitude,
              duration: effect.amplitudeDuration);
        } else {
          Vibration.vibrate(
              pattern: effect.pattern, intensities: effect.intensities, duration: effect.duration);
        }
      } else {
        Vibration.vibrate(duration: effect.duration);
      }
    }
  }
}
