/***************************************************************************
 * songviewcommands.cpp: implementation of SetSongPropCommand class
 *                                         SetTrackPropCommand
 *                                         InsertTabsCommand
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2003-2006 the KTabEdit development team <mrgwen@gmail.com>
 * copyright (C) 2002-2003 the KGuitar 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 "songviewcommands.h"
#include "util/assert.h"

#include <QtDebug>

#include "tablature/tabbar.h"
#include "tablature/tabproperties.h"
#include "tablature/tabtimes.h"
#include "tablature/tabtrack.h"
#include "tablatureModel/tabsongmodel.h"

SongView::AddTrackCommand::AddTrackCommand(TabSongModel* tabSong, TabTrack* tabTrack)
: QUndoCommand(tr("Add track")),
  mTabSong(tabSong),
  mTabTrack(tabTrack)
{
	PRECONDITION( tabSong != 0x0 );
	PRECONDITION( tabTrack != 0x0 );
}

SongView::AddTrackCommand::~AddTrackCommand()
{
	delete mTabTrack;
}

void SongView::AddTrackCommand::undo()
{
	delete mTabSong->back();
	
	mTabSong->removeLast();
}

void SongView::AddTrackCommand::redo()
{
	TabTrack* track = new TabTrack();
	track->clone(mTabTrack);
	qDebug() << "nbString2 : " << track->nbStrings() << "\n";
	mTabSong->push_back(track);
}

SongView::DeleteTrackCommand::DeleteTrackCommand(TabSongModel* tabSong, TabTrack* tabTrack)
: QUndoCommand(tr("Delete track")),
  mTabSong(tabSong),
  mTabTrack(tabTrack),
  mEmpty(false),
  mExecuted(false)
{
	PRECONDITION( tabSong != 0x0 );
	PRECONDITION( tabTrack != 0x0 );
}

SongView::DeleteTrackCommand::~DeleteTrackCommand()
{
	if (mExecuted == true)
		delete mTabTrack;
}

void SongView::DeleteTrackCommand::undo()
{
	if (mEmpty == true) {
		delete mTabSong->front();
		mTabSong->removeFirst();
	}
	
	mTabSong->push_back(mTabTrack);
	
	mExecuted = false;
}

void SongView::DeleteTrackCommand::redo()
{
	mTabSong->removeAll(mTabTrack);
	
	if (mTabSong->size() == 0) {
		mEmpty = true;
		TabTrack* tabTrack = new TabTrack(FretTab, tr("Track 1"), 1, 0, 25, 6, 24);
		tabTrack->insertEmptyBar(0);
		
		mTabSong->push_front(tabTrack);
	}
	
	mExecuted = true;
}

SongView::SetTrackPropCommand::SetTrackPropCommand(TrackProperties* trackProperties, TabTrack* tabTrack)
: QUndoCommand(tr("Set track properties")),
  mTrackProperties(trackProperties),
  mSavedProperties(0x0),
  mTabTrack(tabTrack)
{
	PRECONDITION( trackProperties != 0x0 );
	PRECONDITION( tabTrack != 0x0 );
	
	mSavedProperties = new TrackProperties();
	mSavedProperties->clone(tabTrack->properties());
}

SongView::SetTrackPropCommand::~SetTrackPropCommand()
{
	delete mSavedProperties;
	delete mTrackProperties;
}

void SongView::SetTrackPropCommand::undo()
{
	TrackProperties* properties = new TrackProperties();
	properties->clone(mSavedProperties);
	mTabTrack->setProperties(properties);
}

void SongView::SetTrackPropCommand::redo()
{
	TrackProperties* properties = new TrackProperties();
	properties->clone(mTrackProperties);
	mTabTrack->setProperties(properties);
}

SongView::SetSongPropCommand::SetSongPropCommand(TabSongModel* tabSong, TabProperties* songProperties)
: QUndoCommand(tr("Set song properties")),
  mTabSong(tabSong),
  mSavedProperties(0x0),
  mProperties(songProperties)
{
	mSavedProperties = new TabProperties();
	mSavedProperties->clone(tabSong->properties());
}

SongView::SetSongPropCommand::~SetSongPropCommand()
{
	delete mSavedProperties;
	delete mProperties;
}

void SongView::SetSongPropCommand::undo()
{
	TabProperties* properties = new TabProperties();
	properties->clone(mSavedProperties);
	mTabSong->setProperties(properties);
}

void SongView::SetSongPropCommand::redo()
{
	TabProperties* properties = new TabProperties();
	properties->clone(mProperties);
	mTabSong->setProperties(properties);
}

SongView::InsertTabsCommand::InsertTabsCommand(TrackEditView& tv, TabTrack& trk, TabTrack& tabs, uint bar)
: QUndoCommand()/*,
  barPosition(bar),
  currentTrack(trk),
  tempTrack(tabs),
  oldTrack(trk),
  trackView(tv)*/
{
}

void SongView::InsertTabsCommand::execute()
{
	/*for (uint i = 0; i < tempTrack.size(); i++) {
		currentTrack.insertBar(barPosition + i, tempTrack.bar(i));
	}*/
	
// 	trackView.drawAllTrack();
}

void SongView::InsertTabsCommand::unexecute()
{
	//currentTrack = oldTrack;
	
// 	trackView.drawAllTrack();
}

QString SongView::InsertTabsCommand::name() const
{
	return tr("Insert from clipboard");
}
