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/expand.dart';
import 'package:electronic_assistant/utils/mmkv_util.dart';
import 'package:electronic_assistant/widget/alert_dialog.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:get/get.dart';

class SplashController extends BaseController {
  final splashDelayedTime = 2;

  @override
  void onReady() {
    // TODO: implement onInit
    super.onReady();

    final isAgreePrivacy = KVUtil.getBool('isAgreePrivacyKey', false);
    if (isAgreePrivacy) {
      Timer(Duration(seconds: splashDelayedTime), () {
        Get.offNamed(RoutePath.mainTab);
      });
    } 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 = () {
                    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: () {
          EAAlertDialog.dismiss();
          KVUtil.putBool('isAgreePrivacyKey', true);
          Get.offNamed(RoutePath.mainTab);
        },
      );
    }
  }
}
