/*

file filter and accumulator test

             CREATOR: DAn williams
                DATE:
         DESCRIPTION: test the viewport class
            FILENAME: fileaccumtest.cpp
             STARTED: jan 22
    OPERATING SYSTEM: Linux
1st VERSION FINISHED:

  Apr 15 / 2003  update adding propfilter. dan.

*/


#include <stdio.h>
#include <stdlib.h>

void Error( char * desc );

#include "accumulator2.h" 
#include "filefilter.h"
#include "displayfilter.h"
#include "propaccumulator.h"
#include "propfilter.h"
#include "vif.h" 

#include <conio.h>

//global variables

#define QUIT  0
#define IDLE  1
#define DOWN  2
#define LEFT  4
#define RIGHT 6
#define UP    8

int main(int argc, char** argv)
{
  
  VIF             device;  
  DisplayFilter1  DFilter;
  Accumulator     Cbuffer; // Content Buffer
  FileFilter1     FFilter;
  PropAccum       Tbuffer; // Tag buffer
  PropFilter      PFilter;
  Navigator       scout;
  
  
  int      command = IDLE;
  int      character;

  
 // printf("VIF is %d X %d\n", device.Width, device.Height);
// printf("setting Tag source\n");  
  PFilter.SetSource( &Tbuffer );  
 // printf("setting display\n");
  DFilter.SetDisplay( &device );
 // printf("setting source\n");
  DFilter.SetSource( &Cbuffer );
 // printf("setting tag source to display\n");
  DFilter.SetPropFilter( &PFilter );
 // printf("loading file\n");
  FFilter.LoadFile( &Cbuffer, "test.txt" );
  
  scout.SetAccumulator( &Cbuffer );

 // printf("waiting for command\n");

 // long int X  = 0, Y  = 0;  // screen scroll locations
 Point2D_t   ScreenPos;
 // long int OX = X, OY = Y;
 Point2D_t   OldScreenPos;
 
 OldScreenPos.X = ScreenPos.X = 0;
 OldScreenPos.X = ScreenPos.Y = 0;
 
 // ChrOffset ScreenPos, OldScreenPos;
  ChrOffset CursorPos, OldCursorPos;
  ChrOffset TempPos;
  
 // ScreenPos    = 0;
 // OldScreenPos = ScreenPos;
  CursorPos    = 0;
  OldCursorPos = CursorPos;
  
 // int CursorX = 1, CursorY = 1;  // cursor location
 
  // reposition the cursor
  //device.CursorPos(CursorX, CursorY);  //!!!???!!!
  
 // printf("setting scroll\n");
 // DFilter.Scroll( ScreenPos );
  DFilter.SetScroll( ScreenPos.X, ScreenPos.Y );
 // printf("setting cursor Position\n");
  Tbuffer.set( 0, 0, CursorPos, 0 ); // index:owner 0:0, pos = 0, type = 0
 // printf("redrawing screen\n");
  DFilter.ReDraw();
  
  
  while(command) {
    
    command = IDLE;
    character = wgetch(conio_scr);
        
    if (character == 'q') {
       command = QUIT;
       
    }else if (character == 259){ // up
	   
      
	//	if (CursorY > 0) {
	//	 CursorY--;
	//	} else {
   //    command = UP;
   //    Y = MAX(0, Y - 1);
	//	} 
   
     TempPos   = scout.LinePos(CursorPos) - 1;  // get the cur line pos
      CursorPos = scout.PrevLine(CursorPos);     // go to prev line
      if (scout.LineEnd(CursorPos) > (CursorPos + TempPos)){ //go to pos if poss.
        CursorPos += TempPos;
      } else {
        CursorPos = scout.LineEnd(CursorPos);   // else go to end of line
      }
      
	 }else if (character == 258){ // down
	 
	 //  if (CursorY < device.Height) {
	//	 CursorY++;
	//	} else {
    //   command = DOWN;
    //   Y = MIN(1024, Y + 1);
	//	}	
       
      TempPos   = scout.LinePos(CursorPos) - 1;  // get the cur line pos
      CursorPos = scout.NextLine(CursorPos);     // go to next line
      if (scout.LineEnd(CursorPos) > (CursorPos + TempPos)){ //go to pos if poss.
        CursorPos += TempPos;
      } else {
        CursorPos = scout.LineEnd(CursorPos);   // else go to end of line
      }
      
      // check what line we are on
      // if were off the screen, scroll up
        // * the problem with this is knowing where teh screen ends
        // * aside from that we can scroll up by moving the screen_start
        // pointer 1 line forward.
        // * but we have to dispense with the concept of knowing our 
        // ultimate y position
      
      
       
    }else if (character == 260){ // left
			 
      if (CursorPos > 0) {
		  CursorPos--;
		}	
       
    }else if (character == 261){ // right
		  CursorPos++;     
    }
	 
    //do off-the-screen scrolling
    if ((scout.LinePos(CursorPos) + 1) > (ScreenPos.X + device.Width)) { // scroll right 
          ScreenPos.X = scout.LinePos(CursorPos) - device.Width + 1;
    }
    if ((scout.LinePos(CursorPos))  < ScreenPos.X + 1) {  // scroll left
          ScreenPos.X = scout.LinePos(CursorPos) - 1;
    }
	 /*
    if ((scout.
    
    */
    
    // check for changes and update as nesc.
    
   if ((OldScreenPos.X != ScreenPos.X) |
       (OldScreenPos.Y != ScreenPos.Y) |
       (OldCursorPos != CursorPos)) { 
       
      DFilter.SetScroll ( ScreenPos.X, ScreenPos.Y );
     // reposition the cursor
     // Cursor type should be 2 for start, 3 for end 
     Tbuffer.set( 0, 0, CursorPos, 0 ); // index:owner 0:0, pos = 0, type = 0  
     DFilter.ReDraw();    
     
    // OldScreenPos = ScreenPos;
     OldCursorPos = CursorPos;
     OldScreenPos.X = ScreenPos.X;
     OldScreenPos.Y = ScreenPos.Y;
   }
	// reposition the cursor
	//device.CursorPos(CursorX, CursorY); //!!!???!!!
  }

  return ( 0 );
}

//subroutines


void Error( char * desc ){
  printf ("%s\n", desc);
  exit ( 0 );
}

