//------------------------------------------------------------------------------

#include "stdafx.h"
#include "megatune.h"
#include "repository.h"
#include "msDatabase.h"

extern msDatabase mdb;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//------------------------------------------------------------------------------

repository::repository()
{
   mtDV   =     1.0;

   lofRPM =     0.0;
   hifRPM = 10000.0;
   rdfRPM =  6700.0;
   lofMAP =     0.0;
   hifMAP =   100.0;
   rdfMAP =     0.0;
   lofMAT =   -40.0;
   hifMAT =   215.0;
   rdfMAT =   200.0;
   lofTP  =     0.0;
   hifTP  =   100.0;
   rdfTP  =     0.0;
   lofPW  =     0.0;
   hifPW  =   100.0;
   rdfPW  =     0.0;
   lofDC  =     0.0;
   hifDC  =   100.0;
   rdfDC  =     0.0;

   lorEGO =     0.0;
   hirEGO =     1.0;
   lorCT  =   -40.0;
   hirCT  =   215.0;
   lorBAT =     6.0;
   hirBAT =    15.0;
   lorTR  =     0.0;
   hirTR  =   100.0;
   lorGE  =     0.0;
   hirGE  =   100.0;
   lorMAP =     0.0;
   hirMAP =   250.0;
   lorMAT =   -40.0;
   hirMAT =   215.0;
   lorRPM =     0.0;
   hirRPM = 10000.0;
   lorPW  =     0.0;
   hirPW  =    25.5;
   lorDC  =     0.0;
   hirDC  =   100.0;
   lorEGC =     0.0;
   hirEGC =   200.0;
   lorBC  =     0.0;
   hirBC  =   200.0;
   lorWC  =     0.0;
   hirWC  =   200.0;
   lorADC =     0.0;
   hirADC =   200.0;
   lorVE  =     0.0;
   hirVE  =   200.0;
   lorACC =     0.0;
   hirACC =   200.0;

   lotRPM =     0.0;
   hitRPM = 10000.0;
   rdtRPM =  6700.0;
   lotMAP =     0.0;
   hitMAP =   250.0;
   rdtMAP =   200.0;
   lotTR  =     0.0;
   hitTR  =   100.0;
   rdtTR  =     0.0;
   lotEGO =     0.0;
   hitEGO =     1.0;
   rdtEGO =     0.5;
   lotWU  =     0.0;
   hitWU  =   200.0;
   rdtWU  =   100.0;
   lotVEG =     0.0;
   hitVEG =   120.0;
   rdtVEG =     0.0;
   lotACE =     0.0;
   hitACE =   200.0;
   rdtACE =     0.0;
   lotVEB =     0.0;
   hitVEB =   120.0;
   
   g_alertColor      = RGB(255,   0,   0); // "Redline" part of gauge scale
   g_backgroundColor = RGB(202, 202, 202);
   g_ledColor        = RGB(  0, 255,   0); // Normal LEDs on bar meter
   g_ledAlertColor   = RGB(255,   0,   0); // "Redline" part of bar meter
   g_needleColor     = RGB(155,   0,   0);
   g_rangeColor      = RGB(  0,   0,   0); // Numbers at either end of scale
   g_scaleColor      = RGB( 96,  96,  96); // Normal part of the scale
   g_titleColor      = RGB( 80,  80,  80); // Text title at top of gauge
   g_valueColor      = RGB(  0,   0,   0); // Current value text and units

   t_backgroundColor = RGB(  0,   0,   0);
   t_cursorColor     = RGB(255,   0,   0);
   t_gridColor       = RGB(200, 200, 200);
   t_spotColor       = RGB(  0, 255,   0);
   t_textColor       = RGB(255, 255, 255);
   t_xRot            = 250.0;
   t_yRot            =   0.0;
   t_zRot            = 340.0;

   readInit();
}

repository::~repository()
{
}

//------------------------------------------------------------------------------

#define strEq(s) (strncmp(s,lineBuffer,strlen(s)) == 0)

static void getNums(char *s, double *nums)
{
   int nNums = 0;
   s = strchr(s, '=');
   do {
      s++;
      nums[nNums++] = strtod(s, &s);
      while (isspace(*s)) s++;
   } while (*s == ',');
   while (nNums < 3) nums[nNums++] = 0.0;
}

FILE *cfgOpen(const char *fileName, const char *mode);

void repository::readInit()
{
   FILE *iniFile = cfgOpen("megatune.ini", "r");
   if (iniFile) {
      char lineBuffer[512];
      int  block = 0;
      double d[3];
      while (fgets(lineBuffer, sizeof(lineBuffer)-1, iniFile)) {
         char *s = lineBuffer;
         if (strchr(s, ';')) *strchr(s, ';') = 0;
         if (strEq("[Units]"      )) { block = 1; continue; }
         if (strEq("[FrontPage]"  )) { block = 2; continue; }
         if (strEq("[RunTime]"    )) { block = 3; continue; }
         if (strEq("[Tuning]"     )) { block = 4; continue; }
         if (strEq("[MegaTune]"   )) { block = 5; continue; }
         if (strEq("[GaugeColors]")) { block = 6; continue; }

         if (strchr(lineBuffer, '=')) getNums(lineBuffer, d);

         switch (block) {
            case 1:
               if (strEq("temperature")) {
                  s = strchr(s, '=');
                  if (s && s[1] == 'C') mdb.therm = msDatabase::Celsius;
                  if (s && s[1] == 'F') mdb.therm = msDatabase::Fahrenheit;
               }
               break;
            case 2:
               if (strEq("rpmGauge"       )) { frontRPM(d); } //=0,8000,6700
               if (strEq("mapGauge"       )) { frontMAP(d); } //=0,255,200
               if (strEq("matGauge"       )) { frontMAT(d); } //=-40,215,200
               if (strEq("throttleGauge"  )) { frontTP (d); } //=0,100,100
               if (strEq("pulseWidthGauge")) { frontPW (d); } //=0,25.5,25.5
               if (strEq("dutyCycleGauge" )) { frontDC (d); } //=0,100,85
               break;
            case 3:
               if (strEq("egoBar"         )) { runEGO(d); } //=0,1.0
               if (strEq("coolantBar"     )) { runCT (d); } //=-40,215
               if (strEq("batteryBar"     )) { runBAT(d); } //=6,15
               if (strEq("throttleBar"    )) { runTR (d); } //=0,100
               if (strEq("gammaEBar"      )) { runGE (d); } //=0,100
               if (strEq("mapBar"         )) { runMAP(d); } //=0,255
               if (strEq("matBar"         )) { runMAT(d); } //=-40,215
               if (strEq("rpmBar"         )) { runRPM(d); } //=0,8000
               if (strEq("pulseWidthBar"  )) { runPW (d); } //=0,25.5
               if (strEq("dutyCycleBar"   )) { runDC (d); } //=0,100
               if (strEq("egoCorrBar"     )) { runEGC(d); } //=0,200
               if (strEq("baroCorrBar"    )) { runBC (d); } //=0,200
               if (strEq("warmupCorrBar"  )) { runWC (d); } //=0,200
               if (strEq("airdenCorrBar"  )) { runADC(d); } //=0,200
               if (strEq("veCorrBar"      )) { runVE (d); } //=0,200
               if (strEq("accCorrBar"     )) { runACC(d); } //=0,200
               break;
            case 4:
               if (strEq("rpmGauge"       )) { tuneRPM(d); } //=0,8000,6700
               if (strEq("mapGauge"       )) { tuneMAP(d); } //=0,255,200
               if (strEq("throttleGauge"  )) { tuneTR (d); } //=0,100
               if (strEq("egoGauge"       )) { tuneEGO(d); } //=0,1.0,0.5
               if (strEq("warmupGauge"    )) { tuneWU (d); } //=0,200,100
               if (strEq("veGauge"        )) { tuneVEG(d); } //=0,120
               if (strEq("acceGauge"      )) { tuneACE(d); } //=0,200
               if (strEq("veBar"          )) { tuneVEB(d); } //=0,120
               if (strEq("backgroundColor")) { t_backgroundColor = RGB(d[0],d[1],d[2]); }
               if (strEq("cursorColor"    )) { t_cursorColor     = RGB(d[0],d[1],d[2]); }
               if (strEq("gridColor"      )) { t_gridColor       = RGB(d[0],d[1],d[2]); }
               if (strEq("spotColor"      )) { t_spotColor       = RGB(d[0],d[1],d[2]); }
               if (strEq("textColor"      )) { t_textColor       = RGB(d[0],d[1],d[2]); }
               if (strEq("gridOrient"     )) { t_xRot = d[0]; t_yRot = d[1]; t_zRot = d[2]; }
               break;
            case 5:
               if (strEq("defaultVersion")) { defaultVersion(d); mdb.getConst(); }
               break;
            case 6:
               if (strEq("alertColor"     )) { g_alertColor      = RGB(d[0],d[1],d[2]); }
               if (strEq("backgroundColor")) { g_backgroundColor = RGB(d[0],d[1],d[2]); }
               if (strEq("ledColor"       )) { g_ledColor        = RGB(d[0],d[1],d[2]); }
               if (strEq("ledAlertColor"  )) { g_ledAlertColor   = RGB(d[0],d[1],d[2]); }
               if (strEq("needleColor"    )) { g_needleColor     = RGB(d[0],d[1],d[2]); }
               if (strEq("rangeColor"     )) { g_rangeColor      = RGB(d[0],d[1],d[2]); }
               if (strEq("scaleColor"     )) { g_scaleColor      = RGB(d[0],d[1],d[2]); }
               if (strEq("titleColor"     )) { g_titleColor      = RGB(d[0],d[1],d[2]); }
               if (strEq("valueColor"     )) { g_valueColor      = RGB(d[0],d[1],d[2]); }
               break;
         }
      }
      fclose(iniFile);
   }
}

//------------------------------------------------------------------------------

repository rep; // One and only one.

//------------------------------------------------------------------------------

