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

int volume;
bool stopped;
bool loop;

/* local */
void display_menu( void );

void display_menu( void )
{
     printf( "\n\nMenu:\n----------\n" );
     printf( "q - Quit\n" );
     printf( "p - Pause\n" );
     printf( "u - Volume Up\n" );
     printf( "d - Volume Down\n" );
     printf( "a - Play again/Restart\n" );
     printf( "l - Toggle Looping\n" );
     printf( "\n\n" );
}
/* /local */

int main( void )
{
  bool done = FALSE;
  static unsigned int trigger = 0;
  static unsigned int count = 0;

  if ( init_dx() == FALSE )
  {
    printf( "\n\nCould not initialize DirectX! Press any key to quit...\n\n" );
    getche();
    return 0;
  }
  
//  set_volume( song, volume );
//  play_wave(song,TRUE);


  printf( "Playing song...\n\n" );
  display_menu();
  
  while ( done == FALSE )
  {
    if ( kbhit() == 0 )
    {
       count = GetTickCount();
       
       if ( count-trigger >= 2100 ) /* 2.1 seconds */
       {
         trigger = count; /* the new trigger needs to be 2100 greater */
         handle_dx(FALSE);
       }
    }
    else
    {
      char c = getche(); /* get the pressed key */
      
      switch ( tolower(c) )
      {
        case 'q': /* quit */
             done = TRUE;
             break;
             
        case 'm':
             display_menu();
             break;

        case 'p':
             if ( stopped == FALSE )
             {
               stop_wave(song);
               printf("\n\nSong now stopped.\n\n" );
             }
             else
             {
               play_wave(song, FALSE);
               printf( "\n\nSong now playing.\n\n");
             }
               
             display_menu();
             break;

        case 'a':
             stopped = FALSE;
             handle_dx(TRUE); /* start from beginning */
             printf( "\n\nPlaying song...\n\n\n" );
             display_menu();
             break;

        case 'l':
             if ( loop == FALSE )
              loop = TRUE;
             else
              loop = FALSE;
              
             printf( "\n\nLooping is now %s\n\n", (loop == FALSE ? "off" : "on" ) );
             break;

       case 'u':
             if ( stopped == FALSE )
             {
                 volume += 100;
                 set_volume( song, volume );
                 printf( "\n\nVolume now set to %d\n\n", (volume+10000) );
             }
             else
              printf( "\n\nThe song is stopped!\n\n" );
             break;

       case 'd':
             if ( stopped == FALSE )
             {
                 volume -= 100;
                 set_volume( song, volume );
                 printf( "\n\nVolume now set to %d\n\n", (volume+10000) );
             }
             else
              printf( "\n\nThe song is stopped!\n\n" );
             break;
      }
      
    }
    
  }


  delete_wave(song); /* kill the current buffer */
  ds->lpVtbl->Release(ds); /* get rid of the DS interface */
  mmioClose( wave, 0 ); /* close handle to open wave */

  printf( "\n\n\n\nThanks for using Odis\' streaming audio v1.0!\n\n" );
  getche();
  
  return 0;
}