import 'dart:convert';

import 'package:mhunt_launcher/Backend/DebugHelper.dart';

class FileHashEntry{
  String file;
  String hash;
  int bytes;

  FileHashEntry(this.file, this.hash, this.bytes);


  static fromJson(json) {
    FileHashEntry(json['file'], json['hash'], json['bytes']);
  }

  Map<String, dynamic> toJson()=> {
    'file':file,
    'hash':hash,
    'bytes':bytes
  };

  static String listToJson(List<FileHashEntry> list){
    List<Map<String,dynamic>> localFilesJsonList = [];
    for(int i =0; i < list.length; i++){
      localFilesJsonList.add(list[i].toJson());
    }
    return jsonEncode(localFilesJsonList);
  }

  static List<FileHashEntry> listFromJson(String json){
    List<dynamic> list = jsonDecode(json);
    List<FileHashEntry> output = [];
    for(int i= 0; i < list.length; i++){
      if(list[i]==null){continue;}
      FileHashEntry entry = FileHashEntry(list[i]['file'], list[i]['hash'], list[i]['bytes']);
      output.add(entry);
    }

    return output;
  }

  static int getTotalSizeInBytes(List<FileHashEntry> list){
    int out = 0;
    list.forEach((file){
      out += file.bytes;
    });

    return out;
  }

  static double getTotalSizeInMbytes(List<FileHashEntry> list){
    int bytes = getTotalSizeInBytes(list);
    return bytes / 1024 / 1024;
  }
}