package devices.ups.ica.sin2100c;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

@BA.ShortName("SIN2100C_Data")
public class SIN2100C_Data {
	
	
	
	private boolean isvalid = false;
	private Map result;
	
	public SIN2100C_Data() {
		isvalid = false;
		result = new Map();
		result.Initialize();
	}
	
	public SIN2100C_Data(InputStream data) {
		isvalid = false;
		result = new Map();
		result.Initialize();
		
		if (data == null) return;
		BufferedReader buf = new BufferedReader(new InputStreamReader(data));
		String rx;
		do {
			try {
				rx = buf.readLine();
				if (rx!=null) {
					rx = rx.trim();
					if (!rx.isEmpty()) {
						//System.out.println("Found "+rx);
						UPSC_Strings ss = new UPSC_Strings(rx);
						if (ss.isWanted()) {
							if (ss.HasValue()) {
								//System.out.println("Put Map, Key="+ss.Key()+", Value="+ss.StringValue());
								result.Put(ss.Key(), ss.StringValue());
							} 
						} 
					}
				}
			} catch (IOException e) {
				rx = null;
				System.out.println("SIN2100C_Data read error, Msg : "+e.getMessage());
				break;
			}
			
		} while(rx != null);
		
		if (result.getSize()>0) isvalid = true;
	}
	
	/**
	 * Check if SIN2100C_Data is valid data
	 * @return true if valid
	 */
	public boolean IsValidData() {
		return isvalid;
	}
	
	/**
	 * Get Battery charge in percent
	 * @return zero if not available
	 */
	public float getBatteryCharge() {
		if (isvalid) {
			if (result instanceof Map) {
				if (result.IsInitialized()) {
					return Float.valueOf((String) result.GetDefault("battery.charge", 0));	
				}
			}
		}
		
		return 0f;
	}
	
	/**
	 * Get Battery Charge in Volt DC
	 * @return zero if not available
	 */
	public float getBatteryVoltage() {
		if (isvalid) {
			if (result instanceof Map) {
				if (result.IsInitialized()) {
					return Float.valueOf((String) result.GetDefault("battery.voltage", 0));	
				}
			}
		}
		
		return 0f;
	}
	
	/**
	 * Get internal UPS Temperature in Celcius
	 * @return zero if not available
	 */
	public float getUPSTemperature() {
		if (isvalid) {
			if (result instanceof Map) {
				if (result.IsInitialized()) {
					return Float.valueOf((String) result.GetDefault("ups.temperature", 0));	
				}
			}
		}
		
		return 0f;
	}
	
	/**
	 * Get Input UPS Voltage in Volt AC
	 * @return zero if not available
	 */
	public float getInputVoltage() {
		if (isvalid) {
			if (result instanceof Map) {
				if (result.IsInitialized()) {
					return Float.valueOf((String) result.GetDefault("input.voltage", 0));	
				}
			}
		}
		
		return 0f;
	}
	
	/**
	 * Get Ouput UPS Voltage in Volt AC
	 * @return zero if not available
	 */
	public float getOutputVoltage() {
		if (isvalid) {
			if (result instanceof Map) {
				if (result.IsInitialized()) {
					return Float.valueOf((String) result.GetDefault("output.voltage", 0));	
				}
			}
		}
		
		return 0f;
	}
	
	
	
	/**
	 * Will return Map , with following keys :
	 *   battery.charge
	 *   battery.voltage
	 *   ups.mfr
	 *   ups.model
	 *   ups.status
	 *   ups.temperature
	 *   ups.firmware
	 *   ups.load
	 *   driver.name
	 *   input.current.nominal
	 *   input.frequency
	 *   input.voltage
	 *   output.voltage
	 *   
	 * @return Map object
	 */
	public Map MakeJSONMap() {
		return result;
	}
}

class UPSC_Strings{
	private final String[] keys= {
			"battery.charge:",
			"battery.voltage:",
			"ups.mfr:",
			"ups.model:",
			"ups.status:",
			"ups.temperature:",
			"ups.firmware:",
			"ups.load:",
			"driver.name:",
			"input.current.nominal:",
			"input.frequency:",
			"input.voltage:",
			"output.voltage:"
	};
	
	private final String mytext;
	private String key="";
	private String value="";
	private float value_number;
	private boolean wanted = false;
	
	public UPSC_Strings(String value) {
		mytext = (value != null) ? value.trim() : "";
		if (mytext.isEmpty()) return;		// gak ada nilainya
		
		for(int ii=0;ii<keys.length;ii++) {
			if (mytext.startsWith(keys[ii])){
				wanted = true;
				String ss = keys[ii];
				key = ss.substring(0,ss.length()-1);	// gak pake titik dua nya
				this.value = mytext.substring(ss.length()).trim();
				return;
			}
		}
	}
	
	public String Key() {
		return key;
	}
	
	public float NumberValue() {
		return value_number;
	}
	
	public String StringValue() {
		return value;
	}
	
	/**
	 * Check if this UPSC_String contain wanted header
	 * @return true if wanted
	 */
	public boolean isWanted() {
		return wanted;
	}
	
	/**
	 * Check if this UPSC_String contain empty value --> invalid value
	 * @return true if value is empty
	 */
	public boolean HasValue() {
		if (value.isEmpty())
			return false;
		else {
			return true;
		}
	}
	
	/**
	 * Check if this UPSC_String contain number value --> valid float value
	 * @return true if value is number
	 */
	public boolean isNumberValue() {
		if (value.isEmpty())
			return false;
		else {
			try {
				Float ff = Float.valueOf(value);
				value_number = ff.floatValue();
				return true;
			} catch(NumberFormatException e) {
				return false;
			}
			
		}
	}
	
	
	
}
