package nxm.audio.net;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import nxm.audio.lib.hwplayer;
import nxm.audio.lib.playerdaemon;

/** Remote player object for RMI communication between Mixer and Player.
    A stub of this object is returned when the Mixer does a lookup on the player.

    @author Lisa Tursky
*/
public class RemotePlayerDelegate extends UnicastRemoteObject implements PlayerIntf {

  static final long serialVersionUID = 5578538881323037726L;

  /** The Audio Player Controller */
  private playerdaemon controller;
  /** The Hardware Player */
  private hwplayer thePlayer;

  /** RemotePlayerDelegate Constructor
   *  @param playerThread the audio player main thread
   *  @throws RemoteException the Remote exception
   */
  public RemotePlayerDelegate(playerdaemon playerThread) throws RemoteException {
    controller = playerThread;
    thePlayer = playerdaemon.getPlayer();
  }

  public String getName() throws RemoteException {
    return "HWPlayer";
  }

  public void setHostPort(String host,int port,int channel,String audiotype,boolean on) throws RemoteException {
    controller.setHostPort(host, port, channel, audiotype, on);
  }

  public void shutdown() throws RemoteException {
    thePlayer.shutdown();
    thePlayer = null;
    controller.shutdown();
    controller = null;
  }

  public void close() throws RemoteException {
    controller.close();
    controller = null;
    thePlayer = null;
  }

  public String getPlayerChannelInfo() throws RemoteException {
    return controller.getPlayerChannelInfo();
  }

  public int addReader(String host,int port,String audiotype,int bufferSize) throws RemoteException {
    return controller.addReader(host,port,audiotype,bufferSize,1);
  }

  public void removeReader(int channel) throws RemoteException {
    controller.removeReader(channel);
  }

  public void isAlive() throws RemoteException { }

  public float getVolume(int lineNum) throws RemoteException {
    return thePlayer.getVolume(lineNum);
  }

  public float getPan(int lineNum) throws RemoteException {
    return thePlayer.getPan(lineNum);
  }

  public boolean getOff(int lineNum) throws RemoteException {
    return thePlayer.getOff(lineNum);
  }

  public void setVolumeRemote(int volume,int lineNum) throws RemoteException {
    setVolumeRemote(volume, lineNum, false);
  }

  public void setVolumeRemote(int volume,int lineNum, boolean convert) throws RemoteException {
    double cvol = volume;
    if (convert) {
      if(volume > 50) {
        cvol = (volume-50)*.40;
      } else {
        cvol = (volume*.80)-40;
      }
      volume = (int)cvol;
    }

    controller.setVolume(volume, lineNum);
  }

  public void setPanRemote(float pan,int lineNum) throws RemoteException {
    setPanRemote(pan, lineNum, false);
  }

  public void setPanRemote(float pan,int lineNum, boolean convert) throws RemoteException {
    double p = pan;
    if (convert) {
      p = pan / 100.0;
    }
    pan = (float)p;
    controller.setPan(pan, lineNum);
  }

  public void setOffRemote(boolean off, int lineNum) throws RemoteException {
    controller.setOff(off, lineNum);
  }

}