package org.josast.AmsatList.Commande;

import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.josast.AmsatList.generated.IARUCoordination;
import org.josast.AmsatList.generated.Satellites;

public class Commande {
	
	public final static int IARUCoordinationUpdate = 1;
	public final static int SatelliteStatusUpdate = 2;
	public final static int NasaIDUpdate=3;
	
	
	private String satName;
	private int nasaId;
	private int action;
	private String valueRef;
	private String valueNew;
	private String comments;
	
	
	
	public Commande(String satName, int nasaId, int action, String valueRef, String valueNew, String comments) {
		super();
		this.satName = satName;
		this.nasaId = nasaId;
		this.action = action;
		this.valueRef = valueRef;
		this.valueNew = valueNew;
		this.comments = comments;
	}

  public Commande(String[] values)
  {
	  
		this.satName = values[0];
		this.nasaId = Integer.parseInt(values[1]);
		this.action = Integer.parseInt(values[2]);
		this.valueRef = values[3];
		this.valueNew = values[4];
		this.comments = values[5];
  }
	

	public String toString()
	{
		return satName +";"+nasaId+";"+action +";"+ valueRef +";"+ valueNew+";"+ comments+";";
	}

	public static int getIARUCoordinationUpdate() {
		return IARUCoordinationUpdate;
	}




	public String getSatName() {
		return satName;
	}

	public void setSatName(String satName) {
		this.satName = satName;
	}

	public int getNasaId() {
		return nasaId;
	}

	public void setNasaId(int nasaId) {
		this.nasaId = nasaId;
	}

	public int getAction() {
		return action;
	}

	public void setAction(int action) {
		this.action = action;
	}

	public String getValueRef() {
		return valueRef;
	}

	public void setValueRef(String valueRef) {
		this.valueRef = valueRef;
	}

	public String getValueNew() {
		return valueNew;
	}

	public void setValueNew(String valueNew) {
		this.valueNew = valueNew;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}
	
	public Satellites execute (Satellites sat)
	{
		/* veriie que c'est le bon sat */
		if(sat.getNasaID() != this.nasaId)
		{
			System.out.println("Erreur satellite");
		}
		else
		{
			switch (this.action)
			{
			case Commande.IARUCoordinationUpdate :
			  IARUCoordination iaru = sat.getIARUCoordination();
			  if (iaru == null)
			  {
				  iaru = new IARUCoordination();
				  sat.setIARUCoordination(iaru);
			  }			  
			  iaru.setCoordinated(Boolean.parseBoolean(this.getValueNew()));
			  break;
			
			case Commande.NasaIDUpdate :
				sat.setNasaID(Integer.parseInt(this.valueNew));		
				break;
			
			
			case Commande.SatelliteStatusUpdate :
				// update Status du satellite
					sat.setStatus(this.valueNew);
				break;
			default : System.out.println("erreur commande");
			
			}
			
		      GregorianCalendar gcal = new GregorianCalendar();
		      XMLGregorianCalendar xgcal = null;
			try {
				xgcal = DatatypeFactory.newInstance()
				        .newXMLGregorianCalendar(gcal);
			} catch (DatatypeConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			sat.setUpdateDate(xgcal);
			
		}
		

		
		
		return sat;
		
	}

}
