package nl.sikken.bertrik.hab;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author bertrik
 *
 */
public final class Sentence {
	
	private final String callSign;
	private final int id;
	private final int time;
	private final double latitude;
	private final double longitude;
	private final double altitude;
	
	private final CcittCrc16 crc16 = new CcittCrc16();
	
	private final List<String> extras = new ArrayList<>();

	/**
	 * Constructor with all mandatory fields.
	 * 
	 * @param callSign the call sign
	 * @param id some incrementing number 
	 * @param time the current time (UTC seconds)
	 * @param latitude the latitude (degrees)
	 * @param longitude the longitude (degrees)
	 * @param altitude the altitude (meter)
	 */
	public Sentence(String callSign, int id, int time, double latitude, double longitude, double altitude) {
		this.callSign = callSign;
		this.id = id;
		this.time = time;
		this.latitude = latitude;
		this.longitude = longitude;
		this.altitude = altitude;
	}
	
	/**
	 * Adds a pre-formatted extra field (optional).
	 * 
	 * @param value the pre-formatted value
	 */
	public void addField(String value) {
		extras.add(value);
	}
	
	public String format() throws UnsupportedEncodingException {
		// format time
		final SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
		fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
		final Date date = new Date(1000L * time);
		final String time = fmt.format(date);
		
		// format basic string
		final StringBuilder sb = new StringBuilder();
		sb.append(String.format(Locale.US, "%s,%d,%s,%.6f,%.6f,%.1f", callSign, id, time, latitude, longitude, altitude));
		for (String s : extras) {
			sb.append(",");
			sb.append(s);
		}
		final String basic = sb.toString();

		// append header, checksum, etc
		final byte[] bytes = basic.getBytes("US-ASCII");
		final int crcValue = crc16.calculate(bytes, 0xFFFF);
		final String formatted = String.format("$$%s*%04X\n", basic, crcValue);
		return formatted;
	}

	
	
}

@@ -43,39 +43,36 @@
 ava.
+n
 io.
-UnsupportedEncodingException
+charset.StandardCharsets
 ;%0D%0Ai
@@ -379,19 +379,20 @@
 e final 
-int
+Date
  time;%0D%0A
@@ -969,19 +969,20 @@
 int id, 
-int
+Date
  time, d
@@ -1381,66 +1381,116 @@
 %09%0D%0A%09
-public String format() throws UnsupportedEn
+/**%0D%0A%09 * @return a sentence formatted ac
 co
+r
 ding
-Exception
+ to the basic UKHAS convention%0D%0A%09 */%0D%0A%09public String format()
  %7B%0D%0A
@@ -1634,65 +1634,26 @@
 nal 
-Date date = new Date(1000L * time);%0D%0A%09%09final 
+String time
 String 
-time 
 = fm
@@ -1661,19 +1661,19 @@
 .format(
-dat
+tim
 e);%0D%0A%09%09%0D
@@ -1830,16 +1830,22 @@
 id, time
+String
 , latitu
@@ -2062,18 +2062,33 @@
 tes(
-%22US-
+StandardCharsets.US_
 ASCII
-%22
 );%0D%0A

use Date instead of int, use a standard charset avoiding an UnsupportedEncodingException.