#import "PlayerReceiver.h"
#import "Player.h"
#import <appkit/Window.h>
#import <appkit/TextField.h>
#import <appkit/Application.h>
#import <appkit/Panel.h>
#import <appkit/Matrix.h>
#import <objc/NXStringTable.h>
#import "PlayerListener.h"
#import "PlayerSpeaker.h"
#import <stdio.h>
#define TIMEOUT 5000

@implementation PlayerReceiver

/* see class notes 8-11 */


- appWillInit:sender;
{
    pSpeaker = [[PlayerSpeaker alloc] init];
    [pSpeaker setSendTimeout:TIMEOUT];
    [pSpeaker setDelegate:NXApp];
    [NXApp setAppSpeaker:pSpeaker];
    return self;

}

- getMyName:sender;
{
    char               *myPlayerName = "PlayerA";
    char               *myHostName = "alfred.econ.lsa.umich.edu";
    char               *refereeHost = "alfred.econ.lsa.umich.edu";
    char                realHostname[65];
    int                 speakerResult;
    port_t              refereePort;
    int                 whichRow;

    whichRow = [nameButtons selectedRow];
    switch (whichRow) {
    case 0:
	myPlayerName = "PlayerA";
	break;
    case 1:
	myPlayerName = "PlayerB";
	break;
    case 2:
	myPlayerName = NXCopyStringBuffer([myPlayerNameBox stringValue]);
	break;
    }
    printf("myPlayerName is %s.", myPlayerName);
    pListener = [[PlayerListener alloc] init];
    [pListener setDelegate:NXApp];
    [NXApp setAppListener:pListener];
    [pListener checkInAs:myPlayerName];
    [pListener setTimeout:TIMEOUT];
    [pListener addPort];
    [myWindow setTitle:myPlayerName];
    [myNamePanel close];
    whichRow = [refereeButtons selectedRow];
    switch (whichRow) {
    case 0:
	refereeHost = "alfred.econ.lsa.umich.edu";
	break;
    case 1:
	refereeHost = "petrarch.ccs.itd.umich.edu";
	break;
    case 2:
	refereeHost = NXCopyStringBuffer([myRefereeHostBox stringValue]);
	break;
    }
    [myPlayer refereeHostIs:refereeHost];
    gethostname(realHostname, 65);
    myHostName = NXCopyStringBuffer(realHostname);
    printf("getMyName: Referee host is %s. Player host is %s.\n", refereeHost, myHostName);

    refereePort = [self getPortFor:"Referee" atHost:refereeHost];
    speakerResult = [[NXApp appSpeaker] remoteCheckInPlayer:myPlayerName atHost:myHostName];
    if (speakerResult != 0)
	[self speakerError:"Referee"];

/* Should do some error checking here! */

    return self;
}

//*
//*We have to make sure to free up the speaker and listener we created above
// *

-free;
{
    [pListener free];
    [pSpeaker free];
    [super free];

    return self;
}



- remotePlayGameWith:(char *)opponentName atHost:(char *)opponentHost
    myPullCard:(int)mypull
    myPushCard:(int)mypush
    hisPullCard:(int)hispull
    hisPushCard:(int)hispush;

{
    [myPlayer playGameWith:opponentName atHost:opponentHost
     myPullCard:mypull
     myPushCard:mypush
     hisPullCard:hispull
     hisPushCard:hispush];
    return self;
}

- remotePlayRandomGameWith:(char *)opponentName atHost:(char *)opponentHost
    myPullCard:(int)mypull
    myPushCard:(int)mypush
    hisPullCard:(int)hispull
    hisPushCard:(int)hispush;
{
    [myPlayer playRandomGameWith:opponentName atHost:opponentHost
     myPullCard:mypull
     myPushCard:mypush
     hisPullCard:hispull
     hisPushCard:hispush];
    return self;
}

- remotePlayerReset;
{
    [myPlayer playerReset];
    return self;
}

- remoteComputePayoffs;
{
    [myPlayer computePayoffs];
    return self;
}

- remoteMySidePaymentIs:(int)sidePayment;
{
    [myPlayer mySidePaymentIs:sidePayment];
    return self;
}

- remoteMyChoiceIs:(int)choice;
{
    [myPlayer myChoiceIs:choice];
    return self;
}

- remoteStartPhaseTwo;
{
    [myPlayer startPhaseTwo];
    return self;
}

- remotePlayerNumberIs:(int)num;
{
    [myPlayer playerNumberIs:num];
    return self;
}

- remoteGameIsOver;
{
    NXRunAlertPanel("Game is over...",
		    "Thanks for playing.\n", "OK", 0, 0, NULL);
    exit(0);
    return self;
}

- remoteNudge;
{
    [myPlayer nudge];
    return self;
}

/*----------------------------------------------------------------------*/
/* The following is boilerplate that can be inserted in any application */
/*----------------------------------------------------------------------*/
- (port_t)getPortFor:(char *)appName 
 atHost:(char *)appHost
{
    
    port_t              appPort;

    
    
 /* get port */
    appPort = NXPortFromName(appName, appHost);
    
    if (appPort == PORT_NULL)
	
    {
	NXRunAlertPanel("Define...", "%s Port was not found.\n", 
			"OK", 0, 0, appName);
	
    }
    [[NXApp appSpeaker] setSendPort:appPort];
    
    return appPort;
    
} 

- speakerError:(char *)receiverName 
{
    

    NXRunAlertPanel("Define...", "message to %s failed.\n", 
		    "OK", 0, 0, receiverName);
    
    return self;
    
} 


@end
