
// 1. Stations Filename
// 2. Select Beginning station
// 3. Count trajectory & time
// 4. Exit

#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"


#include "ItemBox.c"
#include "Stations.c"
#include "ConMatrix.c"
#include "Traj.c"

//-----------------------------------------------------------
 int StatSel = -1;
 unsigned int DataTime = 0;
 char IsLoadedStat = 0;
 char *filename = "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";

static struct Stations *stat;
static CMatrix m, sm;


//-----------------------------------------------------------

//-----------------------------------------------------------
void FreeAllMem(){
if (IsLoadedStat){
 DestCMatrix(&m, stat->c);
 DestCMatrix(&sm, stat->c);
 DestStations(stat);
 }
}

 int TestFile(char *s){
  FILE *f;
   if ((f = fopen(s,"rt")) == NULL) return -1;
   fclose(f);
  return 1;
 }


// ------------- STATIONS LOAD ---------------
void LoadStat(){
 int res1 = -1;

 if (TestFile(filename)== -1) {

 IsLoadedStat = 0;
 FreeAllMem();


   while (res1==-1){

    printf("Enter a filename: ");
    scanf("%s", filename);
    res1 = TestFile(filename);
   if (res1 == -1)
    switch (iItemBox(2, "Cannot open station file.\n" ,"Retry?", "No, exit.")) {
     case  0 :  break;
     case -1 :
     case  1 :  return -1;
    }
   }
 } // end if TestFile

 // ------------- STATIONS READ ------------------
  stat = InitStations();
  ReadStations(filename, stat);
 // ------------- COMP MATRIX READ ---------------
  InitCMatrix(&m, stat -> c);
 ReadTimeComponents(filename, stat, m);
  InitCMatrix(&sm, stat -> c);
 ReadConComponents(filename, stat, sm);

 IsLoadedStat = 1;
 return 0;
}


//-----------------------------------------------------------
int GetInt(){
 char s[256], c;
 int i = 0;
 printf("Get approximatly time(only numeric): ");

  while ((c=getch()) != 13)
   if (c>='0'  &&  c<='9') {
    s[i] = c;
    i++;
     printf("%c",c);
   }
    s[i] = '\0';
  return atoi(&s);
}



//-----------------------------------------------------------
void CountTraj(){
iClearScreen;
if (!(IsLoadedStat && DataTime!=0 && StatSel != -1)){
 printf("\n Not all parameters are taken! \n");
  printf(" Trajectory file loaded ... %s\n", (IsLoadedStat)? "done" : "! no");
  printf(" Begin station selected ... %s\n", (StatSel!=-1)? "done" : "! no");
  printf(" Time is taken ............ %s\n", (DataTime!=0)? "done" : "! no");
 printf("\n ... press any key to repair parameters ...");
 getch();
 }   else
 {
  iClearScreen();
  PrintCMatrix(sm, stat->c);
   printf("\n");

    BaseFunc(stat, StatSel, DataTime, m, sm);

  PrintCMatrix(sm, stat->c);
   printf("\n");

  PrintTraj(&cur_traj, stat); 

  getch();
 }
}


//-----------------------------------------------------------
int main(int argc, char* argv[])
{
 int uc;

    LoadStat();
    DataTime = 30;
    StatSel = 1;

//    iClearScreen();

 while
  ( (uc = iItemBox(4,"30.11.2006\n\n","1. Load stations from file","2. Select the beginning point","3. Get approximate time for trajectory", "4. Count trajectory & time")) !=-1 )
    switch (uc) {
     case 0 : LoadStat(); break;
     case 1 : if (IsLoadedStat) StatSel = iItemBoxMas(stat->c, stat->items ); else { printf("Trajectory is not loaded from file."); getch();}break;
     case 2 : DataTime = GetInt(); break; // get time
     case 3 : CountTraj(); LoadStat(); break; // get time
    }

 FreeAllMem();
return 0;
}
//---------------------------------------------------------------------------
