
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>

#include "../plx_lib/plx.h"






/* run this program using the console pauser or add your own getch, system("pause") or input loop */



int main(int argc, char** argv) {
	
	int nextcol = 1;
	int row = 0; 
	std::map<int,int> mapping; 
	std::vector<std::vector<double> *> store;
	std::vector<std::string> headings;
	size_t constexpr size_step = 10000;
	
	FILE  * f;
	if(argc > 1) {
	
	   f=fopen(argv[1],"r");
	   if(f){
	   	  while (!feof(f))
				{
				int key;
				int index;
			  int value;
						  
				
				int c = fscanf(f,"%d,%d,%d\n",&key,&index,&value);
				if(c)
				{
				int search = key * 10 + index; 
				
				int & col = mapping[search];
				
				if(col == 0)
				{
					col = nextcol++;
					mapping[search] = col;
				}
				
       if (col > store.size())
        {
				  if(key <=  PLX_X_CHT )
				  {
					
        	std::string st = PLX_Obs_Names[key];
				  char c_code = '1'+ index;
					st += c_code;
          headings.push_back( st ); // save column heading 
        	std::vector<double> * new_vec = new std::vector<double>; 
			//		new_vec->reserve(size_step);  // reserve a big chunk of store 
  				store.push_back(new_vec); // save value
			    }
				}
				
	      std::vector<double> * vec = store[col-1];
				if(vec)
				{
				
				if(vec->capacity() <= row )
				{
					vec->reserve(vec->capacity() + size_step);
				}	
				
				// decode scaling 
				double res = 0; 
				switch (key)
				{
					case PLX_Volts:
						 vec->push_back(ConveriMFDRaw2Data(key ,0, value)) ;
						 break;
					case PLX_AFR:
			    	 vec->push_back(ConveriMFDRaw2Data(key , AFR_Gasoline, value)) ;
						 break;
				  case PLX_RPM:
						 vec->push_back( ConveriMFDRaw2Data(key ,0, value)) ;
          		 break;
				  case PLX_MAP:
				  	  vec->push_back(ConveriMFDRaw2Data(key ,PRESSURE_kPa, value)) ;
              break;
          case PLX_X_CHT:
       	     vec->push_back(ConveriMFDRaw2Data(key ,TEMP_Celsius, value)); 
 		 				 break;
					case PLX_FluidPressure:
					    vec->push_back(ConveriMFDRaw2Data(key ,PRESSURE_PSI_Oil, value)); 	
							break;	 				 
					default:
						break;
				}
				
				if(col == 1)
				{
					row++;
				}

}
			//	printf("%3d %3d %10d\n", key,index,value);
					
				}}
				
			}
	   	
}
	
	// having gathered all the data print it
	
	for(auto it = headings.begin(); it != headings.end() ;it++)
	{
		printf("%s,",(*it).c_str());
	}
		printf("\n");
	
	for(size_t print = 0; print < row ; print++)
	{
	

	for(auto it = store.begin(); it != store.end() ;it++)
		  
		  
		  {

			       double val = 0.0;
			       	if(*it != nullptr) 
			 		 	  {
						  std::vector<double> * vec =  *it;
						  if(vec && (print < vec->size()))
 		  	      val =  vec->at(print);
							}
		  	     	printf("%0.2f," ,val);
			     }
		
		printf("\n");
		
	}
	
	
	return 0;
}

