/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtWidgets>
#include "mainwindow.h"
#include "subwindow.h"
#include "stable.h"
#include "sdictionary.h"
#include "tablealgorithm_interface.h"
#include "yalgorithm.h"
#include "ymbasicwidget.h"
#include "ymonedepthwidget.h"
#include "ymnxnmatrixwidget.h"
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlError>
#include "ymtwodepthwidget.h"
#include "ymplatformwidget.h"
#include "ymtwodepthawidget.h"
#include "ymcenterkeywordwidget.h"
#include <QShortcut>
#include <QKeySequence>
#include <QUrl>
//! [0]
SubWindow::SubWindow()
{
    QWidget *widget = new QWidget;
    setCentralWidget(widget);

    m_pYMBasicWidget = new YMBasicWidget;
    m_pYMOneDepthWidget = new YMOneDepthWidget;
    m_pYMTwoDepthWidget = new YMTwoDepthWidget;
    m_pYMTwoDepthAWidget = new YMTwoDepthAWidget;

    m_pYMNxNMatrixWidget = new YMNxNMatrixWidget;
    m_pYMPlatformWidget = new YMPlatformWidget;
    m_pYMCenterKeywordWidget = new YMCenterKeywordWidget;
    p_qwDict = new SDictionary;

    m_ptwSource = new QTabWidget;
    m_ptwSource->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_pTAInterface = new TableAlgorithmInterface;
    m_pTAInterface->ReloadColumn();
    m_pUploadWidget = new YMUploadWidget();


    m_ptwResult = new QTabWidget;
    m_ptwResult->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    m_ptwResult->addTab(m_pYMBasicWidget, "Basic");
    m_ptwResult->addTab(m_pYMOneDepthWidget, "OneDepth");
    m_ptwResult->addTab(m_pYMTwoDepthWidget, "TwoDepth");
    m_ptwResult->addTab(m_pYMTwoDepthAWidget, "TwoDepthAdv");
    m_ptwResult->addTab(m_pYMPlatformWidget, "Platform");
    m_ptwResult->addTab(m_pYMNxNMatrixWidget, "NxNMatrix");
    m_ptwResult->addTab(m_pYMCenterKeywordWidget, "Keyword");


    m_pYMBasicWidget->setTabWidget(GetMainWindow()->m_pDataDlg->GetCurrentTab());
    m_pYMOneDepthWidget->setTabWidget(GetMainWindow()->m_pDataDlg->GetCurrentTab());
    m_pYMTwoDepthWidget->setTabWidget(GetMainWindow()->m_pDataDlg->GetCurrentTab());
    m_pYMNxNMatrixWidget->setTabWidget(GetMainWindow()->m_pDataDlg->GetCurrentTab());
    m_pYMPlatformWidget->setTabWidget(GetMainWindow()->m_pDataDlg->GetCurrentTab());
    m_pYMTwoDepthAWidget->setTabWidget(GetMainWindow()->m_pDataDlg->GetCurrentTab());
    m_pYMCenterKeywordWidget->setTabWidget(GetMainWindow()->m_pDataDlg->GetCurrentTab());


    m_pUploadWidget->setResultTabWidget(m_ptwResult);
    m_pUploadWidget->setSourceTabWidget(GetMainWindow()->m_pDataDlg->GetCurrentTab());
    QHBoxLayout *layout = new QHBoxLayout;

    layout->setMargin(5);
    layout->addWidget(m_ptwResult);

    widget->setLayout(layout);
//! [1]

    actD3View = new QAction(tr("D3View"), this);
    actD3View->setShortcut(Qt::Key_0 | Qt::CTRL);
    connect(actD3View, SIGNAL(triggered()), this, SLOT(slotD3View()));
    this->addAction(actD3View);

//! [2]
    createActions();
    createMenus();

    QString message = tr("A context menu is available by right-clicking");
    statusBar()->showMessage(message);
    //test = new QWebView(this);
    setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
    setWindowTitle(tr("Morphere Analyzer"));
    setMinimumSize(800, 600);
    resize(800, 600);
}
void SubWindow::createActions()
{
//! [5]
//!
    actDBNew = new QAction(tr("&New DB "), this);
    actDBNew->setStatusTip(tr("Create a new DB"));
    connect(actDBNew, SIGNAL(triggered()), this, SLOT(newDB()));

    actDBCsvImport = new QAction(tr("DB - CSV Import"), this);
    actDBCsvImport->setStatusTip(tr("Import DB as a csv file"));
    connect(actDBCsvImport, SIGNAL(triggered()), this, SLOT(importDB()));

    actDBCsvExport  = new QAction(tr("DB - CSV Export"), this);
    actDBCsvExport->setStatusTip(tr("Export DB as a csv file"));
    connect(actDBCsvExport, SIGNAL(triggered()), this, SLOT(exportDB()));

    actMorphereOriginalExport  = new QAction(tr("Morphere - CSV Export(OtherType Result)"), this);
    actMorphereOriginalExport->setStatusTip(tr("Export Morphere as a csv file"));
    connect(actMorphereOriginalExport, SIGNAL(triggered()), this, SLOT(exportOriginalMorphere()));

    actMorphereExport  = new QAction(tr("Morphere - CSV Export"), this);
    actMorphereExport->setStatusTip(tr("Export Morphere as a csv file"));
    connect(actMorphereExport, SIGNAL(triggered()), this, SLOT(exportMorphere()));

    actMorphereImport  = new QAction(tr("Morphere - CSV Import"), this);
    actMorphereImport->setStatusTip(tr("Import Morphere as a csv file"));
    connect(actMorphereImport, SIGNAL(triggered()), this, SLOT(importMorphere()));

    actExit = new QAction(tr("Exit"), this);
    actExit->setStatusTip(tr("Exit the application"));
    connect(actExit, SIGNAL(triggered()), this, SLOT(close()));

    actAnalyze = new QAction(tr("Analyze"), this);
    actAnalyze->setStatusTip(tr("Analyze"));
    connect(actAnalyze, SIGNAL(triggered()), this, SLOT(slotAnalyze()));

    actDictionary = new QAction(tr("Dictionary"), this);
    actAnalyze->setStatusTip(tr("Execute Dictionary Widget"));
    connect(actDictionary, SIGNAL(triggered()), this, SLOT(slotDictionary()));

    actTestUpload = new QAction(tr("Test Upload"), this);
    connect(actTestUpload, SIGNAL(triggered()), this, SLOT(slotTestUpload()));
    actShowUpload = new QAction(tr("Show Upload"), this);
    connect(actShowUpload, SIGNAL(triggered()), this, SLOT(slotShowUpload()));
    actD3ViewTree = new QAction(tr("D3View Tree"), this);
    connect(actD3ViewTree, SIGNAL(triggered()), this, SLOT(slotD3ViewTree()));

    actD3ViewTreeFile = new QAction(tr("D3ViewTree From File"), this);
    connect(actD3ViewTreeFile, SIGNAL(triggered()), this, SLOT(slotD3ViewTreeFile()));

    actD3ViewFile = new QAction(tr("D3View From File"), this);
    connect(actD3ViewFile, SIGNAL(triggered()), this, SLOT(slotD3ViewFile()));



}

void SubWindow::slotShowUpload()
{
    m_pUploadWidget->show();

}

void SubWindow::slotD3View()
{
    ((YMorphereWidget*)(m_ptwResult->currentWidget()))->D3View();
    //qDebug() << "hide the D3View";
}

void SubWindow::slotD3ViewTree()
{
    ((YMorphereWidget*)(m_ptwResult->currentWidget()))->D3ViewTree();
    //qDebug() << "hide the D3View";
}

void SubWindow::slotD3ViewFile()
{
    ((YMorphereWidget*)(m_ptwResult->currentWidget()))->D3ViewFile();
    //qDebug() << "hide the D3View";
}

void SubWindow::slotD3ViewTreeFile()
{
    ((YMorphereWidget*)(m_ptwResult->currentWidget()))->D3ViewTreeFile();
    //qDebug() << "hide the D3View";
}


void SubWindow::createMenus()
{
//! [9] //! [10]
    menuFile = menuBar()->addMenu(tr("File"));

    menuFile->addAction(actDBNew);
    menuFile->addSeparator();
//! [9]
    menuFile->addAction(actDBCsvImport);
//! [10]
    menuFile->addAction(actDBCsvExport);
    menuFile->addSeparator();
    menuFile->addAction(actMorphereImport);
    menuFile->addAction(actMorphereExport);
    menuFile->addAction(actMorphereOriginalExport);

    menuFile->addSeparator();
    menuFile->addAction(actExit);
//! [11]

//! [11]
    menuDictionary = menuBar()->addMenu(tr("Dictionary"));
    menuDictionary->addAction(actDictionary);

    menuAnalyze = menuBar()->addMenu(tr("Analyze"));
    menuAnalyze->addAction(actAnalyze);

    menuTestUpload = menuBar()->addMenu(tr("TestUpload"));
    menuTestUpload->addAction(actTestUpload);

    menuShowUpload = menuBar()->addMenu(tr("ShowUpload"));
    menuShowUpload->addAction(actShowUpload);

    menuD3View = menuBar()->addMenu(tr("D3View"));
    menuD3View->addAction(actD3View);
    menuD3View->addAction(actD3ViewFile);
    menuD3View->addAction(actD3ViewTree);
    menuD3View->addAction(actD3ViewTreeFile);

}
//! [12]


void SubWindow::newDB()
{
}

void SubWindow::importDB()
{
}

void SubWindow::exportDB()
{
}

void SubWindow::importMorphere()
{
}

void SubWindow::exportMorphere()
{
    ((YMorphereWidget*)(m_ptwResult->currentWidget()))->SaveFile();
}

void SubWindow::exportOriginalMorphere()
{
    ((YMorphereWidget*)(m_ptwResult->currentWidget()))->SaveFileSimple();
}

void SubWindow::slotAnalyze()
{
    QMessageBox msg2;
    msg2.setWindowFlags(msg2.windowFlags() | Qt::WindowCloseButtonHint);
    msg2.setText("Please wait...");
    msg2.setVisible(true);

    m_pTAInterface->setSource(GetMainWindow()->m_pDataDlg->GetCurrentWidget());
    YAFactory::getInstance()->getYAlgorithm(((YMorphereWidget*)(m_ptwResult->currentWidget()))->convertToParam())->setDataAlgorithmInterface(m_pTAInterface);
    m_ptwResult->currentWidget();

    ((YMorphereWidget*)(m_ptwResult->currentWidget()))->Exec();

    msg2.setVisible(false);
}

void SubWindow::slotTestUpload()
{
    ((YMorphereWidget*)(m_ptwResult->currentWidget()))->TestUpload();
}

void SubWindow::slotDictionary()
{
    p_qwDict->show();
    p_qwDict->repaint();
}

void SubWindow::closeEvent(QCloseEvent *event)
{
    QApplication::quit();
}
