import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:vitalapp/managers/interfaces/models/upgrade.dart';
import 'package:vitalapp/managers/interfaces/permission.dart';
import 'package:vitalapp/managers/interfaces/upgrade.dart';
import 'package:vitalapp/pages/upgrade/state.dart';

class AppUpgradeController extends GetxController {
  final state = AppUpgradeState();
  final upgradeManager = Get.find<IUpgradeManager>();

  @override
  void onInit() async {
    state.currentVersion = await getCurrentVersion();
    super.onInit();
  }

  Future<UpgradeCheckResultModel?> checkNewVersionArrival() async {
    final result = await upgradeManager.checkNewVersionArrival();
    return result;
  }

  Future<bool> downloadPackage(
    String version,
    String url, {
    ValueChanged<double>? onProgress,
  }) async {
    final result = await upgradeManager.downloadPackage(version, url,
        onProgress: onProgress);
    return result;
  }

  Future<void> installPackage(String version) async {
    var permissionManager = Get.find<IPermissionManager>();
    await permissionManager.requestInstallPackagesPermission();
    await upgradeManager.install(version);
  }

  ///获取当前版本号
  Future<String> getCurrentVersion() async {
    var version = await upgradeManager.getCurrentVersion();
    return version;
  }

  void deleteFile(String version) async {
    await upgradeManager.deleteFile(version);
  }
}
