#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include <sys/time.h>
#include <unistd.h>

//#include <pthread.h>

#include "Global.h"

#include "Random.h"
#include "Dice_Funcs.h"

#include "Gtk_Structs.h"
#include "Gtk_Funcs.h"

/*
extern uint32_t Exit_Program;
extern uint32_t Num_Players;
extern uint32_t Dice;
extern uint32_t Simulations;
extern uint32_t Min_Get_Onboard;
extern uint32_t Straight_Score;
extern uint32_t Three_Pairs_Score;
*/
extern struct All_Gtk_Widgets gtk;
extern struct Player_Data *pd;
extern struct Game_Status gs;

/*
void Usage(void){
	printf("Usage:\n");
	printf("./rns [-d dice] [-h]\n");
	printf("\t[optional]\n");
	printf("\tdice\t= dice to roll\n"); 
	
}
*/

/*
int main(int argc, char **argv){
	struct timeval cur;
	gettimeofday(&cur, NULL);
	uint64_t rseed = (uint64_t)cur.tv_usec + 3;
	uint32_t num1, num2;
	struct Player_Data *pd = (struct Player_Data *)malloc(sizeof(struct Player_Data) * Num_Players);
	//uint32_t turn_score[2] = {0, 0};
	//uint32_t cumulative_score[2] = {0, 0};
	//uint8_t onboard[2] = {0, 0};
	//uint32_t wins[2] = {0, 0};
	uint32_t turn = 0;
	//const uint32_t players = 2;
	uint8_t ten_k_reached = 0;
	uint32_t tentative_winner = 0;
	uint32_t leader_score = 0;
	//struct All_Gtk_Widgets gtk;
	pthread_t gtk_thread;
	pthread_t worker_thread;

	//initialize player data defaults
	for(num1=0; num1<Num_Players; num1++){
		pd[num1].onboard = 0;
		pd[num1].type = COMPUTER_PLAYER;
		pd[num1].min_dice_will_roll = 3;
		pd[num1].min_score_will_keep = 200;
		//pd[num1].filler = 0;
		pd[num1].turn_score = 0;
		pd[num1].cumulative_score = 0;
		pd[num1].wins = 0;
	}
	//pd[num1].type =
*/

int main(int argc, char **argv){
	//pthread_t gtk_thread;
	//pthread_t worker_thread;
	struct timeval cur;
	gettimeofday(&cur, NULL);
	Set_Default_Game_Status();
	pd = (struct Player_Data *)malloc(sizeof(struct Player_Data) * gs.Num_Players);
    //set defaults for 0/1 players
	gs.rseed = (uint64_t)cur.tv_usec + 3;

	pd[0].onboard = 0;
	pd[0].min_dice_will_roll = 0;
	pd[0].type = LOCAL_PLAYER;
	pd[0].min_score_will_keep = 0;
	pd[0].turn_score = 0;
	pd[0].cumulative_score = 0;
	pd[0].wins = 0;

	//pd[1].rseed = (uint64_t)cur.tv_usec + 4;

	pd[1].onboard = 0;
	pd[1].min_dice_will_roll = 3;
	pd[1].type = COMPUTER_PLAYER;
	pd[1].min_score_will_keep = 250;
	pd[1].turn_score = 0;
	pd[1].cumulative_score = 0;
	pd[1].wins = 0;

	gtk_init(&argc, &argv);
	GTK_Func();
/*
	//launch gtk thread
	if(pthread_create(&worker_thread, NULL, Worker_Func, NULL) != 0){
		printf("Error: pthread_create failed to launch worker thread !\n");
		free(pd);
		exit(1);
	}//void *GTK_Func(void *data)
	//launch worker thread
	if(pthread_create(&gtk_thread, NULL, GTK_Func, NULL) != 0){
		printf("Error: pthread_create failed to launch gtk thread !\n");
		free(pd);
		exit(1);
	}
	//allow for modifications of all variables
	//roll 1 dice to see who goes first
	//start game
	if(pthread_join(gtk_thread, NULL) != 0){
		printf("Error: pthread_join failed to join gtk thread!\n");
		free(pd);
		exit(1);
	}
	if(pthread_join(worker_thread, NULL) != 0){
		printf("Error: pthread_join failed to join worker thread!\n");
		free(pd);
		exit(1);
	}
*/
	free(pd);
	return 0;
}

/*
	while(ten_k_reached != 1){ //untill 10K is reached by 1 player
		for(num1=0; num1<Num_Players; num1++){
			printf("Player %u\n", num1);
			if(pd[num1].type == COMPUTER_PLAYER){
				pd[num1].turn_score = Roll_Dice_AI(&rseed, &pd[num1]);
			}else{
				pd[num1].turn_score = Roll_Dice(&rseed, &pd[num1]);
			}
			if(pd[num1].onboard == 1){
				printf("\tadded %u to total score\n", pd[num1].turn_score);
				pd[num1].cumulative_score += pd[num1].turn_score;
				printf("\t\ttotal: %u\n", pd[num1].cumulative_score);
			}else if(pd[num1].turn_score >= Min_Get_Onboard){
				pd[num1].onboard = 1;
				printf("\tgot onboard with %u\n", pd[num1].turn_score);
				pd[num1].cumulative_score = pd[num1].turn_score;
			}else{
				printf("\tno score for turn\n");
				continue;
			}
			if(pd[num1].cumulative_score >= 10000){
				printf("10K Reached by player %u with score of %u\n", num1, pd[num1].cumulative_score);
				ten_k_reached = 1;
				tentative_winner = num1;
				break;
			}
		}
		turn++;
	}

	leader_score = pd[tentative_winner].cumulative_score;
	if(Num_Players == 1){
		printf("Finished in %u turns with %u score\n", turn, leader_score);
		free(pd);
		return 0;
	}else{
		printf("tentative winner: %u score: %u\n", tentative_winner, leader_score);
	}
	printf("turns: %u\n", turn);
	//each player that is not tentative winner gets a chance to take win
	for(num1=0; num1<Num_Players; num1++){
		if(num1 == tentative_winner)
			continue;
		pd[num1].onboard = 1;
		num2 = leader_score - pd[num1].cumulative_score;
		pd[num1].min_score_will_keep = num2;
		if(pd[num1].type == COMPUTER_PLAYER)
			pd[num1].turn_score = Roll_Dice_AI(&rseed, &pd[num1]);
		else
			pd[num1].turn_score = Roll_Dice(&rseed, &pd[num1]);
		pd[num1].cumulative_score += pd[num1].turn_score;
		if(pd[num1].cumulative_score > leader_score)
			leader_score = pd[num1].cumulative_score;
		printf("Player %u got %u for total of %u\n", num1, pd[num1].turn_score, pd[num1].cumulative_score);
	}

	//player with highest score wins!
	for(num1=0; num1<Num_Players; num1++){
		if(pd[num1].cumulative_score == leader_score) //a tie can happen here
			printf("winner: %u score: %u\n", num1, pd[num1].cumulative_score);
	}
	//printf("turns: %u\n", turn);
	printf("Game Over.\n");
	free(pd);
	return 0;
}
*/