import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:sport/bean/game.dart';

class GameManager {

  static String getFileName(GameInfoData game){
    return "${game.version}.apk";
  }

  static Future<File> createFile(GameInfoData game) async {
    Directory documentsDir = await getExternalStorageDirectory();
    String documentsPath = documentsDir.path;
    // 先创建目录
    Directory saveDir = Directory("$documentsPath/apk/${game.id}");
    if (!await saveDir.exists()) {
      saveDir.createSync(recursive: true);
    }
    String savePath = "${saveDir.path}/${getFileName(game)}";
    File _file = File(savePath);

    if (!await _file.exists()) {
      _file.createSync();
    }

    print("file: $_file");

    return _file;
  }

  static deleteFile(GameInfoData game) async {
    Directory documentsDir = await getExternalStorageDirectory();
    String documentsPath = documentsDir.path;
    Directory saveDir = Directory("$documentsPath/apk/${game.id}");
    if (!await saveDir.exists()) {
      return;
    }
    var files = saveDir.listSync();
    files.forEach((element) {
      element.delete();
    });
  }
}
