package gmt.protocol.ais;
/**
 * AIS Parser SDK
 * AIS Message 5 Class
 * Copyright 2008 by Brian C. Lane <bcl@brianlane.com>
 * All Rights Reserved
 * 
 * @author Brian C. Lane
 */

/**
 * AIS Message 5 class
 * Static and Voyage Related Data
 * 
 */
public class Message05 extends Messages {
    int            version;           // 2 bits          : AIS Version
    long           imo;               // 30 bits         : IMO Number
    String         callsign;          // 7x6 (42) bits   : Callsign
    String         name;              // 20x6 (120) bits : Ship Name
    int            ship_type;         // 8 bits          : Type of Ship and Cargo
    int            dim_bow;           // 9 bits          : GPS Ant. Distance from Bow
    int            dim_stern;         // 9 bits          : GPS Ant. Distance from stern
    int            dim_port;          // 6 bits          : GPS Ant. Distance from port
    int            dim_starboard;     // 6 bits          : GPS Ant. Distance from starboard
    int            pos_type;          // 4 bits          : Type of position fixing device
    long           eta;               // 20 bits         : Estimated Time of Arrival MMDDHHMM
    int            draught;           // 8 bits          : Maximum present static draught
    String         dest;              // 6x20 (120) bits : Ship Destination
    int            dte;               // 1 bit           : DTE flag
    int            spare;             // 1 bit           : spare
    String		   eta_str;

    public int version() { return this.version; }
    public long imo() { return this.imo; }
    public String callsign() { return this.callsign; }
    public String name() { return this.name; }
    public int ship_type() { return this.ship_type; }
    public int dim_bow() { return this.dim_bow; }
    public int dim_stern() { return this.dim_stern; }
    public int dim_port() { return this.dim_port; }
    public int dim_starboard() { return this.dim_starboard; }
    public int pos_type() { return this.pos_type; }
    public long eta() { return this.eta; }
    public int draught() { return this.draught; }
    public String dest() { return this.dest; }
    public int dte() { return this.dte; }
    public int spare() { return this.spare; }
    public String eta_str() { return this.eta_str; }
    
    
	public Message05()
	{
		super();
	}
	
	public void parse( Sixbit six_state )
		throws SixbitsExhaustedException, AISMessageException
	{
		if (six_state.bit_length() != 424 )
			throw new AISMessageException("Message 5 wrong length");
		
		super.parse( 5, six_state );
    
	    this.version      = (int)  six_state.get( 2 );
	    this.imo          = (long) six_state.get( 30 );
	    this.callsign     =        six_state.get_string(7);
	    this.name         =        six_state.get_string(20);	    
	    this.ship_type    = (int)  six_state.get( 8  );
	    this.dim_bow      = (int)  six_state.get( 9  );
	    this.dim_stern    = (int)  six_state.get( 9  );
	    this.dim_port     = (int)  six_state.get( 6  );
	    this.dim_starboard= (int)  six_state.get( 6  );
	    this.pos_type     = (int)  six_state.get( 4  );
	    this.eta          = (long) six_state.get( 20 );
	    this.draught      = (int)  six_state.get( 8  );
	    this.dest         =        six_state.get_string(20);	    
	    this.eta_str      = convertETA(this.eta);	    
	}
	
	private String convertETA(long eta) {
		String result = "00002460";
		try
		{
			long month  = 0;
			long day    = 0;
			long hour   = 24;
			long minute = 60;
			
			String sMonth  = "";
			String sDay    = "";
			String sHour   = "";
			String sMinute = "";
			String sETA    = "";
			
			month  = eta >> 16;
			day    = (eta >> 11) & 0x1F;
			hour   = (eta >>  6) & 0x1F;
			minute = eta & 0x3F;
			
			if (month  < 1 || month  > 12) { month  = 0; }
			if (day    < 1 || day    > 31) { day    = 0; }
			if (hour   < 0 || hour   > 23) { hour   = 24; }
			if (minute < 0 || minute > 59) { minute = 60; }
			
			sMonth  = String.valueOf(month);
			sDay    = String.valueOf(day);
			sHour   = String.valueOf(hour);
			sMinute = String.valueOf(minute);
			
			if (sMonth.length() == 1) {
				sETA = sETA + "0";
			}
			sETA = sETA + sMonth;
			
			if (sDay.length() == 1) {
				sETA = sETA + "0";
			}
			sETA = sETA + sDay;
			
			if (sHour.length() == 1) {
				sETA = sETA + "0";
			}
			sETA = sETA + sHour;
			
			if (sMinute.length() == 1) {
				sETA = sETA + "0";
			}
			sETA = sETA + sMinute;
			
			result = sETA;
					
		} catch (Exception e) {
			result = "00002460";
		}		
		return result;	
	}
	
}
