import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:keyboard/data/bean/keyboard_info.dart';
import 'package:keyboard/plugins/plugin_constant.dart';
import 'package:keyboard/utils/atmob_log.dart';

import '../utils/default_keyboard_helper.dart';
import '../utils/floating_window_helper.dart';
import 'keyboard_android_service.dart';
import 'keyboard_method_handler.dart';

class KeyboardAndroidPlatform {
  KeyboardAndroidPlatform._();

  static const MethodChannel _channel = MethodChannel(
    PluginConstant.flutterMethodChannelName,
  );
  static const String _tag = 'KeyboardAndroidPlatform';

  static final KeyboardAndroidService _keyboardAndroidService =
      KeyboardAndroidService();
  static final KeyboardMethodHandler _methodHandler = KeyboardMethodHandler();

  static void init() async {
    AtmobLog.d(_tag, '初始化 KeyboardAndroidPlatform ');
    _channel.setMethodCallHandler(_methodHandler.handleMethodCall);

    // 通知插件初始化
    await initPlugin();
    // 默认键盘切换监听器
    DefaultKeyboardHelper.init();
    initFlowingBall();
  }

  /// 初始化悬浮球
  static Future<void> initFlowingBall() async {
    FloatingWindowHelper.init();
    // 有权限，则马上显示悬浮窗
    FloatingWindowHelper.showFloatingBall();
  }

  static Future<void> sendDynamicTextRequest(
    String method,
    String currentContent,
  ) async {
    AtmobLog.d(_tag, 'sendDynamicTextRequest: $method');
    switch (method) {
      case 'MethodA':
        AtmobLog.d(_tag, '处理 MethodA');
        break;
      default:
        AtmobLog.d(_tag, '默认处理');
    }
  }

  /// 初始化插件
  static Future<void> initPlugin() async {
    debugPrint('initPlugin');
    return _keyboardAndroidService.initPlugin();
  }

  /// 打开/关闭悬浮窗
  static void enableFloatingWindow(bool enable) {
    debugPrint('enableFloatingWindow $enable');
    _keyboardAndroidService.enableFloatingWindow(enable);
  }

  /// 跳转到系统的悬浮窗设置页
  static void jumpFloatingWindowSetting() {
    debugPrint('jumpFloatingWindowSetting');
    _keyboardAndroidService.jumpFloatingWindowSetting();
  }

  /// 是否有悬浮窗权限
  static Future<bool> hasFloatingWindowPermission() {
    debugPrint('hasFloatingWindowPermission');
    return _keyboardAndroidService.hasFloatingWindowPermission();
  }

  /// 打开输入法设置
  static void openInputMethodSettings() {
    debugPrint('openInputMethodSettings');
    _keyboardAndroidService.openInputMethodSettings();
  }

  /// 是否启用目标输入法
  static Future<bool> isTargetKeyboardEnabled() async {
    final result = await _keyboardAndroidService.isTargetKeyboardEnabled();
    debugPrint('isTargetKeyboardEnabled value: $result');
    return result;
  }

  /// 是否是默认输入法
  static Future<bool> isDefaultKeyboard() async {
    final result = await _keyboardAndroidService.isDefaultKeyboard();
    debugPrint('isTargetKeyboardEnabled value: $result');
    return result;
  }

  /// 更新键盘信息
  static void updateKeyboardInfo(KeyboardInfo keyboardInfo) {
    _keyboardAndroidService.updateKeyboardInfo(keyboardInfo);
  }

  /// 刷新人设列表
  static void refreshCharacterList() {
    _keyboardAndroidService.refreshCharacterList();
  }

  /// 刷新数据
  static void refreshData() {
    _keyboardAndroidService.refreshData();
  }
}
