/***************************************************************************
 * tabbend.h: implementation of TabBend
 *
 * This file is part of KGuitar, a KDE tabulature editor
 *
 * copyright (C) 2003-2006 the KTabEdit development team <mrgwen@gmail.com>
 ***************************************************************************/

/***************************************************************************
 * This program 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.
 *
 * See the file COPYING for more information.
 ***************************************************************************/
 
#include "tabbend.h"
#include "util/assert.h"

#include <QString>
#include <QtDebug>

TabBend::TabBend()
: mType(NoneBend),
  mValue(0),
  mPoints()
{
}

TabBend::~TabBend()
{
	for(iterator it = mPoints.begin(); it != mPoints.end(); ++it)
		delete *it;
}

void TabBend::clone(const TabBend* bend)
{
	PRECONDITION( bend != 0x0 );
	
	mType = bend->mType;
	mValue = bend->mValue;
	
	for(const_iterator it = bend->mPoints.begin(); it != bend->mPoints.end(); ++it) {
		TabBendPoint* point = new TabBendPoint();
		point->clone(*it);
		
		mPoints.push_back(point);
	}
}

void TabBend::setBendType(TabBend::BendType b)
{
	mType = b;
}

TabBend::BendType TabBend::bendType() const
{
	return mType;
}

void TabBend::setBendValue(int i)
{
	mValue = i;
}

int TabBend::bendValue() const
{
	return mValue;
}

QString TabBend::bendValueToString() const
{
	switch (mValue) {
	case 25:
		return "1/4";
	case 50:
		return "1/2";
	case 75:
		return "3/4";
	case 100:
		if (mType < Dip)
			return "full";
		else
			return "1";
	case 125:
		return "1 1/4";
	case 150:
		return "1 1/2";
	case 175:
		return "1 3/4";
	case 200:
		return "2";
	case 225:
		return "2 1/4";
	case 250:
		return "2 1/2";
	case 275:
		return "2 3/4";
	case 300:
		return "3";
	case 350:
		return "3 1/2";
	case 400:
		return "4";
	case 450:
		return "4 1/2";
	case 500:
		return "5";
	case 550:
		return "5 1/2";
	case 600:
		return "6";
	}
	
	qDebug() << "BendValueEffect = " << mValue << "\n";
	return "unknow bend value";
}

int TabBend::sizeOfBendPoints() const
{
	return mPoints.size();
}

void TabBend::addBendPoint(TabBendPoint* point)
{
	PRECONDITION( point != 0x0 );
	
	mPoints.push_back(point);
}

void TabBend::setBendPoint(uint i, TabBendPoint* point)
{
	PRECONDITION( static_cast<long>(i) < sizeOfBendPoints() );
	PRECONDITION( point != 0x0 );
	
	if (mPoints[i] != 0x0)
		delete mPoints[i];
	
	mPoints[i] = point;
}

void TabBend::removeBendPoint(uint i)
{
	PRECONDITION( static_cast<long>(i) < sizeOfBendPoints() );
	
	if (mPoints[i] != 0x0)
		delete mPoints[i];
	
	mPoints.removeAt(i);
}

TabBendPoint* TabBend::bendPoint(uint i) const
{
	PRECONDITION( static_cast<long>(i) < sizeOfBendPoints() );
	
	TabBendPoint* result = mPoints[i];
	
	POSTCONDITION( result != 0x0 );
	
	return result;
}

bool TabBend::operator==(const TabBend &bend) const
{
	return (this->mType == bend.mType && this->mValue == bend.mValue && this->mPoints == bend.mPoints);
}

bool TabBend::operator!=(const TabBend &bend) const
{
	return !(*this == bend);
}
