#include <rpc/rpc.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifdef __GNUC__
#include "clib.h"
#endif
#include "othello.h"
#include "io.h"
#include "global.h"
#include "remote.h"

/* external functions */
extern void   count_pieces();
extern bool   can_move();
extern int    num_legal_moves();


CLIENT *cl = NULL;
int    progno;
char   *host = NULL;

/*
 * Initialize the io package.
 */

void
init_io(argc, argv)
int    *argc;
char   *argv[];
{
  int i;
  for (i=1; i<*argc; i++) {
    if (argv[i][0]== '-' && argv[i][1]== 'm')
      host = argv[i] + 2;
  };
  if (host == NULL) {
    fprintf(stderr, "No host specified.\n");
    exit(1);
  };
};

void
init_io_for_one_game()
{
  CLIENT *clnt_create();
  static connect_data cd;
  int *retval;

  cl = clnt_create(host, OTHELLOPROG, OTHELLOVERS, "tcp");
  if (cl == NULL) {
    clnt_pcreateerror(host);
    exit(1);
  };
  
  cd.progno = progno;
  cd.color = mycolor;
  cd.level = level;

  retval = connect_1(&cd, cl);
  if (retval == NULL) {
    fprintf(stderr, "Lost contact with server.\n");
    exit(1);
  } else {
    progno = *retval;
  };
}



/*
 * Print some statistics about the state PLAYSTATE.
 */

void
print_statistics(state)
State   *state;
{
    int   mypieces;
    int   yourpieces;
    
    count_pieces(state, &mypieces, &yourpieces);
    printf("me:\t%d\n", mypieces);
    printf("you:\t%d\n", yourpieces);
    fflush(stdout);
}



/*
 * Print a move in text format.
 */

void
print_move(sq)
Square   sq;
{
    if (sq == PASS)
	puts("pass");
    else
	printf("%c%c\n", COL(sq) + 'a' - 1, ROW(sq) + '0');
    fflush(stdout);
}



void
print_my_move(sq)
Square   sq;
{
    static move_data md;
    void *res;

    md.square = sq;
    md.progno = progno;
    res = put_move_1(&md, cl);

    printf("My move: ");
    print_move(sq);
}

void
print_your_move(sq)
Square	sq;
{
  print_move(sq);
};

void
print_message(msg)
char   *msg;
{
    printf("%s", msg);
    fflush(stdout);
}



/*
 * Print a much simplified and somewhat formatted summary of the
 * position in 'state'.
 */

void
print_state(state)
State   *state;
{
    int   my;
    int   yours;
    
    print_board(&(state->board[0]));

    count_pieces(state, &my, &yours);
    printf("I have %d. \tYou have %d.\n\n", my, yours);
    fflush(stdout);
}



/*
 * Print a board with a little extra info.
 */

void
print_board(board)
Board   board;
{
    int   row;
    int   col;
    
    puts("   A B C D E F G H");
    for (row = 1; row <= 8; ++row) {
	putchar(' ');
	putchar(row + '0');
	putchar(' ');
	for (col = 1; col < 9; ++col) {
	    switch (board[SQ(col, row)]) {
	      case EMPTY:
		putchar('.');
		break;
	      case MYCOLOR:
		putchar(mycolor == WHITE ? 'O' : 'X');
		break;
	      case YOURCOLOR:
		putchar(yourcolor == WHITE ? 'O' : 'X');
		break;
	    }
	    putchar(' ');
	}
	putchar(row + '0');
	putchar('\n');
    }
    puts("   A B C D E F G H");
    fflush(stdout);
}


/* ======================= Input =========================*/


/*
 * Print help about commands to the user.
 */

static void
printhelp()
{
    puts("[a-hA-H][1-8]\tput a piece on the location.");
    puts("0\t\tpass - don't move (illegal if you can move)");
    puts("u\t\tundo your last move");
    puts("p\t\tprint the board");
    puts("l[1-9]\t\tset the playing level to the number. 1 is easiest.");
    puts("?\t\tprint this help.");
    fflush(stdout);
}

/*
 * Get the users move, and return it. For getting the users input,
 * the function get_input is called. Also take care of other
 * user requests such as print the board, get help etc.
 */

Square
get_users_move(state)
State   *state;
{
    Square     *retval;
    extern     void sleep();

    printf("Waiting for move:"); fflush(stdout);
    while (1) {
      retval = (Square *)get_move_1(&progno, cl);
      if (retval == NULL) {
	fprintf(stderr, "\nLost contact with server.\n");
	exit(1);
      } else {
	if (*retval == NOMOVE) {
	  printf("."); fflush(stdout);
	  sleep(1);
	} else {
	  printf("\nyour move: ");
	  print_move(*retval);
	  return *retval;
	};
      };
    };
}

void 
start_game() {};

void
show_status(status)
     int status;
{};

bool
another_game()
{
  disconnect_1(&progno, cl);
  return FALSE;

/*
    if (quietmode)
	return FALSE;

    printf("another game (y/n)?");
    do {
	answer = getchar();
    } while (answer != 'y' && answer != 'n');

    return answer == 'y';
*/
}
