/**********************************************************************************************
    Copyright (C) 2009 Oliver Eichler oliver.eichler@gmx.de

    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.

    This program 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

**********************************************************************************************/

#include "CDlgTrackEdit.h"
#include "CTrack.h"
#include "CTrackDB.h"
#include "GeoMath.h"
#include "CCanvas.h"
#include "CDlgMenu.h"

#include <proj_api.h>

#include <QtGui>

CDlgTrackEdit::CDlgTrackEdit(CTrack& trkpt, QWidget * parent)
: QDialog(parent)
, trkpt(trkpt)
{
    setupUi(this);
    setupKeys(tabWidget);
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Dialog);
    setAttribute(Qt::WA_DeleteOnClose, false);
    showFullScreen();

    db = QSqlDatabase::database("qlandkarte");
    if (trkpt.dbKey != "") {
        QString errmsg;
        if(!CTrackDB::self().getTrackInfo(trkpt.dbKey, trkpt, errmsg)){
            QMessageBox::critical(this, tr("Error..."), tr("Database error: %1").arg(errmsg), QMessageBox::Abort, QMessageBox::Abort);
            return;
        }
    }

    pushKey1->setText(tr("ok"));
    connect(pushKey1, SIGNAL(clicked()), this, SLOT(slotKey1()));
    pushKey2->setText(tr("cancel"));
    connect(pushKey2, SIGNAL(clicked()), this, SLOT(slotKey2()));
    pushKey3->setText(tr("delete"));
    connect(pushKey3, SIGNAL(clicked()), this, SLOT(slotKey3()));
    pushKey4->setText(tr("show"));
    connect(pushKey4, SIGNAL(clicked()), this, SLOT(slotKey4()));

#ifdef WINCE

#else
    spacerKeyboard->changeSize(20,0,QSizePolicy::Fixed,QSizePolicy::Fixed);
#endif

    lineName->setText(trkpt.name);

    labelTimestamp->setText(QDateTime::fromTime_t(trkpt.timestamp).toString());

    textComment->setText(trkpt.comment);

    lineDistance->setText(trkpt.distance );

    QPixmap icon(16,8);
    for(int i=0; i < 17; ++i) {
        icon.fill(CTrack::lineColors[i]);
        comboColor->addItem(icon,"",QVariant(i));
    }
    comboColor->setCurrentIndex(trkpt.colorIdx);
}

CDlgTrackEdit::~CDlgTrackEdit()
{

}

void CDlgTrackEdit::slotKey1()
{
    trkpt.name = lineName->text();
    trkpt.comment = textComment->toPlainText();

    trkpt.colorIdx = comboColor->currentIndex();

    // Update information if exist
    QString errmsg;
    if(!CTrackDB::self().updateTrackInfo(trkpt, errmsg)){
        QMessageBox::critical(this, tr("Error..."), tr("Database error: %1").arg(errmsg), QMessageBox::Abort, QMessageBox::Abort);
        return;
    }

    done(0);
}


void CDlgTrackEdit::slotKey2()
{

    done(-1);
}


void CDlgTrackEdit::slotKey3()
{
    QMessageBox::StandardButton res = QMessageBox::question(this, tr("Delete track..."), tr("Really delete: %1").arg(trkpt.name), QMessageBox::Ok|QMessageBox::Cancel, QMessageBox::Ok);
    if(res != QMessageBox::Ok){
        return;
    }

    QString errmsg;
    if(!CTrackDB::self().del(trkpt.trackid, errmsg)){
        QMessageBox::critical(this, tr("Error..."), tr("Database error: %1").arg(errmsg), QMessageBox::Abort, QMessageBox::Abort);
        return;
    }
    done(0);
}


void CDlgTrackEdit::slotKey4()
{
    slotKey1();

    QString errmsg;
    if(!CTrackDB::self().show(trkpt.dbKey, errmsg)){
        QMessageBox::critical(this, tr("Error..."), tr("Database error: %1").arg(errmsg), QMessageBox::Abort, QMessageBox::Abort);
        return;
    }


    CDlgMenu * main = CDlgMenu::self();
    if(main){
        main->close();
    }
}

