/*********************************************************************
*                                                                    *          
*                              FLY                                   *
*                           --E#Y#E--				     *
*                         (==MUSCLE==)                               *
*                                                                    *
*********************************************************************/

/* C Header Files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <time.h>
/* X11 */
#include <X11/keysym.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
/* OpenGL */
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <GL/glxext.h>
/* Flysimulator */
#include "WIN/help.h"
#include "WIN/win.h"
#include "WIN/fps.h"
#include "NET/point.h"
#include "NET/network.h"
#include "TEX/texture.h"
#include "CONF/config.h"
#include "CONF/uniform.h"
#include "CONF/geometry.h"
#include "FONT/font.h"
#include "OGL/move.h"
#include "OGL/cube.h"
#include "OGL/flybox.h"
#include "OGL/cockpit.h"
#include "OGL/opengl.h"

/*-------------------------------------------------------------------*/

/* Set our window settings */
const int width = 1280;
const int height = 640;
const int BPP = 16;

/* Network */
socket_t server1;
socket_t server2; 
socket_t client1;

/* Settings */
char fwindow, fgrid, fserver, fclient, fversion, fhelp, wide = FALSE;

/*-------------------------------------------------------------------*/

/* Loader */
void Loader()
 {
	/* Notation */
	char nmove[DC];
	
	nmove[1] =  0;
	nmove[2] =  0;
	nmove[3] = 24;
	nmove[4] =  4;
	
	/* Create Window */
	printf(" ---[ Window ]--------------------------------- \n"); 
        createWin(width, height, BPP, wide);
        createRender(width, height, BPP, wide);
        
        /* FPS */
        InitTimeCounter();
        
        /* Texture Loader */
        printf(" ---[ Texture ]-------------------------------- \n"); 
        textureLoader();
        loadTexture();     
        
        /* GLEW Status */
        printf(" ---[ Library ]-------------------------------- \n"); 
        glewStatus();
                
        /* Network */
        printf(" ---[ Network ]-------------------------------- \n"); 
        printf(" NETWORK PORT: %d \n", PORT);
        
        /* Cord */
        init_Cord();
        make_Cord(nmove[1], nmove[2]);
	print_info();
        print_path(nmove);
 }

/*-------------------------------------------------------------------*/

/* Flysimulator */
void Flysimu()
{     
        const float dt = 0.00001;

        /* Landscape */
        initMain();
        Landscape(width, height);
        
        /* Motor */
        pointGrid(dt);
        motorGrid(dt);
        
        /* Cockpit */
        Motor();
        Cockpit();
        Terminal();
      
        /* Display */
        perspective(width, height);
        loadFont();
        eventloop();
         
        /* FPS */
        CalculateFPS();
        UpdateTimeCounter(dt);	
}

/*-------------------------------------------------------------------*/

/* Server */
void Server()
 {
 	/* TCP Server Network */
        printf(" ---[ Server ]--------------------------------- \n"); 
        server1 = socket(AF_INET, SOCK_STREAM, 0);
        printf(" TCP-SERVER: SOCKET\n");
        net_bind(&server1, INADDR_ANY, PORT );
        printf(" TCP-SERVER: BIND\n");
        net_listen(&server1);	
        printf(" TCP-SERVER: LISTEN\n");
        while(TRUE)
        {
               net_accept( &server1, &server2 );
               printf(" TCP-SERVER: ACCEPT\n");
               while(TRUE)
               {
                      if(fgrid) {
                      Flysimu(); }		
               }
               net_close(&server2);
        }	
        net_close(&server1);
 }

/*-------------------------------------------------------------------*/

/* Client */
void Client()
 {
        /* Server IP */
        char ipadr[DC];
 
	/* TCP Client Network */
	printf(" ---[ Client ]--------------------------------- \n"); 
        printf(" SERVER IP: ");
        fgets(ipadr, DC, stdin);
        client1 = socket(AF_INET, SOCK_STREAM, 0);
        printf(" TCP-CLIENT: SOCKET\n");
        net_connect(&client1, ipadr, PORT);
        printf(" TCP-CLIENT: CONNECT\n");
        while(TRUE)
        {
                 if(fgrid) {
                 Flysimu(); }	
        }	
        net_close(&client1);
 }

/*-------------------------------------------------------------------*/

/* Main Program */
int main(int egac, char **egav) 
{
	/* Settings */	
	int n = 0;
	int f = 0;
      
	for(n = 0; n < egac; n++)
	{
	for(f = 0; f < egav[n][f]; ++f)
	{
	switch(egav[n][f])    
	{
		case 'F': wide = TRUE; break;
		case 'W': fwindow = TRUE; Loader(); break;
	        case 'S': fserver = TRUE; Server(); break;
		case 'C': fclient = TRUE; Client(); break;
	        case 'G': fgrid = TRUE; while(fgrid) { Flysimu(); } break;
		case 'v': fversion = TRUE; version(); break;
		case 'h': fhelp = TRUE; help_Desk(egac); break;
	}}}	
      	 
	return 0;
}

/*-------------------------------------------------------------------------*/

