package org.josast.ModuleSatelliteDatabase;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

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

public class UpdateAmsatListFile {

	
    private Hashtable<String, String> amsatListStatus = new Hashtable<String, String>();
    /**
     *
     */
    private AmsatList al;
    /**
     *
     */
    private AmsatListFile alf = null;

    /**
     * @param filein
     */
    @Deprecated 
    public UpdateAmsatListFile(final String filein) {
        alf = new AmsatListFile();
        al = alf.openAmsatList(filein);
        amsatListStatusSetup();
    }
    public UpdateAmsatListFile() {

        amsatListStatusSetup();
    }
    private void amsatListStatusSetup() {

        // init table status

        amsatListStatus.put("decayed", "Decayed");
        amsatListStatus.put("Decayed", "Decayed");
        amsatListStatus.put("Deep Space", "Deep Space");
        amsatListStatus.put("Deep space", "Deep Space");
        amsatListStatus.put("Fail", "Failed");
        amsatListStatus.put("Failed", "Failed");
        amsatListStatus.put("Launch", "Launch");
        amsatListStatus.put("No operational", "Not operational");
        amsatListStatus.put("No operational", "Not operational");
        amsatListStatus.put("Not amateur Satellite", "Not amateur satellite");
        amsatListStatus.put("Unknown", "Unknown");
        amsatListStatus.put("Not operational", "Not operational");
        amsatListStatus.put("Not Operational", "Not operational");
        amsatListStatus.put("Operational", "Operational");
        amsatListStatus.put("Operationnal", "Operational");
        amsatListStatus.put("To be deployed", "To be deployed");
        amsatListStatus.put("To Be Laucnh", "To be launch");
        amsatListStatus.put("To Be Launch", "To be launch");
        amsatListStatus.put("To be launched", "To be launch");
        amsatListStatus.put("To be Launched", "To be launch");
        amsatListStatus.put("unknown", "Unknown");
        amsatListStatus.put("Unknown", "Unknown");
	}

	/**
     * @param filename
     */
    @Deprecated 
    public void saveUpdateFile(final String filename) {
        alf.save(filename, al);
    }

    /**
     *
     */
    @Deprecated 
    public void updateStatus() {
    	
    	

        // init table status

        amsatListStatus.put("decayed", "Decayed");
        amsatListStatus.put("Decayed", "Decayed");
        amsatListStatus.put("Deep Space", "Deep Space");
        amsatListStatus.put("Deep space", "Deep Space");
        amsatListStatus.put("Fail", "Failed");
        amsatListStatus.put("Failed", "Failed");
        amsatListStatus.put("Launch", "Launch");
        amsatListStatus.put("No operational", "Not operational");
        amsatListStatus.put("No operational", "Not operational");
        amsatListStatus.put("Not amateur Satellite", "Not amateur satellite");
        amsatListStatus.put("Unknown", "Unknown");
        amsatListStatus.put("Not operational", "Not operational");
        amsatListStatus.put("Not Operational", "Not operational");
        amsatListStatus.put("Operational", "Operational");
        amsatListStatus.put("Operationnal", "Operational");
        amsatListStatus.put("To be deployed", "To be deployed");
        amsatListStatus.put("To Be Laucnh", "To be launch");
        amsatListStatus.put("To Be Launch", "To be launch");
        amsatListStatus.put("To be launched", "To be launch");
        amsatListStatus.put("To be Launched", "To be launch");
        amsatListStatus.put("unknown", "Unknown");
        amsatListStatus.put("Unknown", "Unknown");

        List<Satellites> listSatellite = al.getSatellites();

        ListIterator<Satellites> it = listSatellite.listIterator();
        while (it.hasNext()) {
            Satellites sat = it.next();
            String status = sat.getStatus();
            System.out.println(sat.getName() + " " + status +" : ");
            if (status == null) {
                sat.setStatus("Unknown");
                System.out.println(" add status to  :" + sat.getStatus());

            } else {
                status = status.trim();

                if (amsatListStatus.containsKey(status)) {
                    String refstatus = amsatListStatus.get(status);

                    if (refstatus.compareTo(status) != 0) {
                        sat.setStatus(refstatus);
                        System.out.println(
                                "Status change :" + status + "to" + refstatus);
                    }
                } else {
                    sat.setStatus("Unknown");
                    System.out.println(
                            "add status to :" + sat.getStatus());
                }
               
            }
            
        }

    }
    
    
	public Vector<Commande> checksatelliteStatus(FlatSatList ref) {
		Vector<Commande> listeCommande = new Vector<Commande>();

		Enumeration<Integer> idSat = ref.keys();
		while (idSat.hasMoreElements()) {
			Integer key = (Integer) idSat.nextElement();
			FlatSatellite flatSatelliteRef = ref.get(key);
            String status = flatSatelliteRef.getSatelliteStatus();
            
            if (status == null) {
            	
            	Commande cmd = new Commande(flatSatelliteRef.getSatelliteName(), flatSatelliteRef.getSatelliteNoradId(),
						Commande.SatelliteStatusUpdate, "null", "Unknown" , "Set status to Unknown");
            	listeCommande.add(cmd);

            } else {
                status = status.trim();
                if (amsatListStatus.containsKey(status)) {
                    String refstatus = amsatListStatus.get(status);

                    if (refstatus.compareTo(status) != 0) {
                    	
                    	Commande cmd = new Commande(flatSatelliteRef.getSatelliteName(), flatSatelliteRef.getSatelliteNoradId(),
        						Commande.SatelliteStatusUpdate, status, refstatus , "Status change :" + status + "to" + refstatus);
                    	listeCommande.add(cmd);
                    }
                } else {
                	Commande cmd = new Commande(flatSatelliteRef.getSatelliteName(), flatSatelliteRef.getSatelliteNoradId(),
    						Commande.SatelliteStatusUpdate, status, "Unknown" , "Status change :" + status + "to" + "Unknown");
                	listeCommande.add(cmd);
                }
            }
			
		}
		
		return listeCommande;
	}

}
