import 'dart:async';
import 'dart:io';

import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/consts/constants.dart';
import 'package:electronic_assistant/module/browser/view.dart';
import 'package:electronic_assistant/router/app_pages.dart';
import 'package:electronic_assistant/utils/desktop_shortcut_utils.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:electronic_assistant/widget/alert_dialog.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:get/get.dart';

import '../../main.dart';

class SplashController extends BaseController {
  final splashDelayedTime = 2;

  @override
  void onReady() {
    super.onReady();
    final isAgreePrivacy = isAgreePrivacyPolicy();
    if (isAgreePrivacy) {
      isAgreePrivacyNextStep();
    } else {
      EAAlertDialog.show(
        title: "隐私政策及权限说明",
        contentWidget: RichText(
          textAlign: TextAlign.left,
          text: TextSpan(
            style: TextStyle(
              color: "#5F5F61".toColor(),
              fontSize: 14,
              decoration: TextDecoration.none,
            ),
            children: [
              const TextSpan(
                text:
                    "为了更好地为您服务，我们可能向系统申请一些必要权限，用于基本服务和功能。我们非常重视您的隐私和个人信息，请使用之前请仔细阅读",
              ),
              TextSpan(
                text: "《隐私政策》",
                style: TextStyle(
                  color: "#5E8BFF".toColor(),
                  decoration: TextDecoration.none,
                ),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    if (GetPlatform.isIOS) {
                      BrowserPage.start(Constants.privacyPolicyIos);
                    } else {
                      BrowserPage.start(Constants.privacyPolicy);
                    }
                  },
              ),
              const TextSpan(
                text: "和",
              ),
              TextSpan(
                text: "《用户使用协议》",
                style: TextStyle(
                  color: "#5E8BFF".toColor(),
                  decoration: TextDecoration.none,
                ),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    BrowserPage.start(Constants.userAgreement);
                  },
              ),
              const TextSpan(
                text: "。同意后，我们将继续为您服务。",
              ),
            ],
          ),
        ),
        cancelText: "不同意",
        confirmText: "同意并继续",
        cancelOnTap: () {
          EAAlertDialog.dismiss();
          exit(0);
        },
        confirmOnTap: () async {
          EAAlertDialog.dismiss();
          setPrivacyPolicy(true);
          await initAfterGrant();
          await Future.delayed(const Duration(seconds: 1));
          Get.offNamed(RoutePath.mainTab);
        },
      );
    }
  }

  isAgreePrivacyNextStep() {
    //判断是否有额外操作执行
    if (DesktopShortcutUtils.getRouteMap() != null) {
      final routeMap = DesktopShortcutUtils.getRouteMap();
      if (routeMap?[LaunchAction.key] == LaunchAction.recordAudioAction) {
        _goMain(Duration.zero, arguments: routeMap);
      } else {
        _goMain(Duration(seconds: splashDelayedTime));
      }
    } else {
      _goMain(Duration(seconds: splashDelayedTime));
    }
  }

  void _goMain(Duration delayTime, {Map<String, dynamic>? arguments}) {
    Timer(delayTime, () {
      Get.offNamed(RoutePath.mainTab, arguments: arguments);
    });
  }
}
