/*
 * Copyright (c) 2021, Technische Universität Kaiserslautern
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. Neither the name of the copyright holder 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 HOLDER
 * 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.
 *
 * Authors:
 *    Derek Christ
 */

#ifndef SIMULATIONDIALOG_H
#define SIMULATIONDIALOG_H

#include "ui_simulationdialog.h"

#include "DRAMSys/config/DRAMSysConfiguration.h"

#include <QFileInfo>
#include <QPointer>
#include <QProcess>
#include <QSyntaxHighlighter>
#include <QTemporaryFile>
#include <QWidget>

namespace Ui
{
class SimulationDialog;
}

class SimulationDialog : public QWidget
{
    Q_OBJECT

public:
    explicit SimulationDialog(QWidget *parent = nullptr);

signals:
    void openFileRequested(const QString &path);

private Q_SLOTS:
    void on_browseDramSysButton_clicked();
    void on_browseConfigButton_clicked();
    void on_browseOutputButton_clicked();
    void on_browseResourceDirButton_clicked();
    void on_simulateButton_clicked();
    void on_reloadButton_clicked();
    void on_saveButton_clicked();
    void on_stopButton_clicked();

private:
    void loadConfigurationFromPath();
    void loadConfigurationFromTextFields();

    void loadConfiguration();
    void loadSimConfig();
    void loadMcConfig();
    void loadMemSpec();
    void loadAddressMapping();
    void loadTraceSetup();
    void loadPreview();

    void showStopButton(bool val);
    void saveConfiguration(QFile &file);
    void processMessage(const std::string &msg);

    QFileInfoList getSimulationResults();
    void openSimulationResults(const QFileInfoList &fileInfos);

    QTemporaryFile temporaryConfigurationFile;
    DRAMSys::Config::Configuration configuration;

    QPointer<QProcess> simulatorProcess;

    Ui::SimulationDialog *ui;
};

#endif
