// CWAVInfo.h (c) Andreas Andersson
#ifndef _CWAVINFO_H_
#define _CWAVINFO_H_

#include <windows.h>
#include <fstream> // BASSFIX
#include <stdlib.h>
#include <stdio.h>
#include <objbase.h>
#include "fileinfo.h"

class CWAVInfo : public CFileInfo
{
public:
    
    // function to load a file into this structure
    // the argument passed is the path to a WAV file
    int   loadInfo( const char* srcWAV );

	// Does the actual data loading, called by loadInfo
	int	  loadInfoFromHandle(ifstream* ifile);

    int   getBitrate() {return 0;}
    
    // functions to calculate the length of the song
    int   getFileSize() { return m_FileSize; };
    void  getFileName(char* input) { lstrcpy(input, m_FileName); };

	int   getLengthInSeconds() {return m_LengthInSeconds;}
    void  getFormattedLength(char* input);
 
    // This information is not available in wav files:
    void  getTitle(char* input)		{input[0] = 0;}
    void  getArtist(char* input)	{input[0] = 0;}
    void  getAlbum(char* input)		{input[0] = 0;}
    void  getComment(char* input)	{input[0] = 0;}
    void  getGenre(char* input)		{input[0] = 0;}

    int   getYear()					{return 0;}
    int   getTrackNumber()			{return 0;}

private:

    // the file information can not be found elsewhere
    char m_FileName[260];
    int m_FileSize;
	int m_LengthInSeconds;
};

int CWAVInfo::loadInfo(const char *scrWAV)
{
    //return value, in case loadInfoFromHandle generates an error
	int ret = 0;
	
	// open input-file stream to the specified file, name
    ifstream* ifile = new ifstream(scrWAV, ios::in | ios::binary /* BASSFIX | ios::nocreate*/);
    
    if (ifile->is_open())	 // if the file was opened correctly
	{
        // get file size, by setting the pointer in the end and tell the position
        ifile->seekg(0,ios::end);
        m_FileSize = ifile->tellg();

        // get scrFile into fileName variable
        strcpy(m_FileName, scrWAV);

		//Call virtual function specific to format
		ret = loadInfoFromHandle(ifile);
	}
	else
	{
		ifile->close();
        delete ifile;
        return 1;
    }

	ifile->close();
    delete ifile;
    return ret;
}

int	CWAVInfo::loadInfoFromHandle(ifstream* ifile)
{
	BYTE m_ChunkRIFF[12];
	BYTE m_ChunkFORMAT[24];

	ifile->seekg(0);
	ifile->read((char *)m_ChunkRIFF, 12); // BASSFIX
	ifile->seekg(12);
	ifile->read((char *)m_ChunkFORMAT, 24); // BASSFIX

	if (!memcmp(&m_ChunkRIFF[0], "RIFF", 4) &&
		!memcmp(&m_ChunkRIFF[8], "WAVE", 4) &&
		!memcmp(&m_ChunkFORMAT[0], "fmt ", 4))
	{
		int BytesPerSecond = 0;

		memcpy(&BytesPerSecond, &m_ChunkFORMAT[16], 4);

		//Length of file minus header size
		m_LengthInSeconds = (m_FileSize - 36) / BytesPerSecond;

		return 0;
	}
	else
	{
		return 1;
	}
}

void CWAVInfo::getFormattedLength(char* input)
{
    //  s  = complete number of seconds
    int s  = getLengthInSeconds();

    //  ss = seconds to display
    int ss = s%60;

    //  m  = complete number of minutes
    int m  = (s-ss)/60;

    //  mm = minutes to display
    int mm = m%60;

    //  h  = complete number of hours
    int h = (m-mm)/60;

    char szTime[16]; // temporary string

    // make a "hh:mm:ss" if there is any hours, otherwise
    // make it   "mm:ss"
    if (h>0) sprintf(szTime,"%02d:%02d:%02d", h,mm,ss);
    else     sprintf(szTime,     "%02d:%02d",   mm,ss);

    // copy to the inputstring
    strcpy(input, szTime);
}

#endif