part of 'settings.dart';

/// Created by Haoyi on 2022/8/25

class UtilsSettingsKeys {
  static const PropertyKey version = PropertyKey.setting("version");
  static const PropertyKey buildNumber = PropertyKey.setting("build_number");
  static const PropertyKey firstInstallTime = PropertyKey.general("first_install_time");
  static const PropertyKey firstInstallVersion = PropertyKey.setting("first_install_version");
  static const PropertyKey prevInstallVersion = PropertyKey.general("prev_install_version");
  static const PropertyKey latestInstallVersion = PropertyKey.general("latest_install_version");
  static const PropertyKey previousInstalledVersion =
      PropertyKey.general("previous_installed_version");
  static const PropertyKey appInstanceId = PropertyKey.general("app_instance_id");
  static const PropertyKey soundEffect = PropertyKey.setting("sound_effect", tag: "Game");
  static const PropertyKey vibration = PropertyKey.setting("vibration", tag: "Game");
  static const PropertyKey deviceId = PropertyKey.general("device_id");
  static const PropertyKey debugMode = PropertyKey.setting("debug_mode");
  static const PropertyKey latestLtDate = PropertyKey.general("latest_lt_date");
  static const PropertyKey ltDays = PropertyKey.general("lt_days");
  static const PropertyKey keepOnScreenDuration = PropertyKey.setting("keep_on_screen_duration");
}

mixin UtilsSettings {
  final SettingIntData soundEffect =
      SettingIntData(UtilsSettingsKeys.soundEffect, defaultValue: SoundEffect.on);
  final SettingIntData vibration =
      SettingIntData(UtilsSettingsKeys.vibration, defaultValue: VibrationState.on);

  // 0: off
  // > 0: minutes
  // < 0: infinite
  final SettingIntData keepOnScreenDuration =
      SettingIntData(UtilsSettingsKeys.keepOnScreenDuration, defaultValue: 0);

  final SettingBoolData debugMode =
      SettingBoolData(UtilsSettingsKeys.debugMode, defaultValue: false);
  final SettingStringData version = SettingStringData(UtilsSettingsKeys.version, defaultValue: "");
  final SettingStringData buildNumber =
      SettingStringData(UtilsSettingsKeys.buildNumber, defaultValue: "");
  final SettingStringData firstInstallVersion =
      SettingStringData(UtilsSettingsKeys.firstInstallVersion, defaultValue: "");

  Future _auditVersion(String versionBuildNumber) async {
    final firstInstallTime = await AppProperty.getInstance()
        .getOrCreateInt(UtilsSettingsKeys.firstInstallTime, DateTimeUtils.currentTimeInMillis());
    RuntimeProperty.instance.setInt(UtilsSettingsKeys.firstInstallTime, firstInstallTime);

    final fiv = firstInstallVersion.get();

    final latestInstallVersion = await AppProperty.getInstance()
        .getOrCreateString(UtilsSettingsKeys.latestInstallVersion, fiv);

    if (latestInstallVersion != versionBuildNumber) {
      AppProperty.getInstance()
          .setString(UtilsSettingsKeys.latestInstallVersion, versionBuildNumber);
      AppProperty.getInstance()
          .setString(UtilsSettingsKeys.prevInstallVersion, latestInstallVersion);
      final previousInstalledVersion = await AppProperty.getInstance()
          .getOrCreateString(UtilsSettingsKeys.previousInstalledVersion, fiv);
      List<String> versionList = previousInstalledVersion.split("|");
      versionList.add(versionBuildNumber);
      if (versionList.length > 15) {
        versionList = versionList.sublist(versionList.length - 15);
      }
      AppProperty.getInstance()
          .setString(UtilsSettingsKeys.previousInstalledVersion, versionList.join("|"));
    }
  }

  Future _syncVersion() async {
    final PackageInfo packageInfo = await PackageInfo.fromPlatform();
    final currentVersion = packageInfo.version.replaceAll(RegExp(r'-.*'), '');
    final currentBuildName = packageInfo.buildNumber;
    Log.d("VERSION: ${version.get()}-${buildNumber.get()} => $currentVersion-$currentBuildName");
    if (currentVersion != version.get()) {
      version.set(currentVersion);
    }

    if (currentBuildName != buildNumber.get()) {
      buildNumber.set(currentBuildName);
    }

    final versionBuildNumber = "${version.get()}-${buildNumber.get()}";
    if (firstInstallVersion.get() == "") {
      final fiv = await AppProperty.getInstance()
          .getOrCreateString(UtilsSettingsKeys.firstInstallVersion, versionBuildNumber);
      firstInstallVersion.set(fiv);
    }
    _auditVersion(versionBuildNumber);
  }
}
