#include "graphic.h"
#include "assert.h"

Graphic graph;
char gr_result [512];

Graphic::Graphic()
{
	// set all pos to -1 initially, means it's not displayed
	
 	comp1.type = "fog";
 	comp1.x = -1;
        comp1.y = -1;

        comp2.type = "fog";
 	comp2.x = -1;
        comp2.y = -1;

        comp1.typeb = "backup_comp";
 	comp1.xb = -1;
        comp1.yb = -1;

        comp2.typeb = "backup_comp";
 	comp2.xb = -1;
        comp2.yb = -1;

        vehicle1.name = "Vehicle 1";
        vehicle1.x = -1;
	vehicle1.y = -1;
	
        vehicle2.name = "Vehicle 2";
        vehicle2.x = -1;
	vehicle2.y = -1;
	
	light1.name = "Light 1";
	light1.x = -1;
	light1.y = -1;
	
	light2.name = "Light 2";
	light2.x = -1;
	light2.y = -1;
	
	tmsx1 = -1;
	tmsy1 = -1;
 	tmsx2 = -1;
 	tmsy2 = -1;

 	first_ov = NULL;
 	ov_ptr = NULL;
 	ov_count = 0;
}


Graphic::~Graphic()
{


}


int
Graphic::set_light_db ( int screen, int xpos, int ypos,
     		int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9, int x10,     		
     		int y1, int y2, int y3, int y4, int y5, int y6, int y7, int y8, int y9, int y10 )
{

 	if (screen == 1 )
 	{
 		light1.x = xpos;
 		light1.y = ypos;
 		light1.x1 = x1;
                light1.x2 = x2;
                light1.x3 = x3;
                light1.x4 = x4;
                light1.x5 = x5;
                light1.x6 = x6;
                light1.x7 = x7;
                light1.x8 = x8;
                light1.x9 = x9;
                light1.x10 = x10;
                light1.y1 = y1;
                light1.y2 = y2;
                light1.y3 = y3;
                light1.y4 = y4;
                light1.y5 = y5;
                light1.y6 = y6;
                light1.y7 = y7;
                light1.y8 = y8;
                light1.y9 = y9;
                light1.y10 = y10;

                /*
                cout <<"graphic.cpp : light1.name = " <<light1.name <<endl;
                cout <<"graphic.cpp : light1.x = " <<light1.x <<endl;
                cout <<"graphic.cpp : light1.y = " <<light1.y <<endl;
                */


	}            	
	else
	{
		light2.x = xpos;
 		light2.y = ypos;
 		light2.x1 = x1;
   		light2.x2 = x2;
   		light2.x3 = x3;
   		light2.x4 = x4;
   		light2.x5 = x5;
   		light2.x6 = x6;
   		light2.x7 = x7;
   		light2.x8 = x8;
   		light2.x9 = x9;
   		light2.x10 = x10;
             	light2.y1 = y1;
             	light2.y2 = y2;
   		light2.y3 = y3;
   		light2.y4 = y4;
	  	light2.y5 = y5;
                light2.y6 = y6;
                light2.y7 = y7;
                light2.y8 = y8;
                light2.y9 = y9;
                light2.y10 = y10;

                /*
	        cout <<"graphic.cpp : light2.x1 = " <<light2.x1 <<endl;
                cout <<"graphic.cpp : light2.y1 = " <<light2.y1 <<endl;
                cout <<"graphic.cpp : light2.x2 = " <<light2.x2 <<endl;
                cout <<"graphic.cpp : light2.y2 = " <<light2.y2 <<endl;
                cout <<"graphic.cpp : light2.x3 = " <<light2.x3 <<endl;
                cout <<"graphic.cpp : light2.y3 = " <<light2.y3 <<endl;
                cout <<"graphic.cpp : light2.name = " <<light2.name <<endl;
                cout <<"graphic.cpp : light2.x = " <<light2.x <<endl;
                cout <<"graphic.cpp : light2.y = " <<light2.y <<endl;
                */

	}


          return 1;

}


int
Graphic::set_graphic_info ( string type, string name, int x, int y, int screen )
{

	if (screen == 1)
	{
		if (type == "vehicle")
		{
		 	vehicle1.name = name;
		 	vehicle1.x = x;
		 	vehicle1.y = y;	
		 	
		// 	cout <<"graphic.cpp: vehicle1.name " <<vehicle1.name<<endl;	
		//      cout <<"graphic.cpp: vehicle1.x " <<vehicle1.x<<endl;	
		//	cout <<"graphic.cpp: vehicle1.y " <<vehicle1.y<<endl;	
			
		
		}
		
		if (type == "compass")
		{        		     	
			if (name == "fog") //primary
		         	{		         	
 				comp1.type = name;
 				comp1.x = x;
     				comp1.y = y;
        			}
		         	
		        if (name == "backup_comp") //secondary
		         	{
		         	comp1.typeb = name;
 				comp1.xb = x;
        			comp1.yb = y;		
                                }
     		/*
		cout <<"--------------------------------\n";       	
        	cout <<"graphic.cpp: comp1.type " <<comp1.type<<endl;	
		cout <<"graphic.cpp: comp1.x " <<comp1.x<<endl;	
		cout <<"graphic.cpp: comp1.y " <<comp1.y<<endl;	
		cout <<"graphic.cpp: comp1.typeb " <<comp1.typeb<<endl;	
		cout <<"graphic.cpp: comp1.xb " <<comp1.xb<<endl;	
		cout <<"graphic.cpp: comp1.yb " <<comp1.yb<<endl;	
		cout <<"graphic.cpp: comp2.x " <<comp2.x<<endl;	
		cout <<"graphic.cpp: comp2.y " <<comp2.y<<endl;	
		cout <<"graphic.cpp: comp2.typeb " <<comp2.typeb<<endl;	
		cout <<"graphic.cpp: comp2.xb " <<comp2.xb<<endl;	
		cout <<"graphic.cpp: comp2.yb " <<comp2.yb<<endl;	
		cout <<"--------------------------------\n";
		*/		
		}	
		
		if (type == "light")
		{
		 	light1.name = name;
		 	light1.x = x;
		 	light1.y = y;	
		 	
		//cout <<"graphic.cpp: light1.name " <<light1.name<<endl;	
		//cout <<"graphic.cpp: light1.x " <<light1.x<<endl;	
		//cout <<"graphic.cpp: light1.y " <<light1.y<<endl;	
		
		} 	
	}
	
	else	// screen 2
	{
	    	if (type == "vehicle")
		{
		 	vehicle2.name = name;
		 	vehicle2.x = x;
		 	vehicle2.y = y;	
		 	
		//cout <<"graphic.cpp: vehicle2.name " <<vehicle2.name<<endl;	
		//cout <<"graphic.cpp: vehicle2.x " <<vehicle2.x<<endl;	
		//cout <<"graphic.cpp: vehicle2.y " <<vehicle2.y<<endl;	
		
		}
		
		if (type == "compass")
		{        		
			if (name == "fog")
				{  	
 				comp2.type = name;
 				comp2.x = x;
        			comp2.y = y;
		        	}
		         	
		        if (name == "backup_comp")
		         	{
		         	comp2.typeb = name;
 				comp2.xb = x;
        			comp2.yb = y; 		         	                            		         	
		         	}
		         	
		         	
        	//	cout <<"graphic.cpp: comp2.type " <<comp2.type<<endl;	
		//      cout <<"graphic.cpp: comp2.x " <<comp2.x<<endl;	
		//	cout <<"graphic.cpp: comp2.y " <<comp2.y<<endl;	
		
		}	
		
		if (type == "light")
		{
		 	light2.name = name;
		 	light2.x = x;
		 	light2.y = y;	
		 	
		 	//cout <<"graphic.cpp: light2.name " <<light2.name<<endl;	
		        //	cout <<"graphic.cpp: light2.x " <<light2.x<<endl;	
			//cout <<"graphic.cpp: light2.y " <<light2.y<<endl;	
		
		}
	}
	       	
	return 1;

}




int
Graphic::get_light_string ( int screen, char graphic_string[] )
{
	
 	if (screen == 1 )
 	{
 		snprintf (graphic_string, 300, "%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d" ,
 		light1.x, light1.y,
 		light1.x1, light1.x2, light1.x3, light1.x4,
   		light1.x5 , light1.x6, light1.x7, light1.x8,
   		light1.x9, light1.x10, light1.y1, light1.y2,
   		light1.y3, light1.y4, light1.y5, light1.y6,   	
                          light1.y7, light1.y8, light1.y9, light1.y10 );
 	}	
 	
 	else
 	{
 	        	snprintf (graphic_string, 300, "%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d" ,
 		light2.x, light2.y,
 		light2.x1, light2.x2, light2.x3, light2.x4,
   		light2.x5 , light2.x6, light2.x7, light2.x8,
   		light2.x9, light2.x10, light2.y1, light2.y2,
   		light2.y3, light2.y4, light2.y5, light2.y6,   	
                          light2.y7, light2.y8, light2.y9, light2.y10 );
 	
 	}
 	
 	//cout <<"graphic.cpp : light_string is : " << graphic_string <<endl;
 	return 1;
 	
}


int
Graphic::get_compass_string ( char graphic_string[]  )
{
	
	snprintf(graphic_string, 300, "%d|%d|%d|%d|%s|%d|%d|%d|%d|%s",
		comp1.x, comp1.y, comp2.x, comp2.y, (comp1.type).c_str(),
		comp1.xb, comp1.yb, comp2.xb, comp2.yb, (comp1.typeb).c_str() );
	//cout <<"graphic.cpp : compass_string is : " << graphic_string <<endl;
 	
	return 1;
}


int
Graphic::get_vehicle_string ( char graphic_string[]  )
{
	
	snprintf (graphic_string, 300, "%d|%d|%d|%d", vehicle1.x, vehicle1.y, vehicle2.x, vehicle2.y );
//	cout <<"graphic.cpp : vehicle_string is : " << graphic_string <<endl;
 	
	return 1;
}	


int
Graphic::add_overlay_db (int type, int channel, int line, int column, int color, char* unit)
{
	
   	
	Overlay *new_over_node;
     	Overlay *temp_ptr;     // Create a temporary pointer
     	
     	
     	// if it has already exists in overlay database, then just OVERWRITE the old one and return 1 as success
     	if (check_overlay_db (type, channel) > 0)
     	{                 		
     	
     		ov_ptr->type = type;
     		ov_ptr->channel = channel;
     		ov_ptr->line = line;	
     		ov_ptr->column = column;
     		ov_ptr->color = color;
     		strcpy(ov_ptr->unit, unit);     	                   		 	    	      	     		
     		
     		cout <<"graphic.cpp: item exist on overlay already"<<endl;
     	     	                 		
     		return -1;                                	
     	}

     	
     	//cout <<"graphic.cpp: adding new overlay item"<<endl;
     	
     	// else, it does not exist in database yet
	// Create a node that contains overlay output info
 	new_over_node = new Overlay;
     	new_over_node->type = type;
     	new_over_node->channel = channel;
     	new_over_node->line = line;
     	new_over_node->column = column;     	
     	new_over_node->color = color;
     	strcpy(new_over_node->unit, unit);
     	      	   	
     	
     	// it doesn exist in Overlay database yet, then add it to the end of overlay database     		                                                                                                           		
     	new_over_node->next = NULL;			// we have this coz we're going to insert it at the end
     	
     	     		
     	temp_ptr = first_ov;
     	
     	
     	// first node
     	if (temp_ptr == NULL)
     		first_ov = new_over_node;
     		
     	else
     	{
     		for (ov_ptr = first_ov; ov_ptr != NULL; ov_ptr = ov_ptr->next)
     		{
     			temp_ptr = ov_ptr;     		                                              	
     		}
  		
     		// insert new node at the end
     		assert(temp_ptr);
     		temp_ptr->next = new_over_node;
  	}
     	  	
  	ov_count++;
  	
	// -------------- display the database ----------------------
	//display_overlay_db();

   	return 1;     	
}	



int
Graphic::delete_all_overlay_db ()
{
        Overlay *ptr;
	Overlay *temp;
	
	if (first_ov == NULL)
		return 0;
		
	ptr = first_ov;
	
 	while ( ptr != NULL )
  	{

  		temp = ptr;
  		ptr = ptr->next;
  		delete temp;
   	}
               		
   	first_ov = NULL;
   	ov_ptr = NULL;
   	ov_count = 0;
   	
 	return 1;
 	  	
}
int
Graphic::delete_overlay_db (int type, int channel)
{
	

	Overlay *temp;
	Overlay *prev_ov;
	
	if (first_ov == NULL)
		return -1;
		
	ov_ptr = first_ov;
	
	cout << "=================== BEGIN FUNCTION =========================\n";
	cout <<"graphic.cpp: type    : "<<type<<endl;
	cout <<"graphic.cpp: channel : "<<channel<<endl;
	
	// Traverse the overlay database to find the node we want to delete	 		
 	for (ov_ptr; ov_ptr != NULL; ov_ptr = ov_ptr->next)
    	{
     		assert (ov_ptr);
     	 	if (ov_ptr->type == type && ov_ptr->channel == channel)
     	 	{
  			// found it 	 		
     	 		cout <<"graphic.cpp: found the overlay"<<endl;
     	 		break;
     		}                                                     		
     		    		   		     	 		                 	 	                               	 		 	    	 	
     	 }
     	
     	 // if removing the first node
	if (ov_ptr == first_ov)
	{
	        first_ov = ov_ptr->next;
	        delete ov_ptr;
		cout<<"graphic.cpp: Deleting the first Overlay node \n";
	 	
		ov_count--;
	 	
	 	return 1;
	}
	
     	 // nothing to be removed
     	 if (ov_ptr == NULL || first_ov == NULL)
     	 	return 0;
     	
     	 // if it is in the middle
     	 for (prev_ov = first_ov; prev_ov->next != ov_ptr; prev_ov = prev_ov->next)
     	 {
     	 	// do nothing and set the previous pointer
     	 }
     	
     	
     	 assert(ov_ptr);
     	 temp = ov_ptr->next;
     	
     	
     	 prev_ov->next =  temp;
     	
     	 cout << "type to be deleted ==== " << ov_ptr->type << '\n';
     	 delete ov_ptr;
     	 ov_count--;
     	
     	
     	 if (ov_count == 0)
	{
		first_ov = NULL;
		ov_ptr = NULL;
	}
	
	
	// -------------- display the database ----------------------
	display_overlay_db();
	
	               	
     	return 1;
}	




int
Graphic::check_overlay_db (int type, int channel)
{

	if (first_ov == NULL)
		return -1;
		
	ov_ptr = first_ov;
	
               while ( ov_ptr != NULL )
               {                         	
               		// if both the type and channel are the same
                	if ( ov_ptr->type == type && ov_ptr->channel == channel )
                            	return 1;
                 	
                 	ov_ptr = ov_ptr->next;

               }

               return -1;
}


char*
Graphic::get_overlay_info (int index)
{

	if (first_ov == NULL)
		return 0;
		
	ov_ptr = first_ov;
	assert (ov_ptr);
	
	int i = 0;
	
 	while ( ov_ptr != NULL && i < index )
 	{                                        		
               	ov_ptr = ov_ptr->next;
               	i++;
 	}

 	snprintf (gr_result, 512, "%d|%d|%d|%d|%d|%s", ov_ptr->type, ov_ptr->channel, ov_ptr->line,
 				ov_ptr->column, ov_ptr->color, ov_ptr->unit);
 	
        return gr_result;

}




int
Graphic::display_overlay_db ()
{

	Overlay *temp;
	
	if (first_ov == NULL)
		return 0;
		
	temp = first_ov;
	
               while ( temp != NULL )
               {
               		cout <<"\n";
               		cout <<"temp->type : "<< temp->type <<endl;
                 	cout <<"temp->channel : "<< temp->channel <<endl;
                 	cout <<"temp->line : "<< temp->line <<endl;
                 	cout <<"temp->column : "<< temp->column <<endl;
                 	cout <<"temp->color : "<< temp->color <<endl;
                 	cout <<"temp->unit : "<< temp->unit <<endl;
                 	cout <<"\n";
               		
                 	temp = temp->next;

               }

               return 0;
}


