import 'package:keyboard/data/consts/constants.dart';
import 'package:keyboard/utils/mmkv_util.dart';

import '../module/keyboard_guide/keyboard_guide_page.dart';
import '../module/keyboard_tutorial/keyboard_tutorial_page.dart';
import '../plugins/keyboard_android_platform.dart';

/// 键盘教程工具类
class KeyboardTutorialUtil {
  /// 跳转方法
  /// [isCheckTutorialComplete] 是否需要检查教程完成，就自动跳转到键盘引导页
  static void start({bool isCheckTutorialComplete = true}) async {
    if (isCheckTutorialComplete) {
      bool isComplete = await isTutorialComplete();
      if (isComplete) {
        // 如果完成了教程，则跳转到键盘引导页
        KeyboardGuidePage.start();
      } else {
        // 如果没有完成教程，则跳转到键盘教程页
        KeyboardTutorialPage.start(
          isCheckTutorialComplete: isCheckTutorialComplete,
        );
      }
    } else {
      // 不需要开启自动检查
      KeyboardTutorialPage.start(
        isCheckTutorialComplete: isCheckTutorialComplete,
      );
    }
  }

  /// 第一次显示首页，显示键盘引导页
  static firstMainPageShowTutorial() {
    if (_isFirstShowKeyboardTutorial()) {
      start();
    }
  }

  /// 是否首次显示键盘引导
  static bool _isFirstShowKeyboardTutorial() {
    return KVUtil.getBool(Constants.isFirstShowKeyboardTutorial, true);
  }

  /// 设置为非首次键盘引导
  static void setNotFirstShowKeyboardTutorial() {
    KVUtil.putBool(Constants.isFirstShowKeyboardTutorial, false);
  }

  /// 是否完成了教程
  static Future<bool> isTutorialComplete() async {
    // 是否有悬浮窗权限
    bool hasFloatingWindowPermission =
        await KeyboardAndroidPlatform.hasFloatingWindowPermission();
    // 是否启用了键盘
    bool hasIsUseKeyboard =
        await KeyboardAndroidPlatform.isTargetKeyboardEnabled();
    return hasFloatingWindowPermission && hasIsUseKeyboard;
  }
}
