import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:json_annotation/json_annotation.dart';

import '../device/atmob_platform_info.dart';
import '../utils/app_info_util.dart';

part 'base_request.g.dart';

@JsonSerializable()
class BaseRequest {
  /// 平台类型： 1-Android 2-iOS 3-移动H5 4-PC_WEB 5-微信小程序 6-微信小游戏
  /// 7-微信公众号 8-抖音小程序 9-抖音小游戏 10-鸿蒙APP
  @JsonKey(name: "appPlatform")
  late int appPlatform = 0;

  /// 操作系统：android、ios、mac、windows、linux、harmony
  @JsonKey(name: "os")
  late String os = "unknown";
  @JsonKey(name: "osVersion")
  late String osVersion;

  /// 包信息
  @JsonKey(name: "packageName")
  String? packageName;
  @JsonKey(name: "appVersionName")
  String? appVersionName;
  @JsonKey(name: "appVersionCode")
  int? appVersionCode;

  /// 渠道信息, iOS没有渠道信息, Android通过渠道包工具获取
  @JsonKey(name: "channelName")
  String? channelName;
  @JsonKey(name: "appId")
  int? appId;
  @JsonKey(name: "tgPlatform")
  int? tgPlatform;

  /// 设备信息
  /// Android特有
  @JsonKey(name: "oaid")
  String? oaid;
  @JsonKey(name: "aaid")
  String? aaid;
  @JsonKey(name: "androidId")
  String? androidId;
  @JsonKey(name: "imei")
  String? imei;
  @JsonKey(name: "simImei0")
  String? simImei0;
  @JsonKey(name: "simImei1")
  String? simImei1;
  @JsonKey(name: "mac")
  String? mac;

  /// iOS特有
  @JsonKey(name: "idfa")
  String? idfa;
  @JsonKey(name: "idfv")
  String? idfv;

  /// 公共设备信息
  @JsonKey(name: "machineId")
  String? machineId; //给web使用
  @JsonKey(name: "brand")
  String? brand;
  @JsonKey(name: "model")
  String? model;
  @JsonKey(name: "wifiName")
  String? wifiName;

  /// 地理位置信息
  @JsonKey(name: "region")
  String? region;
  @JsonKey(name: "locLng")
  double? locLng;
  @JsonKey(name: "locLat")
  double? locLat;

  BaseRequest() {
    initPlatformOS();
    initPackageInfo();
    initChannelInfo();
    initDeviceInfo();
  }

  Map<String, dynamic> toJson() => _$BaseRequestToJson(this);

  void initPlatformOS() {
    if (Platform.isAndroid) {
      appPlatform = 1;
      os = "android";
    } else if (Platform.isIOS) {
      appPlatform = 2;
      os = "ios";
    } else if (Platform.isMacOS) {
      os = "mac";
    } else if (Platform.isWindows) {
      os = "windows";
    } else if (Platform.isLinux) {
      os = "linux";
    } else if (Platform.isFuchsia) {
      os = "fuchsia";
    }
    osVersion = Platform.operatingSystemVersion;

    debugPrint("os: $os, osVersion: $osVersion");
  }

  void initPackageInfo() {
    packageName = appInfoUtil.packageName;
    appVersionName = appInfoUtil.appVersionName;
    appVersionCode = appInfoUtil.appVersionCode;
  }

  void initChannelInfo() {
    channelName = atmobPlatformInfo.channelName;
    appId = atmobPlatformInfo.appId;
    tgPlatform = atmobPlatformInfo.tgPlatform;
  }

  void initDeviceInfo() {
    oaid = atmobPlatformInfo.oaid;
    aaid = atmobPlatformInfo.aaid;
    androidId = atmobPlatformInfo.androidId;
    imei = atmobPlatformInfo.imei;
    simImei0 = atmobPlatformInfo.simImei0;
    simImei1 = atmobPlatformInfo.simImei1;
    mac = atmobPlatformInfo.mac;
    idfa = atmobPlatformInfo.idfa;
    idfv = atmobPlatformInfo.idfv;
    machineId = atmobPlatformInfo.machineId;
    brand = atmobPlatformInfo.brand;
    model = atmobPlatformInfo.model;
    wifiName = atmobPlatformInfo.wifiName;
    region = atmobPlatformInfo.region;
    locLng = atmobPlatformInfo.locLng;
    locLat = atmobPlatformInfo.locLat;
  }
}
