// ************* TO DO  - fix problem with scrolling ************


#ifndef GLOBAL_DECL
#include "global.h"     // Global data types and variables
#endif

#ifndef PLAYLIST_DECL
#include "player.h"
#endif

#ifndef PLAYLIST_DECL
#include "playlist.h"
#endif

playlist::playlist()
{
  int n;

  intListCurrent = 0;
  intListCount = 0;
  intLine = 0;
  for (n=1;n<300;n++)
    play_list[n][0] = '\0';
}

int playlist::next(player *objPlayer, int key_pressed)
{
  char strPath[300];
  char strFname[300];

  // If user pressed the key to go to the next song, key_pressed = 1, otherwise it = 0
  if (intListCurrent == intListCount-1)  // At the end of the playlist
    return(0);
  else
  {
    intListCurrent++;
    if (objPlayer -> is_playing == 1)
    {
      if (key_pressed == 1)
      {
        // Stop the currently playing song
        objPlayer -> intStopPressed = 1;
        objPlayer -> stop();
      }
      // Start the next song
      strcpy(strPath, path());
      strcpy(strFname, current());
      objPlayer -> play(strPath, strFname);
    }
    return(1);
  }
}

int playlist::previous(player *objPlayer, int key_pressed)
{
  char strPath[300];
  char strFname[300];

  // If user pressed the key to go to the previous song, key_pressed = 1, otherwise it = 0
  if (intListCurrent == 0)  // At the beginning of the playlist
    return(0);
  else
  {
    intListCurrent--;
    if (objPlayer -> is_playing == 1)
    {
      if (key_pressed == 1)
      {
        // Stop the currently playing song
        objPlayer -> intStopPressed = 1;
        objPlayer -> stop();
      }
      // Start the new song
      strcpy(strPath, path());
      strcpy(strFname, current());
      objPlayer -> play(strPath, strFname);
    }
    return(1);
  }
}

int playlist::up(void)
{
  if (intLine > 0)
    intLine--;
  return(0);
}

int playlist::down(void)
{
  if (intLine < intListCount-1)
    intLine++;
  return(0);
}

int playlist::select_current(void)
{
  intLine = intListCurrent;
  return(0);
}

int playlist::display(int intScrollPos)
{
  char strTemp[500];

  if (intLine > intListCount-1)
    intLine = intListCount;

  send_text("Playlist:", 1, 0);
  strTemp[0] = '\0';
  if (intLine == intListCurrent)
    strcpy(strTemp, ">");
  strcat(strTemp, play_list[intLine]);
  strTemp[strlen(strTemp) - 4] = '\0';
  send_text(strTemp, 2, intScrollPos);
  return(0);
}

char* playlist::current(void)
{
  return play_list[intListCurrent];
}

char* playlist::current_viewing(void)
{
  return play_list[intLine];
}

char* playlist::path(void)
{
  return list_paths[intListCurrent];
}

int playlist::clear(void)
{
  intListCurrent = 0;
  intListCount = 0;
  return(0);
}

int playlist::add(char* strPath, char* strFname)
{
  strcpy(list_paths[intListCount], strPath);
  strcpy(play_list[intListCount], strFname);
  intListCount++;
  return(0);
}


syntax highlighted by Code2HTML, v. 0.9.1