/*=========================================================================
 
  Program:   Orbit Visualization Tool
  Source:    $Source: /stor/devel/ovt2g/ovt/object/Sats.java,v $
  Date:      $Date: 2001/04/17 15:04:54 $
  Version:   $Revision: 1.23 $
 
 
Copyright (c) 2000 OVT Team (Kristof Stasiewicz, Mykola Khotyaintsev,
Yuri Khotyaintsev)
All rights reserved.
 
Redistribution and use in source and binary forms, with or without
modification is permitted provided that the following conditions are met:
 
 * No part of the software can be included in any commercial package without
written consent from the OVT team.
 
 * Redistributions of the source or binary code must retain the above
copyright notice, this list of conditions and the following disclaimer.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT OR
INDIRECT DAMAGES  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE.
 
OVT Team (http://ovt.irfu.se)   K. Stasiewicz, M. Khotyaintsev, Y.
Khotyaintsev
 
=========================================================================*/

package ovt.object;

import ovt.*;
import ovt.gui.*;
import ovt.util.*;
import ovt.event.*;
import ovt.datatype.*;
import ovt.interfaces.*;


import java.beans.*;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.*;
//import java.lang.reflect.Array;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.event.*;

public class Sats extends BasicObject implements
TimeChangeListener, CoordinateSystemChangeListener, MagPropsChangeListener {
    
    private double minLaunchMjd = Double.MAX_VALUE;
    public static String satsConfigFile;
    
    //public boolean isInPlotList = false;
    //protected static boolean nothingIsShown = true;
    
    
    // Constructor2. Directory with Sat-data files is passed to it.
    public Sats(OVTCore core) {
        super(core, "Satellites");
        Log.log("Sats :: init ...", 3);
        setIcon(new ImageIcon(OVTCore.getImagesDir() + "satellites.gif"));
        satsConfigFile = getCore().getConfDir() + "sats.conf";
        Properties props = getProperties(satsConfigFile);
        
        String orbitDir = core.getOVTHomeDir()+props.getProperty("OrbitDirectory", "odata") + File.separator;
        props.remove("OrbitDirectory");
        
        String prop;
        String satName, orbitFile;
        Sat sat;
        // Everything left is pairs: "SatName "
        System.out.println("Loading Satellites...");
        
        ClusterSats cluster = new ClusterSats(this);
        addChild(cluster);
        Enumeration e = Utils.sort(props.propertyNames());  // sorting for right sequence of cluster (1,2,3,4)
        while (e.hasMoreElements()) {
            prop = (String)(e.nextElement());
            if (prop.endsWith(".OrbitFile")) {
                sat = null;
                satName = prop.substring(0, prop.indexOf(".OrbitFile"));
                orbitFile = props.getProperty(prop);
                System.out.print(Utils.addSpaces(satName, 16) + "\t");
                try {
                    if (orbitFile.toLowerCase().endsWith(".tle")) {
                        sat = new Sat(satName, orbitDir + orbitFile, getCore());
                        addChild(sat);
                    } else if (orbitFile.toLowerCase().endsWith(".cl")) {
                        sat = new ClusterSat(satName, orbitDir + orbitFile, getCore());
                        cluster.add(sat);
                    } else {
                        System.err.println("Unsupported OrbitFile format: '"+orbitFile+"'");
                        System.exit(-1);
                    }
                    System.out.println("[   OK   ] "+new Time(sat.getFirstDataMjd()) +
                        " - "+new Time(sat.getLastDataMjd()));
                } catch (IOException e2) {
                    System.out.println("[ FAILED ] (" + orbitFile + ")");
                }
            }
            //updateMinLaunchMjd();
        }
    }
    
    private Properties getProperties(String satsConfigFile) {
        Properties props = new Properties();
        try {
            FileInputStream in = new FileInputStream(satsConfigFile);
            props.load(in);
        } catch (IOException e) {
            System.out.println("Error loading Satellite's properties from " + satsConfigFile);
            System.err.println(e.toString());
            System.exit(-1);
        }
        return props;
    }
    
    
    
    
    // returns minimal launchMjd value of all sats
/*
public void updateMinLaunchMjd() {
  double mintime =  Double.MAX_VALUE;
  double elem;
  Enumeration e = sats.elements();
 
  while(e.hasMoreElements()) {
    elem = ((Sat)(e.nextElement())).getFirstDataMjd();
    if (elem < mintime) mintime = elem;
  }
 
  if (mintime == Double.MAX_VALUE) minLaunchMjd = 0;
  else minLaunchMjd = mintime;
}
 
public double getMinLaunchMjd() {
  return minLaunchMjd;
}*/
    
    public Hashtable getVisibleSats() {
        Hashtable res = new Hashtable();
        Enumeration e = getChildren().elements();
        while (e.hasMoreElements()) {
            Object obj = e.nextElement();
            Sat sat = (Sat)obj;
            
            if (sat.isVisible()) res.put(sat.getName(), sat);
        }
        return res;
    }
    
    
  /** Returns a hashtable of all sats */
    public Children getAllSats() {
        Children res = new Children();
        Enumeration e = getChildren().elements();
        Enumeration e2;
        OVTObject obj, obj2;
        while (e.hasMoreElements()) {
            obj = (OVTObject)e.nextElement();
            if (obj instanceof Sat) {
                res.put(obj);
            } else if (obj instanceof ClusterSats) {
                // add all clauster sats
                e2 = ((ClusterSats)obj).getChildren().elements();
                while (e2.hasMoreElements()) {
                    obj2 = (OVTObject)e2.nextElement();
                    res.put(obj2);
                }
            }
        }
        return res;
    }
    
    public Sat get(String satName) {
        return (Sat)getAllSats().get(satName);
    }
    
    public void timeChanged(TimeEvent evt) {
        Enumeration e = getChildren().elements();
        while (e.hasMoreElements()) {
            try {
                ((TimeChangeListener)(e.nextElement())).timeChanged(evt);
            } catch (ClassCastException e2) {
                System.out.println("this sat doesn't care about time..");
            }
        }
    }
    
  /** Tell all sats about cs change */
    public void coordinateSystemChanged(CoordinateSystemEvent evt) {
        Enumeration e = getChildren().elements();
        while (e.hasMoreElements()) {
            try {
                ((CoordinateSystemChangeListener)(e.nextElement())).coordinateSystemChanged(evt);
            } catch (ClassCastException e2) {
                System.out.println("this sat doesn't care about cs..");
            }
        }
    }
    
    public void magPropsChanged(MagPropsEvent evt) {
        Enumeration e = getChildren().elements();
        while (e.hasMoreElements()) {
            try {
                ((MagPropsChangeListener)(e.nextElement())).magPropsChanged(evt);
            } catch (ClassCastException e2) {
                System.out.println("this sat doesn't care about magProps..");
            }
        }
    }
}
