import 'dart:io';

import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:keyboard/module/character_custom/character_custom_page.dart';
import 'package:keyboard/module/login/login_page.dart';
import 'package:keyboard/module/main/main_page.dart';
import 'package:keyboard/module/store/store_page.dart';
import 'package:keyboard/utils/default_keyboard_helper.dart';

import '../module/intimacy_scale/intimacy_scale_page.dart';
import '../widget/platform_util.dart';

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

  static const MethodChannel _channel = MethodChannel('keyboard_ios');

  static void initialize() {
    _channel.setMethodCallHandler(_handleMethod);
    // 默认键盘切换监听器
    DefaultKeyboardHelper.init();
  }

  static Future<dynamic> _handleMethod(MethodCall call) async {
    switch (call.method) {
      case 'navigateToLogin':
        LoginPage.start();
        break;
      case 'navigateToMember':
        StorePage.start();
        break;
      case 'navigateToCharacterMarket':
        MainPage.start(arguments: {"tabName": "character"});
        break;
      case 'navigateToCustomCharacter':
        CharacterCustomPage.start();
        break;
      case 'navigateToIntimacy':
        IntimacyScalePage.start();
        break;
      case 'isKeyboardAdd':
        break;
      default:
        throw PlatformException(
          code: 'Unimplemented',
          details: '未实现的方法: ${call.method}',
        );
    }
  }

  // 保存token到ios端
  static Future<void> saveAuthToken(String token) async {
    // 通知iOS键盘扩展
    if (PlatformUtil.isIOS) {
      _channel.invokeMethod('saveAuthToken', {'token': token});
    }
  }

  // 保存idfv到ios端
  static Future<void> saveIDFV(String? idfv) async {
    // 通知iOS键盘扩展
    if (PlatformUtil.isIOS) {
      _channel.invokeMethod('saveIDFV', {'idfv': idfv});
    }
  }

  // 保存idfa到ios端
  static Future<void> saveIDFA(String? idfa) async {
    // 通知iOS键盘扩展
    if (PlatformUtil.isIOS) {
      _channel.invokeMethod('saveIDFA', {'idfa': idfa});
    }
  }

  // 保存token到ios端
  static Future<void> clearAuthToken() async {
    // 通知iOS键盘扩展
    if (PlatformUtil.isIOS) {
      _channel.invokeMethod('clearAuthToken');
    }
  }

  // 检查键盘是否已添加
  static Future<bool> isDefaultKeyboard() async {
    try {
      // 调用原生方法并等待返回结果
      final bool result = await _channel.invokeMethod('isDefaultKeyboard');
      return result;
    } on PlatformException catch (e) {
      print('检查键盘是否为默认键盘: ${e.message}');
      return false; // 发生错误时返回 false
    }
  }

  // 检查键盘是否已添加
  static Future<bool> isKeyboardAdded() async {
    try {
      // 调用原生方法并等待返回结果
      final bool result = await _channel.invokeMethod('isKeyboardAdded');
      return result;
    } on PlatformException catch (e) {
      print('检查键盘是否添加失败: ${e.message}');
      return false; // 发生错误时返回 false
    }
  }

  static Future<void> openKeyboardGuide() async {
    // 通知iOS键盘扩展
    if (PlatformUtil.isIOS) {
      _channel.invokeMethod('openKeyboardGuide');
    }
  }
}