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

import 'package:amap_core_fluttify/amap_core_fluttify.dart';
import 'package:amap_location_fluttify/amap_location_fluttify.dart';
import 'package:device_info/device_info.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:package_info/package_info.dart';
import 'package:permission_handler/permission_handler.dart';

final logger = Logger();
final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();

class Application {
  static Router router;
  static Map<int, bool> gameLikes = {};
  static int currentPage = 0;

  static Future<String> getDeviceInfo() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    Map<String, dynamic> infos = {};
    infos['os'] = Platform.isAndroid ? 1 : Platform.isIOS ? 2 : 3;
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    infos['versionCode'] = packageInfo.buildNumber;
    infos['versionName'] = packageInfo.version;
    if (Platform.isIOS) {
      print('IOS设备：');
      IosDeviceInfo info = await deviceInfo.iosInfo;
      infos['name'] = info.name;
      infos['model'] = info.model;
      infos['identifierForVendor'] = info.identifierForVendor;
      infos['isPhysicalDevice'] = info.isPhysicalDevice;
      infos['systemName'] = info.systemName;
      infos['systemVersion'] = info.systemVersion;
      infos['utsname'] = info.utsname;
    } else if (Platform.isAndroid) {
      print('Android设备');
      AndroidDeviceInfo info = await deviceInfo.androidInfo;
      infos['model'] = info.model;
      infos['isPhysicalDevice'] = info.isPhysicalDevice;
      infos['id'] = info.id;
      infos['type'] = info.type;
      infos['tags'] = info.tags;
      infos['device'] = info.device;
      infos['androidId'] = info.androidId;
      infos['board'] = info.board;
      infos['bootloader'] = info.bootloader;
      infos['brand'] = info.brand;
      infos['display'] = info.display;
      infos['fingerprint'] = info.fingerprint;
      infos['hardware'] = info.hardware;
      infos['manufacturer'] = info.manufacturer;
      infos['product'] = info.product;
      var version = info.version;
      infos['baseOS'] = version.baseOS;
      infos['codename'] = version.codename;
      infos['sdkInt'] = version.sdkInt;
      infos['release'] = version.release;
    }
    return json.encode(infos);
  }

  static Future<Location> location() async {
    if (Platform.isIOS) {
      await AmapCore.init('4a8518eb97acf5d1998385907e24696a');
    }
    if (await requestPermission(Permission.location)) {
      return await AmapLocation.instance.fetchLocation().catchError((e) {
        logger.e(e);
      });
    }
    return null;
  }

  static Future<bool> requestPermission(Permission permission) async {
    if (Platform.isAndroid) {
      // 检查当前权限状态。
      PermissionStatus _permissionStatus = await permission.status;

      if (_permissionStatus != PermissionStatus.granted) {
        // 没有权限，发起请求权限
        _permissionStatus = await permission.request();
        return _permissionStatus == PermissionStatus.granted;
      } else {
        return true;
      }
    } else {
      return true;
    }
  }
}
