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

import 'package:flutter/services.dart';
import 'package:get/get_utils/src/platform/platform.dart';
import 'package:injectable/injectable.dart';
import 'package:location/base/base_controller.dart';
import 'package:location/dialog/agreement_dialog.dart';
import 'package:location/module/main/main_page.dart';
import 'package:location/sdk/umeng/umeng_helper.dart';

import '../../utils/privacy_compliance.dart';

@injectable
class SplashController extends BaseController {
  final splashDelayedTime = 2;

  @override
  void onReady() {
    final isAgreePrivacy = PrivacyCompliance.isAgreePrivacyPolicy();
    if (isAgreePrivacy) {
      isAgreePrivacyNextStep(splashDelayedTime);
    } else {
      AgreementDialog.show(cancelClick: () {
        exitApp();
      }, sureClick: () async {
        await _agreePrivacy();
        isAgreePrivacyNextStep(0);
      });
    }
  }

  void isAgreePrivacyNextStep(int splashDelayedTime) {
    _goMain(Duration(seconds: splashDelayedTime));
  }

  void _goMain(Duration delayTime, {Map<String, dynamic>? arguments}) {
    Timer(delayTime, () {
      MainPage.start(arguments: arguments);
    });
  }

  void exitApp() {
    if (GetPlatform.isAndroid) {
      SystemNavigator.pop();
    } else {
      exit(0);
    }
  }

  _agreePrivacy() async {
    await UmengHelper.setPolicyGrantResult(true);
    await PrivacyCompliance.setPrivacyPolicy(true);
  }
}
