package SBC;

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

@BA.ShortName("RAM_Info")
public class RAM_Info {
	
	public final double JVMTotalMemory_KB;
	public final double JVMFreeMemory_KB;
	public final double JVMMaxMemory_KB;
	public final double JVMFreePercentage;
	public final double MemTotal_KB;
	public final double MemFree_KB;
	public final double MemAvailable_KB;
	public final double MemFreePercentage;
	public final double SwapTotal_KB;
	public final double SwapFree_KB;
	public final double SwapFreePercentage;		// tambahan
	public RAM_Info() {
		JVMTotalMemory_KB = 0;
		JVMFreeMemory_KB = 0;
		JVMMaxMemory_KB = 0;
		JVMFreePercentage = 0;		
		MemTotal_KB = 0;
		MemFree_KB = 0;
		MemFreePercentage = 0;		
		MemAvailable_KB = 0;
		SwapTotal_KB = 0;
		SwapFree_KB = 0;
		SwapFreePercentage = 0;
	}
	public RAM_Info(Map value) {
		JVMTotalMemory_KB = (double) value.GetDefault("JVMTotalMemory", 0);
		JVMFreeMemory_KB = (double) value.GetDefault("JVMFreeMemory", 0);
		JVMMaxMemory_KB = (double) value.GetDefault("JVMMaxMemory", 0);
		JVMFreePercentage = (double) value.GetDefault("JVMFreePercentage", 0);		// sudah pembulatan
		MemTotal_KB = (double) value.GetDefault("MemTotal", 0);
		MemFree_KB = (double) value.GetDefault("MemFree", 0);
		MemFreePercentage = (double) value.GetDefault("MemFreePercentage", 0);		// sudah pembulatan
		MemAvailable_KB = (double) value.GetDefault("MemAvailable", 0);
		SwapTotal_KB = (double) value.GetDefault("SwapTotal", 0);
		SwapFree_KB = (double) value.GetDefault("SwapFree", 0);
		SwapFreePercentage = pembulatan_1desimal((SwapFree_KB * 100.0) / SwapTotal_KB );
	}
	
	private double pembulatan_1desimal(double value) {
		int temp = (int)(value * 10);
		return (temp / 10.0);
	}
	
	private final double memory_MB = 1024;
	private final double memory_GB = memory_MB * 1024;
	private final double memory_TB = memory_GB * 1024;
	/**
	 * Return an easy to read from values inside this class
	 * @param value : Member of this class, example MemTotal_KB, or MemAvailable_KB
	 * @return String value
	 */
	public String EasyRead_Value(double value) {
		if (value < memory_MB) {
			return pembulatan_1desimal(value)+ " KB";
		} else if (value < memory_GB) {
			return pembulatan_1desimal(value / memory_MB)+" MB";
		} else if (value < memory_TB) {
			return pembulatan_1desimal(value / memory_GB)+" GB";
		} else return pembulatan_1desimal(value / memory_TB)+" TB";
	}
	
	/**
	 * Create values in JSON Map
	 * @return Map value
	 */
	public Map MakeJSONMap() {
		Map result = new Map();
		result.Initialize();
		result.Put("JVMTotalMemory", EasyRead_Value(this.JVMTotalMemory_KB));
		result.Put("JVMFreeMemory", EasyRead_Value(this.JVMFreeMemory_KB));
		result.Put("JVMMaxMemory", EasyRead_Value(this.JVMMaxMemory_KB));
		result.Put("JVMFreePercentage", this.JVMFreePercentage);
		result.Put("MemTotal", EasyRead_Value(this.MemTotal_KB));
		result.Put("MemFree", EasyRead_Value(this.MemFree_KB));
		result.Put("MemFreePercentage", this.MemFreePercentage);
		result.Put("MemAvailable", EasyRead_Value(this.MemAvailable_KB));
		result.Put("SwapTotal", EasyRead_Value(this.SwapTotal_KB));
		result.Put("SwapFree", EasyRead_Value(this.SwapFree_KB));
		result.Put("SwapFreePercentage", this.SwapFreePercentage);
		return result;
	}
}
