import 'package:get/get.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:keyboard/module/keyboard_guide/keyboard_guide_page.dart';
import 'package:keyboard/module/keyboard_tutorial/video/keyboard_tutorial_video_page.dart';

import '../../../plugins/keyboard_android_platform.dart';
import '../../../utils/floating_window_helper.dart';
import '../../../utils/keyboard_tutorial_util.dart';

/// 键盘教程页-Android平台Controller
@injectable
class KeyboardTutorialAndroidViewController extends BaseController {
  /// 是否检查完成教程，完成了就自动跳转去键盘引导页
  Rx<bool> checkTutorialComplete = true.obs;

  /// 键盘是否启用
  Rx<bool> isKeyboardEnable = false.obs;

  /// 悬浮球是否启用
  Rx<bool> isFloatingWindowEnable = false.obs;

  /// 是否步骤都允许了
  bool get isStepComplete =>
      isKeyboardEnable.value && isFloatingWindowEnable.value;

  @override
  void onInit() {
    super.onInit();
    checkSetting();
  }

  /// 设置是否检查完成教程
  void setCheckTutorialComplete(bool check) {
    checkTutorialComplete.value = check;
  }

  /// 检查设置
  void checkSetting() {
    // 检查键盘是否启用
    KeyboardAndroidPlatform.isTargetKeyboardEnabled().then((enable) {
      isKeyboardEnable.value = enable;
    });
    // 检查是否有悬浮窗权限
    KeyboardAndroidPlatform.hasFloatingWindowPermission().then((hasPermission) {
      isFloatingWindowEnable.value = hasPermission;
    });
    // 有权限，则开启悬浮窗
    FloatingWindowHelper.showFloatingBall();
  }

  /// 点击返回
  void clickBack() {
    Get.back();
  }

  /// 点击跳过
  void clickSkip() {
    // 标记，已经完成了键盘引导
    KeyboardTutorialUtil.setNotFirstShowKeyboardTutorial();
    // 跳转到键盘引导页，并关闭自己
    KeyboardGuidePage.startAndOffMe();
  }

  /// 跳转去视频教程引导页
  void clickTutorialVideo() {
    KeyboardTutorialVideoPage.start();
  }

  /// 去启用键盘
  void goEnableKeyboard() {
    KeyboardAndroidPlatform.openInputMethodSettings();
  }

  /// 去启用悬浮窗
  void jumpFloatingWindowSetting() {
    KeyboardAndroidPlatform.jumpFloatingWindowSetting();
  }

  /// 跳转到键盘引导页
  void jump2KeyboardGuide() {
    // 标记，已经完成了键盘引导
    KeyboardTutorialUtil.setNotFirstShowKeyboardTutorial();
    // 跳转，并关闭自己
    KeyboardGuidePage.startAndOffMe();
  }

  /// 检查，是否步骤都完成了
  Future<bool> checkHasComplete() async {
    // 都完成了，则跳转到键盘引导页
    if (await KeyboardTutorialUtil.isTutorialComplete()) {
      jump2KeyboardGuide();
      return true;
    }
    return false;
  }
}
