package devices;

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;
import anywheresoftware.b4a.BA;

public interface Kernel32 extends StdCallLibrary {
	public Kernel32 INSTANCE = (Kernel32) Native.load("Kernel32", Kernel32.class);
	
	/**
	 * Fill the structure.
	 */
	public int GetSystemPowerStatus(SYSTEM_POWER_STATUS result);
	
	public class SYSTEM_POWER_STATUS extends Structure{
		public byte ACLineStatus;
		public byte BatteryFlag;
		public byte BatteryLifePercent;
		public byte Reserved1;
		public int BatteryLifeTime;
		public int BatteryFullLifeTime;
		
		@BA.Hide
		public void autoRead() {
			if (getAutoRead()) {
				read();
			}
		}
		
		@BA.Hide
		public void autoWrite() {
			if (getAutoWrite()) {
				write();
			}
		}
		
		@Override
		protected java.util.List<String> getFieldOrder() {
			return java.util.Arrays.asList(new String[] { "ACLineStatus", "BatteryFlag", "BatteryLifePercent", "Reserved1",
					"BatteryLifeTime", "BatteryFullLifeTime" });
		}
		
		/**
		 * Get AC power status
		 * @return Offline, Online or Unknown
		 */
		public String GetACLineStatusString() {
			switch (ACLineStatus) {
			case 0:
				return "Offline";
			case 1:
				return "Online";
			default:
				return "Unknown";
			}
		}
		
		/**
		 * Get Battery Flag Status
		 * @return High, Low, Critical, Charging, No system battery or Unknown
		 */
		public String GetBatteryFlagString() {
			switch (BatteryFlag) {
	        case 1:
	            return "High";
	        case 2:
	            return "Low";
	        case 4:
	            return "Critical";
	        case 8:
	            return "Charging";
	        case (byte)128:
	            return "No system battery";
	        default:
	            return "Unknown";
	        }
		}
		
		/**
		 * Get Battery Life Percent
		 * 
		 * @return Battery Life Percent or Unknown
		 */
		public String GetBatteryLifePercentString() {
			return (BatteryLifePercent==(byte)255) ? "Unknown" : String.valueOf(BatteryLifePercent)+" %";
		}
		
		/**
		 * Get Battery Life Time
		 * @return Battery Life Time in seconds or Unknown
		 */
		public String GetBatteryLifeTimeString() {
			return (BatteryLifeTime == (int) -1) ? "Unknown" : String.valueOf(BatteryLifeTime)+ " seconds";
		}
		
		/**
		 * Get Battery Full Life Time
		 * 
		 * @return Battery Full Life Time in seconds or Unknown
		 */
		public String GetBatteryFullLifeTimeString() {
			return (BatteryFullLifeTime == (int) -1) ? "Unknown" : String.valueOf(BatteryFullLifeTime) + " seconds";
		}
		
		@Override
		/**
		 * Get all information
	     *
		 */
		public String toString() {
			return "ACLineStatus: " + GetACLineStatusString() + "\n" + "BatteryFlag: " + GetBatteryFlagString() + "\n"
					+ "BatteryLifePercent: " + GetBatteryLifePercentString() + "\n" + "BatteryLifeTime: "
					+ GetBatteryLifeTimeString() + "\n" + "BatteryFullLifeTime: " + GetBatteryFullLifeTimeString();
		}
		
	}
}
