/*
 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
 *
 * This file is part of Composite
 *
 * Composite is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Composite is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "config.h"

#include <Tritium/Sample.hpp>
#include <Tritium/Song.hpp>
#include <Tritium/Instrument.hpp>
#include <Tritium/InstrumentLayer.hpp>
#include <Tritium/Logger.hpp>
using namespace Tritium;

#include "WaveDisplay.hpp"
#include "../Skin.hpp"


WaveDisplay::WaveDisplay(QWidget* pParent)
 : QWidget( pParent )
 , m_sSampleName( "" )
{
	setAttribute(Qt::WA_NoBackground);

	//DEBUGLOG( "INIT" );
	int w = 277;
	int h = 58;
	resize( w, h );

	bool ok = m_background.load( Skin::getImagePath() + "/waveDisplay/background.png" );
	if( ok == false ){
		ERRORLOG( "Error loading pixmap" );
	}

	m_pPeakData = new int[ w ];

}




WaveDisplay::~WaveDisplay()
{
	//DEBUGLOG( "DESTROY" );

	delete[] m_pPeakData;
}



void WaveDisplay::paintEvent(QPaintEvent *ev)
{
	QPainter painter( this );
	painter.setRenderHint( QPainter::Antialiasing );
	painter.drawPixmap( ev->rect(), m_background, ev->rect() );

	painter.setPen( QColor( 102, 150, 205 ) );
	int VCenter = height() / 2;
	for ( int x = 0; x < width(); x++ ) {
		painter.drawLine( x, VCenter, x, m_pPeakData[x] + VCenter );
		painter.drawLine( x, VCenter, x, -m_pPeakData[x] + VCenter );
	}

	QFont font;
	font.setWeight( 63 );
	painter.setFont( font );
	painter.setPen( QColor( 255 , 255, 255, 200 ) );
	painter.drawText( 0, 0, width(), 20, Qt::AlignCenter, m_sSampleName );
}



void WaveDisplay::updateDisplay( Tritium::InstrumentLayer *pLayer )
{
	if ( pLayer && pLayer->get_sample() ) {
		// Extract the filename from the complete path
		QString sName = pLayer->get_sample()->get_filename();
		int nPos = sName.lastIndexOf( "/" );
		m_sSampleName = sName.mid( nPos + 1, sName.length() );

//		DEBUGLOG( "[updateDisplay] sample: " + m_sSampleName  );

		int nSampleLength = pLayer->get_sample()->get_n_frames();
		float nScaleFactor = nSampleLength / width();

		float fGain = height() / 2.0 * pLayer->get_gain();

		float *pSampleData = pLayer->get_sample()->get_data_l();

		int nSamplePos =0;
		int nVal;
		for ( int i = 0; i < width(); ++i ){
			nVal = 0;
			for ( int j = 0; j < nScaleFactor; ++j ) {
				if ( j < nSampleLength ) {
					int newVal = (int)( pSampleData[ nSamplePos ] * fGain );
					if ( newVal > nVal ) {
						nVal = newVal;
					}
				}
				++nSamplePos;
			}
			m_pPeakData[ i ] = nVal;
		}
	}
	else {
		m_sSampleName = "-";
		for ( int i =0; i < width(); ++i ){
			m_pPeakData[ i ] = 0;
		}
	}

	update();
}

