package SBC;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.collections.Map;

@BA.ShortName("Storage_Info")
public class Storage_Info {
	public final String Path;
	public final double Total;
	public final double Free;
	public final double FreePercentage;
	public final boolean isvalid;
	
	public Storage_Info() {
		Path="";
		Total=0;
		Free=0;
		FreePercentage=0;
		isvalid = false;
	}
	
	public Storage_Info(Map value) {
		Path = (String) value.GetDefault("Path", "");
		Total = (double) value.GetDefault("Total", 0);
		Free = (double) value.GetDefault("Free", 0);
		FreePercentage = (double) value.GetDefault("FreePercentage", 0);		// sudah pembulatan 1 desimal
		if (!Path.isEmpty()) {
			if (Total>0) {
				isvalid = true;
			} else isvalid = false;
		} else isvalid = false;
	}
	
	private final double file_KB = 1024;
	private final double file_MB = file_KB * 1024;
	private final double file_GB = file_MB * 1024;
	private final double file_TB = file_GB * 1024;
	
	private double pembulatan_1desimal(double value) {
		int temp = (int)(value * 10);
		return (temp / 10.0);
	}
	
	/**
	 * Return an easy to read from values inside this class
	 * @param value : Member of this class, either Total or Free
	 * @return String value
	 */
	public String EasyRead_Value(double value) {
		if (value < file_KB) {
			return pembulatan_1desimal(value)+ " B";
		} else if (value < file_MB) {
			return pembulatan_1desimal(value / file_KB)+" KB";
		} else if (value < file_GB) {
			return pembulatan_1desimal(value / file_MB)+" MB";
		} else if (value < file_TB) {
			return pembulatan_1desimal(value / file_GB)+" GB";
		} else {
			return pembulatan_1desimal(value / file_TB)+" TB";
		}
	}
	
	/**
	 * Create values in JSON Map
	 * @return Map value
	 */
	public Map MakeJSONMap() {
		Map result = new Map();
		result.Initialize();
		
		result.Put("Path", this.Path);
		result.Put("Total", EasyRead_Value(this.Total));
		result.Put("Free", EasyRead_Value(this.Free));
		result.Put("FreePercentage", this.FreePercentage);
		
		return result;
	}
		
		
}
