import 'dart:io';

import 'package:guru_app/firebase/messaging/remote_messaging_manager.dart';
import 'package:guru_utils/datetime/datetime_utils.dart';
import 'package:json_annotation/json_annotation.dart';

/// Created by Haoyi on 2022/8/29

part 'app_models.g.dart';

@JsonSerializable()
class AppDetails {
  @JsonKey(name: "saas_app_id")
  final String saasAppId;
  @JsonKey(name: "authority")
  final String authority;
  @JsonKey(name: "storage_prefix")
  final String storagePrefix;
  @JsonKey(name: "default_cdn_prefix")
  final String defaultCdnPrefix;
  @JsonKey(name: "android_gp_url")
  final String androidGooglePlayUrl;
  @JsonKey(name: "ios_spp_store_url")
  final String iosAppStoreUrl;
  @JsonKey(name: "policy_url")
  final String policyUrl;
  @JsonKey(name: "terms_url")
  final String termsUrl;
  @JsonKey(name: "email_url")
  final String emailUrl;
  @JsonKey(name: "package_name")
  final String packageName;
  @JsonKey(name: "bundle_id")
  final String bundleId;
  @JsonKey(name: "facebook_app_id")
  final String facebookAppId;

  String get appId => Platform.isAndroid ? packageName : bundleId;

  AppDetails(
      {required this.saasAppId,
      required this.authority,
      required this.storagePrefix,
      required this.defaultCdnPrefix,
      required this.androidGooglePlayUrl,
      this.iosAppStoreUrl = '',
      required this.policyUrl,
      required this.termsUrl,
      required this.emailUrl,
      required this.packageName,
      required this.bundleId,
      required this.facebookAppId});

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

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

@JsonSerializable()
class Deployment {
  static const int defaultIgcBalanceSecret = 2654404609;
  static const int defaultLogFileSizeLimit = 1024 * 1024 * 10;
  static const int defaultLogFileCount = 7;
  static const int defaultPersistentLogLevel = 2;
  static const int defaultApiTimeout = 15000; // 15s
  static const int defaultIosSandboxSubsRenewalSpeed = 2;
  static const int defaultTrackingNotificationPermissionPassLimitTimes = 10;
  static const int defaultSubscriptionRestoreGraceCount = 3;
  static const int defaultFullscreenMinInterval = 60;
  static const int defaultSubscriptionGraceDays = DateTimeUtils.dayInMillis;

  @JsonKey(name: "property_cache_size", defaultValue: 256)
  final int propertyCacheSize;

  @JsonKey(name: "enable_dithering", defaultValue: true)
  final bool enableDithering;

  @JsonKey(name: "disable_rewards_ads", defaultValue: false)
  final bool disableRewardsAds;

  @JsonKey(name: "enable_analytics_statistic", defaultValue: true)
  final bool enableAnalyticsStatistic;

  @JsonKey(name: "auto_restore_iap", defaultValue: true)
  final bool autoRestoreIap;

  @JsonKey(name: "init_igc", defaultValue: 500)
  final int initIgc;

  @JsonKey(name: "igc_balance_secret", defaultValue: defaultIgcBalanceSecret)
  final int igcBalanceSecret;

  @JsonKey(name: "sync_account_profile", defaultValue: true)
  final bool syncAccountProfile;

  @JsonKey(name: "auto_request_notification_permission", defaultValue: false)
  final bool autoRequestNotificationPermission;

  @JsonKey(name: "log_file_size_limit", defaultValue: defaultLogFileSizeLimit)
  final int logFileSizeLimit;

  @JsonKey(name: "log_file_count", defaultValue: defaultLogFileCount)
  final int logFileCount;

  @JsonKey(name: "persistent_log_level", defaultValue: defaultPersistentLogLevel)
  final int persistentLogLevel;

  @JsonKey(name: "ios_validate_receipt_password")
  final String? iosValidateReceiptPassword;

  @JsonKey(name: "conversion_events", defaultValue: {})
  final Set<String> conversionEvents;

  @JsonKey(name: "api_connect_timeout", defaultValue: defaultApiTimeout)
  final int apiConnectTimeout;

  @JsonKey(name: "api_receive_timeout", defaultValue: defaultApiTimeout)
  final int apiReceiveTimeout;

  @JsonKey(name: "ios_sandbox_subs_renewal_speed", defaultValue: defaultIosSandboxSubsRenewalSpeed)
  final int iosSandboxSubsRenewalSpeed;

  @JsonKey(name: "ads_compliant_initialization", defaultValue: false)
  final bool adsCompliantInitialization;

  @JsonKey(name: "notification_permission_prompt_trigger", defaultValue: PromptTrigger.rationale)
  final PromptTrigger notificationPermissionPromptTrigger;

  @JsonKey(name: "tracking_notification_permission_pass", defaultValue: false)
  final bool trackingNotificationPermissionPass;

  @JsonKey(
      name: "tracking_notification_permission_pass_limit_times",
      defaultValue: defaultTrackingNotificationPermissionPassLimitTimes)
  final int trackingNotificationPermissionPassLimitTimes;

  @JsonKey(name: "enabled_guru_analytics_strategy", defaultValue: false)
  final bool enabledGuruAnalyticsStrategy;

  @JsonKey(name: "allow_interstitial_as_alternative_reward", defaultValue: false)
  final bool allowInterstitialAsAlternativeReward;

  @JsonKey(name: "show_internal_ads_when_banner_unavailable", defaultValue: false)
  final bool showInternalAdsWhenBannerUnavailable;

  @JsonKey(name: "subscription_restore_grace_count", defaultValue: defaultSubscriptionRestoreGraceCount)
  final int subscriptionRestoreGraceCount;

  @JsonKey(name: "fullscreen_ads_min_interval", defaultValue: defaultFullscreenMinInterval)
  final int fullscreenAdsMinInterval;

  @JsonKey(name: "subscription_grace_period", defaultValue: defaultSubscriptionGraceDays)
  final int subscriptionGraceDays;

  @JsonKey(name: "enabled_sync_account_profile", defaultValue: false)
  final bool enabledSyncAccountProfile;

  @JsonKey(name: "purchase_event_trigger", defaultValue: 1)
  final int purchaseEventTrigger;

  Deployment(
      {this.propertyCacheSize = 256,
      this.enableDithering = true,
      this.disableRewardsAds = false,
      this.enableAnalyticsStatistic = true,
      this.autoRestoreIap = false,
      this.initIgc = 0,
      this.igcBalanceSecret = defaultIgcBalanceSecret,
      this.syncAccountProfile = true,
      this.autoRequestNotificationPermission = false,
      this.logFileSizeLimit = defaultLogFileSizeLimit,
      this.logFileCount = defaultLogFileCount,
      this.persistentLogLevel = defaultPersistentLogLevel,
      this.iosValidateReceiptPassword,
      this.conversionEvents = const {},
      this.apiConnectTimeout = defaultApiTimeout,
      this.apiReceiveTimeout = defaultApiTimeout,
      this.iosSandboxSubsRenewalSpeed = defaultIosSandboxSubsRenewalSpeed,
      this.adsCompliantInitialization = false,
      this.notificationPermissionPromptTrigger = PromptTrigger.rationale,
      this.trackingNotificationPermissionPass = false,
      this.trackingNotificationPermissionPassLimitTimes =
          defaultTrackingNotificationPermissionPassLimitTimes,
      this.enabledGuruAnalyticsStrategy = false,
      this.allowInterstitialAsAlternativeReward = false,
      this.showInternalAdsWhenBannerUnavailable = false,
      this.subscriptionRestoreGraceCount = defaultSubscriptionRestoreGraceCount,
      this.fullscreenAdsMinInterval = defaultFullscreenMinInterval,
      this.subscriptionGraceDays = defaultSubscriptionGraceDays,
      this.enabledSyncAccountProfile = false,
      this.purchaseEventTrigger = 1});

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

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

@JsonSerializable()
class RemoteDeployment {
  @JsonKey(name: "keep_screen_on_duration_m", defaultValue: 0)
  final int keepScreenOnDuration;

  @JsonKey(name: "subscriptionGraceDays")
  final int? subscriptionGraceDays;

  RemoteDeployment(
      {this.keepScreenOnDuration = 0, this.subscriptionGraceDays});

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

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