package tmcsim.cadsimulator.viewer.model;

import java.util.Iterator;
import java.util.Observable;
import java.util.TreeMap;
import tmcsim.cadsimulator.videocontrol.DVDStatusUpdate;
import tmcsim.cadsimulator.videocontrol.DVDTitleUpdate;
import tmcsim.cadsimulator.viewer.DVDInfoPanel;

/**
 * CADMediaStatus is the status of the DVD connections created by the CAD
 * Simulator. All status and title updates are sent to the corresponding
 * DVDInfoPanel. The DVDs are referenced by connection info, which is unique to
 * each DVD player.
 *
 * @author jdalbey
 */
@SuppressWarnings("serial")
public class CADMediaStatus extends Observable
{

    /**
     * Map of DVDInfoPanels(values) referenced by a DVD's connection info(key).
     */
    private TreeMap<String, DVDInfoPanel> dvdPanels = null;

    /**
     * Constructor. Initialize data and GUI components.
     */
    public CADMediaStatus()
    {

        dvdPanels = new TreeMap<String, DVDInfoPanel>();
    }

    /**
     * Updates the current DVDInfoPanel with the status update. If a panel does
     * not current exist, create one and add a new tab.
     *
     * @param update DVD status update.
     */
    public void updateDVDStatus(DVDStatusUpdate update)
    {
        if (dvdPanels.get(update.connectionInfo) == null)
        {
            dvdPanels.put(update.connectionInfo, new DVDInfoPanel(
                    update.connectionInfo));
        }


        dvdPanels.get(update.connectionInfo).updateDVDStatus(update);
        setChanged();
        notifyObservers();

    }

    public DVDInfoPanel getInfoPanel(String connectionInfo)
    {
        return dvdPanels.get(connectionInfo);
    }

    /**
     * Updates the current DVDInfoPanel with the title update. If a panel does
     * not current exist, create one and add a new tab.
     *
     * @param update DVD title update.
     */
    public void updateDVDTitle(DVDTitleUpdate update)
    {
        if (dvdPanels.get(update.connectionInfo) == null)
        {
            dvdPanels.put(update.connectionInfo, new DVDInfoPanel(update.connectionInfo));
        }

        dvdPanels.get(update.connectionInfo).updateDVDTitle(update);
        setChanged();
        notifyObservers();

    }

    public Iterator<DVDInfoPanel> getDVDlist()
    {
        return dvdPanels.values().iterator();
    }
}
