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

import 'package:device_info_plus/device_info_plus.dart';
import 'package:get/get.dart';
import 'package:nomo/app/data/sp/ix_sp.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:play_install_referrer/play_install_referrer.dart';

import '../../config/translations/localization_service.dart';
import '../../pigeons/core_api.g.dart';
import '../../utils/device_manager.dart';
import '../../utils/log/logger.dart';
import '../constants/platforms.dart';
import '../data/models/fingerprint.dart';

class ApiController extends GetxService {
  final TAG = 'ApiController';
  //初始化fingerprint
  Fingerprint fp = Fingerprint.empty();

  Future<Fingerprint> initFingerprint() async {
    // 读取app发布渠道
    if (Platform.isIOS) {
      fp.channel = 'apple';
    } else if (Platform.isAndroid) {
      fp.channel = 'google';
    }
    try {
      final advertisingId = await CoreApi().getAdvertisingId();
      fp.googleId = advertisingId ?? '';
    } catch (e) {
      log(TAG, 'read app googleId error: $e');
      fp.googleId = '';
    }
    // 读取应用信息
    final info = await PackageInfo.fromPlatform();
    fp.appVersionCode = int.tryParse(info.buildNumber) ?? 0;
    fp.appVersionName = info.version;
    // 读取设备信息
    final deviceInfo = DeviceInfoPlugin();
    if (Platform.isIOS) {
      fp.platform = Platforms.iOS;
      final iosOsInfo = await deviceInfo.iosInfo;
      fp.deviceModel = iosOsInfo.model;
      fp.deviceOs = iosOsInfo.systemVersion;
      fp.deviceBrand = iosOsInfo.utsname.machine;
    } else if (Platform.isAndroid) {
      fp.platform = Platforms.android;
      final androidOsInfo = await deviceInfo.androidInfo;
      fp.deviceModel = androidOsInfo.model;
      fp.deviceOs = androidOsInfo.version.release;
      fp.deviceBrand = androidOsInfo.brand;
      fp.androidId = androidOsInfo.id;
    }
    //获取设备尺寸
    fp.deviceHeight = Get.height.toInt();
    fp.deviceWidth = Get.width.toInt();

    // 读取设备ID
    fp.deviceId = DeviceManager.getCacheDeviceId();

    try {
      ReferrerDetails referrerDetails =
          await PlayInstallReferrer.installReferrer;
      fp.refer = referrerDetails.installReferrer ?? '';
    } catch (e) {
      log(TAG, 'get install referrer error: $e');
    }
    fp.isNewInstall = IXSP.getIsNewInstall();
    fp.userUuid = IXSP.getUserUuid() ?? '';
    await updateFingerprintData();
    return fp;
  }

  // 更新部分数据
  Future<void> updateFingerprintData() async {
    fp.lang = IXSP.getCurrentLocal().languageCode;
    fp.phoneCountryIso = LocalizationService.getSystemCountry();
    fp.isVpn = await CoreApi().isConnected() ?? false;
    if (!fp.isVpn) {
      fp.isConnectedVpn = false;
    }
    try {
      final simInfo = await CoreApi().getSimInfo();
      // 解析sim
      final sim = jsonDecode(simInfo ?? '{}');
      fp.simReady = sim['simReady'];
      fp.carrierName = sim['carrierName'];
      fp.mcc = sim['mcc'];
      fp.mnc = sim['mnc'];
      fp.countryIso = sim['countryIso'];
      fp.networkCarrierName = sim['networkCarrierName'];
      fp.networkMcc = sim['networkMcc'];
      fp.networkMnc = sim['networkMnc'];
      fp.networkCountryIso = sim['networkCountryIso'];
    } catch (e) {
      log(TAG, 'read app sim error: $e');
    }
  }
}
