package moppydesk.inputs;

import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.*;
import javax.sound.midi.MidiDevice.Info;


public class MoppyMIDIInput implements Transmitter, Receiver{
    MidiDevice currentDevice = null;
    Receiver downstreamReceiver = null;

    public void setDevice(Info deviceInfo) throws MidiUnavailableException{
        if (currentDevice != null){
            currentDevice.close();
        }
        currentDevice = MidiSystem.getMidiDevice(deviceInfo);
        currentDevice.getTransmitter().setReceiver(this);
    }
    
    public void setReceiver(Receiver receiver) {
        downstreamReceiver = receiver;
    }

    public Receiver getReceiver() {
        return downstreamReceiver;
    }

    public void close() {
        if (currentDevice != null){
            currentDevice.close();
            currentDevice = null;
        }
        if (downstreamReceiver != null){
            downstreamReceiver.close();
            downstreamReceiver = null;
        }
    }

    //
    /// Receiver logic for forwarding messages
    //
    
    public void send(MidiMessage message, long timeStamp) {
        //TODO Edit message based on settings.
        downstreamReceiver.send(message, timeStamp);
    }
    
    
    //
    //// UTILITY METHODS
    //
    
    public static HashMap<String,MidiDevice.Info> getMIDIInInfos(){
        MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
        HashMap<String,MidiDevice.Info> outInfos = new HashMap<String,MidiDevice.Info>();
        
        for (MidiDevice.Info i : infos){
            try {
                MidiDevice dev = MidiSystem.getMidiDevice(i);
                if (dev.getMaxTransmitters() != 0){
                    outInfos.put(i.getName(), i);
                }
            } catch (MidiUnavailableException ex) {
                Logger.getLogger(MoppyMIDIInput.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return outInfos;
    }
}

@@ -540,16 +540,46 @@
 eInfo);%0A
+        currentDevice.open();%0A
         
@@ -1263,16 +1263,61 @@
 ttings.%0A
+        if (downstreamReceiver != null)%7B%0A    
         
@@ -1361,16 +1361,26 @@
 Stamp);%0A
+        %7D%0A
     %7D%0A  

Add open() for device, allow nulling of receiver