
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:sentry/sentry.dart';
import 'package:device_info/device_info.dart';

import 'cache.dart';

enum ErrorExceptionEnum{
  /// dart异常
  RangeError,
  ///页面渲染异常
  FlutterErrorDetails,
  ///服务定义异常
  MissingPluginException,
  ///dio请求异常
  DioError
}

Map<String ,ErrorExceptionEnum> errorMap = {
  'RangeError' : ErrorExceptionEnum.RangeError,
  'FlutterErrorDetails' : ErrorExceptionEnum.FlutterErrorDetails,
  'MissingPluginException' : ErrorExceptionEnum.MissingPluginException,
  'DioError' : ErrorExceptionEnum.DioError,
};

class ErrorParsing {
  static dynamic errorStatus(dynamic error){
    ///判断是否是请求异常
    if(errorMap[error.runtimeType.toString()] == ErrorExceptionEnum.DioError){
      return {
        '\n msgData' :error?.response?.data??'',
        '\n 请求域名' :error?.request?.baseUrl ??'',
        '\n 请求类型' : error?.request?.method??'',
        '\n 请求地址' : error?.request?.path??'',
        '\n 错误' : '$error \n'
      };
    }
    return error;
  }

  static Map<String, dynamic> readAndroidBuildData(AndroidDeviceInfo build) {
    return <String, dynamic>{
      ' \n 最新补丁日期':'${build.version.securityPatch}',
      ' \n 操作系统名' : '${build.version.baseOS??'安卓'}',
      ' \n sdkInt':'${build.version.sdkInt}',
      '\n Android版本': '${build.version.release} ',
      '\n 手机品牌 ': '${build.brand} ',
      '\n 手机详细版本': '${build.model} ',
      '\n 外观设计名 ': '${build.device} ',
      '\n 版本号': '${build.display} ',
      '\n 当前手机唯一标识': '${build.fingerprint} ',
      '\n 内核(单词简写)': '${build.hardware} ',
      '\n 主机名 ': '${build.host} ',
      '\n id': '${build.id} ',
      '\n supported32BitAbis': '${build.supported32BitAbis} ',
      '\n supported64BitAbis': '${build.supported64BitAbis} ',
      '\n supportedAbis': '${build.supportedAbis} ',
      '\n 是否真机': '${build.isPhysicalDevice} \n',
    };
  }

  static Map<String, dynamic> readIosDeviceInfo(IosDeviceInfo data) {
    return <String, dynamic>{
      '\n 设备名': '${data.name} ',
      '\n 操作系统名': '${data.systemName} ',
      '\n 系统版本': '${data.systemVersion} ',
      '\n 设备型号': '${data.model} ',
      '\n 设备名(本地)': '${data.localizedModel}',
      '\n 当前设备唯一值': '${data.identifierForVendor} ',
      '\n 是否真机': '${data.isPhysicalDevice} ',
      '\n 版本号': '${data.utsname.version} ',
      '\n 硬件类型': '${data.utsname.machine} \n',
    };
  }
}

class SentryUtils {
  static SentryClient _sentry;
  static final DeviceInfoPlugin _plugin = DeviceInfoPlugin();
  static final _dsn = "http://82646cbdedbe446fa5d1edb62d505290@localhost:9090/1";

  static Future<Map<String, dynamic>> initPlatformState() async {
    Map<String, dynamic> deviceData;
    _sentry = new SentryClient(dsn: _dsn);
    try {
      if (Platform.isAndroid) {
        deviceData = ErrorParsing.readAndroidBuildData(await _plugin.androidInfo);
      } else if (Platform.isIOS) {
        deviceData = ErrorParsing.readIosDeviceInfo(await _plugin.iosInfo);
      }
    } on PlatformException {
      deviceData = <String, dynamic>{
        'Error:': 'Failed to get platform version.'
      };
    }
    return deviceData;
  }

  static Future<Null> reportError(dynamic error, dynamic stackTrace) async {
    print("异常收集 error = ${error.toString()}");
    // print("error.type = ${error.runtimeType.toString()}");
    initPlatformState().then((res) {
      Map<String, dynamic> _errMap = {
        '\n 错误类型': error.runtimeType.toString(),
        '\n error': ErrorParsing.errorStatus(error),
        '\n 设备信息': res,
        '\n 用户信息': '${Cache.user.uid} \n '
      };
      _sentry.captureException(
        exception: _errMap,
        stackTrace: stackTrace,
      );
    }).catchError((err) {
      Map<String, dynamic> _errMap = {
        '\n 错误类型': error.runtimeType.toString(),
        '\n error': ErrorParsing.errorStatus(error),
        '\n 设备信息': '获取设备信息出错',
        '\n 用户信息': '${Cache.user.uid} \n '
      };
      _sentry.captureException(
        exception: _errMap,
        stackTrace: stackTrace,
      );
    });
  }
}