#define __main__



#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"
#include "ncurses.h"
#include "curses.h"

#include "mod/ItemBox.h"
#include "mod/Stations.h"
#include "mod/ConMatrix.h"
#include "mod/Traj.h"

#include "mod/Globals.h"

//-----------------------------------------------------------
// #define getch getchar
#define EMPTY_FILE "\0"
//-----------------------------------------------------------
 int StatSel = -1;
 unsigned int DataTime = 0;
 char IsLoadedStat = 0;
 char filename[256] = {EMPTY_FILE};

static struct Stations *stat;
static CMatrix m, sm;

//-----------------------------------------------------------
struct Stations *InitStations();
void LoadStat(char old);


//-----------------------------------------------------------


//------------------------------------------------------------

void InitNCURSES(){
 initscr();
 cbreak();
 nonl();
 intrflush (stdscr, FALSE);
 keypad (stdscr, TRUE);
}
//------------------------------------------------------------


void FreeAllMem(){
if (IsLoadedStat){
 DestCMatrix(m, stat->c);
 DestCMatrix(sm, stat->c);
 DestStations(stat);
 }
};


//--------------- Test File ------------------
 int TestFile(char *s){
  FILE *f;
   if ((f = fopen(s,"rt")) == NULL) return -1;
   fclose(f);
  return 1;
 }


// ------------- STATIONS LOAD ------------------
// 1 - Reload old
// 0 - Enter a file name

void LoadStat(char old){
 int res1 = -1;

   FreeAllMem();
// --------- TEST FILE STATIONS -----------------
        if (!old) {
         StatSel = 0;
         IsLoadedStat = 0;
           while (res1==-1){
            printw("%s", "Enter a filename: ");
            scanw("%s", filename);
            res1 = TestFile(filename);
           }
        }
// ------------- STATIONS READ ------------------
  stat = InitStations();
   ReadStations(filename, stat);
// ------------- TIME MATRIX READ ---------------
  InitCMatrix(&m, stat -> c);
 ReadTimeComponents(filename, stat, m);
// ------------- COMP MATRIX READ ---------------
  InitCMatrix(&sm, stat -> c);
 ReadConComponents(filename, stat, sm);

 IsLoadedStat = 1;
}


//-----------------------------------------------------------
int GetInt(){
 char s[256], c;
 int i = 0;
 noecho();
  while ((c=getch()) != 13)
   if (c>='0'  &&  c<='9') {
    s[i] = c;
    i++;
     printw("%c",c);
   }
  s[i] = '\0';
 echo();
return atoi(s);
}



//-----------------------------------------------------------
void CountTraj(){

 int ma[3][3] = {0,0,1, 0,0,0, 0,0,0};

iClearScreen();



if (!(IsLoadedStat && DataTime!=0 && StatSel != -1)){
 printw("\n Not all parameters are taken! \n");
  printw(" Trajectory file loaded ... %s\n", (IsLoadedStat)? "done" : "! no");
  printw(" Begin station selected ... %s\n", (StatSel!=-1)? "done" : "! no");
  printw(" Time is taken ............ %s\n", (DataTime!=0)? "done" : "! no");
 printw("\n ... press any key to repair parameters ...");
 getch();
 }   else
 {
  iClearScreen();

   printw("Begin station: %s   ", stat->items[StatSel]);
   printw("End station: %s\n", EndStat);

  PrintCMatrix(sm, stat->c);
   printw("\n");

// PrintCMatrix(ma, 3);
// ma[2][0];

    BaseFunc(stat, StatSel, DataTime, m, sm);

// PrintCMatrix(sm, stat->c);
  printw("\n");

  FindTrajTime(&cur_traj,m);
  PrintTraj(&cur_traj, stat);

  getch();
 }
}


//-----------------------------------------------------------
int main(int argc, char* argv[])
{
 int uc;

   // LoadStat();
   DataTime = 30;

   strcpy(filename, EMPTY_FILE);
   
   InitNCURSES();
   iClearScreen();

/*   LoadStat(0);
   CountTraj();   */

//---------------------- MAIN CYCLE -----------------------
 while
   ( (uc = iItemBox(4,0,"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(0);  break;
     case 1 : if (IsLoadedStat) StatSel = iItemBoxMas(stat->c, StatSel, stat->items );
               else { printw("\n Trajectory is not loaded from file."); getch();}  break;
     case 2 : printw("Get approximatly time (only numeric): "); DataTime = GetInt();  break;
     case 3 : CountTraj(); LoadStat(1);  break;
    }       

//--------------- Destroy the application -----------------
 FreeAllMem();
 endwin();
return 0;
}
//---------------------------------------------------------------------------
