import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/data/repository/account_repository.dart';
import 'package:keyboard/data/repository/characters_repository.dart';
import 'package:keyboard/data/repository/keyboard_repository.dart';
import 'package:keyboard/utils/atmob_log.dart';

import '../../di/get_it.dart';
import 'keyboard_android_service.dart';
import 'keyboard_method_handler.dart';

@lazySingleton
class KeyboardAndroidPlatform {
  static const _channel = MethodChannel('keyboard_android');
  final _tag = 'KeyboardAndroidPlatform';

  final KeyboardRepository keyboardRepository;
  final CharactersRepository charactersRepository;
  final AccountRepository accountRepository;
  final KeyboardAndroidService _keyboardAndroidService;
  final KeyboardMethodHandler _methodHandler;

  KeyboardAndroidPlatform(this.keyboardRepository, this.charactersRepository,this.accountRepository)
    : _keyboardAndroidService = KeyboardAndroidService(),
      _methodHandler = KeyboardMethodHandler(
        keyboardRepository,
        charactersRepository,
        accountRepository,

      ) {
    AtmobLog.d(_tag, '初始化 KeyboardAndroidPlatform');
    _init();
  }

  void _init() {
    _channel.setMethodCallHandler(_methodHandler.handleMethodCall);
  }

  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, '默认处理');
    }
  }

  void enableFloatingWindow(bool enable) {
    debugPrint('enableFloatingWindow $enable');
    _keyboardAndroidService.enableFloatingWindow(enable);
  }

  void openInputMethodSettings() {
    debugPrint('openInputMethodSettings');
    _keyboardAndroidService.openInputMethodSettings();
  }

  Future<bool> isTargetKeyboardEnabled() {
    final result = _keyboardAndroidService.isTargetKeyboardEnabled();
    debugPrint('isTargetKeyboardEnabled value: $result');
    return result;
  }

  static KeyboardAndroidPlatform getInstance() {
    return getIt.get<KeyboardAndroidPlatform>();
  }
}
