import 'package:minecraft_server_installer/main/adapter/gateway/installation_api_service.dart';
import 'package:minecraft_server_installer/main/adapter/gateway/installation_file_storage.dart';
import 'package:minecraft_server_installer/main/application/repository/installation_repository.dart';

class InstallationRepositoryImpl implements InstallationRepository {
  final InstallationApiService _installationApiService;
  final InstallationFileStorage _installationFileStorage;

  InstallationRepositoryImpl(this._installationApiService, this._installationFileStorage);

  @override
  Future<void> downloadFile(Uri url, String path, {DownloadProgressCallback? onProgressChanged}) async {
    final fileBytes = await _installationApiService.fetchRemoteFile(url, onProgressChanged: onProgressChanged);
    await _installationFileStorage.saveFile(fileBytes, path);
  }

  @override
  Future<void> writeFile(String path, String content) => _installationFileStorage.writeFile(path, content);

  @override
  Future<void> grantFileExecutePermission(String path) {
    return _installationFileStorage.grantFileExecutePermission(path);
  }
}
