import 'package:guru_analytics_flutter/events_constants.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:guru_utils/converts/converts.dart';

/// Created by Haoyi on 2023/2/9
///
part 'analytics_model.g.dart';

@JsonSerializable()
class AnalyticsConfig {
  static const _defaultGoogleDma = [1, 0, 12, 65];
  static const _defaultDmaCountry = [];
  @JsonKey(name: "cap", defaultValue: ["firebase", "facebook", "guru"])
  @joinedStringConvert
  final List<String> capabilities;

  @JsonKey(name: "init_delay_s", defaultValue: 10)
  final int delayedInSeconds;

  @JsonKey(name: "expired_d", defaultValue: 7)
  final int expiredInDays;

  @JsonKey(name: "strategy", defaultValue: '')
  final String strategy;

  @JsonKey(name: "enabled_strategy", defaultValue: false)
  final bool enabledStrategy;

  /// ad_storage,analytics_storage,personalization,user_data
  @JsonKey(name: "google_dma", defaultValue: _defaultGoogleDma)
  final List<int> googleDmaMask;

  @JsonKey(name: "dma_country", defaultValue: _defaultDmaCountry)
  final List<String> dmaCountry;

  AppEventCapabilities toAppEventCapabilities() {
    int capValue = 0;
    if (capabilities.contains("firebase")) {
      capValue |= AppEventCapabilities.firebase;
    }
    if (capabilities.contains("facebook")) {
      capValue |= AppEventCapabilities.facebook;
    }
    if (capabilities.contains("guru")) {
      capValue |= AppEventCapabilities.guru;
    }
    return AppEventCapabilities(capValue);
  }

  bool googleDmaGranted(ConsentType type, int flags) {
    if (type.index < googleDmaMask.length) {
      return (googleDmaMask[type.index] & flags) == googleDmaMask[type.index];
    }
    return _defaultGoogleDma[type.index] & flags == _defaultGoogleDma[type.index];
  }

  AnalyticsConfig(this.capabilities, this.delayedInSeconds, this.expiredInDays, this.strategy,
      this.enabledStrategy, this.googleDmaMask, this.dmaCountry);

  factory AnalyticsConfig.fromJson(Map<String, dynamic> json) => _$AnalyticsConfigFromJson(json);

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

@JsonSerializable()
class UserIdentification {
  @JsonKey(name: 'firebaseAppInstanceId', defaultValue: "")
  final String firebaseAppInstanceId;

  @JsonKey(name: 'idfa')
  final String? idfa;

  @JsonKey(name: "adid")
  final String? adId;

  @JsonKey(name: "gpsAdid")
  final String? gpsAdId;

  UserIdentification({this.firebaseAppInstanceId = '', this.idfa, this.adId, this.gpsAdId});

  factory UserIdentification.fromJson(Map<String, dynamic> json) =>
      _$UserIdentificationFromJson(json);

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

  @override
  String toString() {
    return 'UserIdentification{firebaseAppInstanceId: $firebaseAppInstanceId, idfa: $idfa, adId: $adId, gpsAdId: $gpsAdId}';
  }
}

class ConsentFieldName {
  static const adStorage = "ad_storage";
  static const analyticsStorage = "analytics_storage";
  static const adPersonalization = "ad_personalization";
  static const adUserData = "ad_user_data";
}

enum ConsentType { adStorage, analyticsStorage, adPersonalization, adUserData }
