/*
    This file is part of Society Chess.
    Society Chess is a network chess game.
    Copyright (C) 2011  Sterling Pickens

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "posix_sockets.h"
#include "opengl.h"
#include "extern_globals.h"
//extern char *Local_Port;
//extern char *Remote_Port;

extern int My_argc;
extern char **My_argv;
extern char *Port;
extern char *Remote_IP;
extern int Send_OK;
extern int IS_Server;
extern int my_turn;
extern int saved_remote;
extern int my_team_color;
extern int local_game;

void Usage(void){
	printf("Error: usage options\n");
	printf("Server: ./test s <port>\n");
	printf("Client: ./test c <remote-ip> <port>\n");
	printf("Local:  ./test\n");
}

int main(int argc, char **argv){
	My_argc = argc;
	My_argv = argv;
	if(argc == 1){
		local_game = 1;
		my_team_color = 0;
		my_turn = 1;
		//IS_Server = 1;
		Create_OpenGL(NULL);
	}
	if(argc != 3 && argc != 4){
		Usage();
		exit(1);
	}
	local_game = 0;
	saved_remote = 0;
	my_team_color = 3;
	char type[2];
	(void)strcpy(type, (const char*)argv[1]);
	if(type[0] == 's'){
		my_turn = 0;
		Send_OK = 0;
		IS_Server = 1;
		Port = (char *)malloc(strlen(argv[2]) + 1);
		//(void)memset(Local_Port, '\0', strlen(argv[2]) + 1);
		(void)strcpy(Port, (const char*)argv[2]);
	}else if( type[0] == 'c'){
		my_turn = 0;
		Send_OK = 0;
		IS_Server = 0;
		//Port = (char *)malloc(strlen(argv[2]) + 1);
		Remote_IP = (char *)malloc(strlen(argv[2]) + 1);
		Port = (char *)malloc(strlen(argv[3]) + 1);
		//(void)strcpy(Local_Port, (const char*)argv[2]);
		(void)strcpy(Remote_IP, (const char*)argv[2]);
		(void)strcpy(Port, (const char*)argv[3]);
	}else{
		Usage();
		exit(1);
	}


	pthread_t *Threads = (pthread_t *)malloc(sizeof(pthread_t) * 3);


	if (pthread_create(&Threads[0], NULL, Net_Recv, NULL) != 0){
		printf("Error: pthread_create failed for thread: %d !\n", 0);
		exit(1);
	}

	if (pthread_create(&Threads[1], NULL, Net_Send, NULL) != 0){
		printf("Error: pthread_create failed for thread: %d !\n", 1);
		exit(1);
	}

	if (pthread_create(&Threads[2], NULL, Create_OpenGL, NULL) != 0){
		printf("Error: pthread_create failed for thread: %d !\n", 2);
		exit(1);
	}


	if (pthread_join(Threads[0], NULL) != 0){
		printf("Error: pthread_join failed for thread: %d !\n", 0);
		exit(1);
	}
	if (pthread_join(Threads[1], NULL) != 0){
		printf("Error: pthread_join failed for thread: %d !\n", 1);
		exit(1);
	}
	if (pthread_join(Threads[2], NULL) != 0){
		printf("Error: pthread_join failed for thread: %d !\n", 2);
		exit(1);
	}

	return 0;
}
