///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2021-2024 Jon Beniston, M7RCE <jon@beniston.com>                //
// Copyright (C) 2021-2022 Edouard Griffiths, F4EXB <f4exb06@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 as version 3 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 V3 for more details.                               //
//                                                                               //
// You should have received a copy of the GNU General Public License             //
// along with this program. If not, see <http://www.gnu.org/licenses/>.          //
///////////////////////////////////////////////////////////////////////////////////

#include <cmath>

#include <QMessageBox>
#include <QLineEdit>
#include <QDesktopServices>
#include <QAction>
#include <QClipboard>
#include <QPainter>
#include <QWheelEvent>

#include "feature/featureuiset.h"
#include "feature/featurewebapiutils.h"
#include "gui/basicfeaturesettingsdialog.h"
#include "gui/tabletapandhold.h"
#include "gui/dialogpositioner.h"
#include "gui/crightclickenabler.h"
#include "util/mmsi.h"
#include "maincore.h"

#include "ui_aisgui.h"
#include "ais.h"
#include "aisgui.h"

#include "SWGMapItem.h"

// Models to use for ships when type is unknown
// Use as many as possibly, so it doesn't look too samey, but don't use
// the massive ships
QStringList AISGUI::m_shipModels = {
    "ship_27m.glbe", "ship_65m.glbe",
    "tug_20m.glbe", "tug_30m_1.glbe", "tug_30m_2.glbe", "tug_30m_3.glbe",
    "cargo_75m.glbe", "tanker_50m.glbe", "dredger_53m.glbe",
    "trawler_22m.glbe",
    "speedboat_8m.glbe", /*"yacht_10m.glbe",*/ "yacht_20m.glbe", "yacht_42m.glbe"
};

QStringList AISGUI::m_sailboatModels = {
    "sailboat_8m.glbe", "sailboat_17m.glbe"
};

QHash<QString, float> AISGUI::m_labelOffset = {
    {"helicopter.glb", 4.0f},
    {"antenna.glb", 4.5f},
    {"buoy.glb", 1.5f},
    {"ship_27m.glbe", 13.0f},
    {"dredger_53m.glbe", 19.0f},
    {"ship_65m.glbe", 26.0f},
    {"tug_20m.glbe", 10.0f},
    {"tug_30m_1.glbe", 17.0f},
    {"tug_30m_2.glbe", 17.0f},
    {"tug_30m_3.glbe", 17.0f},
    {"coastguard.glbe", 4.0},
    {"cargo_75m.glbe", 22.0f},
    {"cargo_190m.glbe", 42.0f},
    {"cargo_230m.glbe", 42.0f},
    {"tanker_50m.glbe", 12.0f},
    {"tanker_180m.glbe", 35.0f},
    {"tanker_245m_1.glbe", 30.0f},
    {"tanker_245m_2.glbe", 30.0f},
    {"tanker_245m_3.glbe", 30.0f},
    {"tanker_380m_1.glbe", 42.0f},
    {"tanker_380m_2.glbe", 42.0f},
    {"passenger_100m.glbe", 34.0f},
    {"dredger_53m.glbe", 19.0f},
    {"trawler_22m.glbe", 15.0f},
    {"sailboat_8m.glbe", 11.0f},
    {"sailboat_17m.glbe", 24.0f},
    {"speedboat_8m.glbe", 3.0f},
    {"yacht_10m.glbe", 3.0f},
    {"yacht_20m.glbe", 7.5f},
    {"yacht_42m.glbe", 10.0f},
};

QHash<QString, float> AISGUI::m_modelOffset = {
    {"helicopter.glb", 4.0f},
};

AISGUI* AISGUI::create(PluginAPI* pluginAPI, FeatureUISet *featureUISet, Feature *feature)
{
    AISGUI* gui = new AISGUI(pluginAPI, featureUISet, feature);
    return gui;
}

void AISGUI::destroy()
{
    delete this;
}

void AISGUI::resetToDefaults()
{
    m_settings.resetToDefaults();
    displaySettings();
    applySettings(true);
}

QByteArray AISGUI::serialize() const
{
    return m_settings.serialize();
}

bool AISGUI::deserialize(const QByteArray& data)
{
    if (m_settings.deserialize(data))
    {
        m_feature->setWorkspaceIndex(m_settings.m_workspaceIndex);
        displaySettings();
        applySettings(true);
        return true;
    }
    else
    {
        resetToDefaults();
        return false;
    }
}

bool AISGUI::handleMessage(const Message& message)
{
    if (AIS::MsgConfigureAIS::match(message))
    {
        qDebug("AISGUI::handleMessage: AIS::MsgConfigureAIS");
        const AIS::MsgConfigureAIS& cfg = (AIS::MsgConfigureAIS&) message;

        if (cfg.getForce()) {
            m_settings = cfg.getSettings();
        } else {
            m_settings.applySettings(cfg.getSettingsKeys(), cfg.getSettings());
        }

        blockApplySettings(true);
        displaySettings();
        blockApplySettings(false);

        return true;
    }
    else if (MainCore::MsgPacket::match(message))
    {
        MainCore::MsgPacket& report = (MainCore::MsgPacket&) message;

        // Decode the message
        AISMessage *ais = AISMessage::decode(report.getPacket());
        // Update table
        if (ais)
        {
            updateVessels(ais, report.getDateTime());
            m_messageCount++;
            delete ais;
        }

        return true;
    }

    return false;
}

void AISGUI::handleInputMessages()
{
    Message* message;

    while ((message = getInputMessageQueue()->pop()))
    {
        if (handleMessage(*message)) {
            delete message;
        }
    }
}

void AISGUI::onWidgetRolled(QWidget* widget, bool rollDown)
{
    (void) widget;
    (void) rollDown;

    RollupContents *rollupContents = getRollupContents();

    rollupContents->saveState(m_rollupState);
    applySettings();
}

AISGUI::AISGUI(PluginAPI* pluginAPI, FeatureUISet *featureUISet, Feature *feature, QWidget* parent) :
    FeatureGUI(parent),
    ui(new Ui::AISGUI),
    m_pluginAPI(pluginAPI),
    m_featureUISet(featureUISet),
    m_doApplySettings(true),
    m_lastFeatureState(0),
    m_messageRateTime(QDateTime::currentDateTime()),
    m_messageCount(0),
    m_chartTickCount(0),
    m_chart(nullptr),
    m_messageSeries(nullptr),
    m_shipSeries(nullptr),
    m_chartXAxis(nullptr),
    m_messageYAxis(nullptr),
    m_shipYAxis(nullptr)
{
    m_feature = feature;
    setAttribute(Qt::WA_DeleteOnClose, true);
    m_helpURL = "plugins/feature/ais/readme.md";
    RollupContents *rollupContents = getRollupContents();
	ui->setupUi(rollupContents);
    rollupContents->arrangeRollups();
	connect(rollupContents, SIGNAL(widgetRolled(QWidget*,bool)), this, SLOT(onWidgetRolled(QWidget*,bool)));

    m_ais = reinterpret_cast<AIS*>(feature);
    m_ais->setMessageQueueToGUI(&m_inputMessageQueue);

    connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onMenuDialogCalled(const QPoint &)));
    connect(getInputMessageQueue(), SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()));

    // Timer to remove vessels we haven't heard from in a while
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(removeOldVessels()));
    m_timer.start(60*1000);

    connect(&m_chartTimer, &QTimer::timeout, this, &AISGUI::updateChart);

    // Resize the table using dummy data
    resizeTable();
    // Allow user to reorder columns
    ui->vessels->horizontalHeader()->setSectionsMovable(true);
    // Allow user to sort table by clicking on headers
    ui->vessels->setSortingEnabled(true);
    // Add context menu to allow hiding/showing of columns
    vesselsMenu = new QMenu(ui->vessels);
    for (int i = 0; i < ui->vessels->horizontalHeader()->count(); i++)
    {
        QString text = ui->vessels->horizontalHeaderItem(i)->text();
        vesselsMenu->addAction(createCheckableItem(text, i, true, SLOT(vesselsColumnSelectMenuChecked())));
    }
    ui->vessels->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->vessels->horizontalHeader(), SIGNAL(customContextMenuRequested(QPoint)), SLOT(vesselsColumnSelectMenu(QPoint)));
    // Get signals when columns change
    connect(ui->vessels->horizontalHeader(), SIGNAL(sectionMoved(int, int, int)), SLOT(vessels_sectionMoved(int, int, int)));
    connect(ui->vessels->horizontalHeader(), SIGNAL(sectionResized(int, int, int)), SLOT(vessels_sectionResized(int, int, int)));
    // Context menu
    ui->vessels->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->vessels, SIGNAL(customContextMenuRequested(QPoint)), SLOT(vessels_customContextMenuRequested(QPoint)));
    TableTapAndHold *tableTapAndHold = new TableTapAndHold(ui->vessels);
    connect(tableTapAndHold, &TableTapAndHold::tapAndHold, this, &AISGUI::vessels_customContextMenuRequested);
    connect(ui->vessels, &QTableWidget::currentCellChanged, this, &AISGUI::vesselSelectionChanged);
    connect(&m_vesselFinder, &VesselFinder::shipPhoto, this, &AISGUI::shipPhoto);
    connect(ui->hidePhoto, &QToolButton::clicked, this, &AISGUI::hideShipPhotoClicked);

    CRightClickEnabler *displayChartClickEnabler = new CRightClickEnabler(ui->displayChart);
    connect(displayChartClickEnabler, &CRightClickEnabler::rightClick, this, &AISGUI::clearChart);

    ui->photoCredit->setOpenExternalLinks(true);
    hideShipPhoto();
    ui->chart->setRenderHint(QPainter::Antialiasing);
    plotChart();
    m_chartTimer.start(1000);

    m_settings.setRollupState(&m_rollupState);

    displaySettings();
    applySettings(true);
    makeUIConnections();
    m_resizer.enableChildMouseTracking();
}

void AISGUI::makeUIConnections()
{
    QObject::connect(
        ui->vessels,
        &QTableWidget::cellDoubleClicked,
        this,
        &AISGUI::on_vessels_cellDoubleClicked
    );
    QObject::connect(
        ui->displayChart,
        &ButtonSwitch::clicked,
        this,
        &AISGUI::on_displayChart_clicked
    );
    QObject::connect(
        ui->deleteVessels,
        &QToolButton::clicked,
        this,
        &AISGUI::on_deleteVessels_clicked
    );
}

void AISGUI::hideShipPhoto()
{
    ui->shipPhoto->clear();
    ui->photoCredit->clear();
    ui->photoPanel->setVisible(false);
}

void AISGUI::hideShipPhotoClicked()
{
    // Prevent an outstanding download from showing the photo again.
    m_selectedPhotoId.clear();
    hideShipPhoto();
}

void AISGUI::on_displayChart_clicked(bool checked)
{
    m_settings.m_displayChart = checked;
    m_settingsKeys.append("displayChart");
    applySettings();
    ui->chart->setVisible(checked);
}

void AISGUI::on_deleteVessels_clicked()
{
    deleteAllVessels();
}

void AISGUI::deleteAllVessels()
{
    m_selectedPhotoId.clear();
    hideShipPhoto();

    for (int row = ui->vessels->rowCount() - 1; row >= 0; row--)
    {
        const QString mmsi = ui->vessels->item(row, VESSEL_COL_MMSI)->text();
        sendToMap(mmsi, "",
            "", "",
            "", 0.0f, 0.0f,
            0.0f, 0.0f, QDateTime(),
            0.0f);
    }

    ui->vessels->setRowCount(0);
    qDeleteAll(m_vessels);
    m_vessels.clear();
}

int AISGUI::countShips() const
{
    int ships = 0;

    for (int row = 0; row < ui->vessels->rowCount(); row++)
    {
        const QTableWidgetItem *typeItem = ui->vessels->item(row, VESSEL_COL_TYPE);

        if (typeItem && (typeItem->text() == "Vessel")) {
            ships++;
        }
    }

    return ships;
}

void AISGUI::clearChart(const QPoint& p)
{
    (void) p;

    m_messageSeries->clear();
    m_shipSeries->clear();
    m_messageCount = 0;
    m_chartTickCount = 0;
    m_averageTime = QDateTime();
    m_messageRateTime = QDateTime::currentDateTime();
    resetChartAxes();
}

void AISGUI::resetChartAxes()
{
    const QDateTime now = QDateTime::currentDateTime();
    m_chartXAxis->setMin(now.addSecs(-60));
    m_chartXAxis->setMax(now);
    m_messageYAxis->setRange(0.0, 10.0);
    m_shipYAxis->setRange(0.0, 10.0);
}

static void scaleChartAxis(qint64& start, qint64& end, qint64 minimumRange, int delta, qreal centre)
{
    qint64 range = end - start;
    const double factor = std::pow(0.5, std::abs(delta) / 120.0);
    qint64 newRange;

    if (delta < 0) {
        newRange = range / factor;
    } else {
        newRange = range * factor;
    }

    range = std::max(minimumRange / 2, range);
    newRange = std::max(minimumRange, newRange);

    if (delta < 0) {
        start -= centre * range;
    } else {
        start += centre * newRange;
    }

    end = start + newRange;
}

bool AISGUI::eventFilter(QObject *obj, QEvent *event)
{
    if ((obj == ui->chart) && (event->type() == QEvent::Wheel))
    {
        QWheelEvent *wheelEvent = static_cast<QWheelEvent *>(event);
        const int delta = wheelEvent->angleDelta().y();

        if (m_messageSeries)
        {
            const QPointF point = wheelEvent->position();
            const QRectF plotArea = m_chart->plotArea();

            if ((plotArea.width() > 0.0) && (plotArea.height() > 0.0))
            {
                if (wheelEvent->modifiers() & Qt::ShiftModifier)
                {
                    qreal y = (point.y() - plotArea.y()) / plotArea.height();
                    y = 1.0 - std::min(1.0, std::max(0.0, y));

                    if (m_messageSeries->isVisible())
                    {
                        qint64 min = (qint64) m_messageYAxis->min();
                        qint64 max = (qint64) m_messageYAxis->max();
                        scaleChartAxis(min, max, 2LL, delta / 2, y);
                        min = std::max(0LL, min);
                        max = std::min(5000LL, max);
                        m_messageYAxis->setMin((qreal) min);
                        m_messageYAxis->setMax((qreal) max);
                    }

                    if (m_shipSeries->isVisible())
                    {
                        qint64 min = (qint64) m_shipYAxis->min();
                        qint64 max = (qint64) m_shipYAxis->max();
                        scaleChartAxis(min, max, 2LL, delta / 2, y);
                        min = std::max(0LL, min);
                        max = std::min(5000LL, max);
                        m_shipYAxis->setMin((qreal) min);
                        m_shipYAxis->setMax((qreal) max);
                    }
                }
                else if (m_messageSeries->count() > 1)
                {
                    qreal x = (point.x() - plotArea.x()) / plotArea.width();
                    x = std::min(1.0, std::max(0.0, x));

                    qint64 startMS = m_chartXAxis->min().toMSecsSinceEpoch();
                    qint64 endMS = m_chartXAxis->max().toMSecsSinceEpoch();
                    scaleChartAxis(startMS, endMS, 10000LL, delta, x);

                    startMS = std::max((qint64) m_messageSeries->at(0).x(), startMS);
                    endMS = std::min((qint64) m_messageSeries->at(m_messageSeries->count() - 1).x(), endMS);
                    m_chartXAxis->setMin(QDateTime::fromMSecsSinceEpoch(startMS));
                    m_chartXAxis->setMax(QDateTime::fromMSecsSinceEpoch(endMS));
                }
            }
        }

        wheelEvent->accept();
        return true;
    }

    return FeatureGUI::eventFilter(obj, event);
}

void AISGUI::plotChart()
{
    QChart *oldChart = m_chart;
    m_chart = new QChart();
    m_chart->layout()->setContentsMargins(0, 0, 0, 0);
    m_chart->setMargins(QMargins(1, 1, 1, 1));
    m_chart->setTheme(QChart::ChartThemeDark);
    m_chart->legend()->setAlignment(Qt::AlignRight);

    m_messageSeries = new QLineSeries();
    m_messageSeries->setName("Messages");
    m_shipSeries = new QLineSeries();
    m_shipSeries->setName("Ships");

    m_chartXAxis = new QDateTimeAxis();
    m_chartXAxis->setFormat("hh:mm:ss");
    m_messageYAxis = new QValueAxis();
    m_messageYAxis->setTitleText("Messages/s");
    m_shipYAxis = new QValueAxis();
    m_shipYAxis->setTitleText("Ships");

    m_chart->addAxis(m_chartXAxis, Qt::AlignBottom);
    m_chart->addAxis(m_messageYAxis, Qt::AlignLeft);
    m_chart->addAxis(m_shipYAxis, Qt::AlignRight);
    m_chart->addSeries(m_messageSeries);
    m_chart->addSeries(m_shipSeries);
    m_messageSeries->attachAxis(m_chartXAxis);
    m_messageSeries->attachAxis(m_messageYAxis);
    m_shipSeries->attachAxis(m_chartXAxis);
    m_shipSeries->attachAxis(m_shipYAxis);
    resetChartAxes();

    ui->chart->setChart(m_chart);
    ui->chart->installEventFilter(this);

    const auto markers = m_chart->legend()->markers();
    for (QLegendMarker *marker : markers) {
        connect(marker, &QLegendMarker::clicked, this, &AISGUI::legendMarkerClicked);
    }

    delete oldChart;
}

void AISGUI::legendMarkerClicked()
{
    QLegendMarker *marker = qobject_cast<QLegendMarker *>(sender());

    if (!marker) {
        return;
    }

    marker->series()->setVisible(!marker->series()->isVisible());
    marker->setVisible(true);

    const qreal alpha = marker->series()->isVisible() ? 1.0 : 0.5;
    QBrush brush = marker->labelBrush();
    QColor color = brush.color();
    color.setAlphaF(alpha);
    brush.setColor(color);
    marker->setLabelBrush(brush);

    brush = marker->brush();
    color = brush.color();
    color.setAlphaF(alpha);
    brush.setColor(color);
    marker->setBrush(brush);

    QPen pen = marker->pen();
    color = pen.color();
    color.setAlphaF(alpha);
    pen.setColor(color);
    marker->setPen(pen);
}

void AISGUI::updateChart()
{
    const QDateTime now = QDateTime::currentDateTime();
    const qint64 elapsedMS = m_messageRateTime.msecsTo(now);

    if (elapsedMS <= 0) {
        return;
    }

    const double messageRate = m_messageCount / (elapsedMS / 1000.0);
    const int ships = countShips();
    const bool xAtMax = (m_messageSeries->count() == 0)
        || (m_messageSeries->at(m_messageSeries->count() - 1).x() == m_chartXAxis->max().toMSecsSinceEpoch());

    m_messageSeries->append(now.toMSecsSinceEpoch(), messageRate);
    m_shipSeries->append(now.toMSecsSinceEpoch(), ships);

    if (xAtMax) {
        m_chartXAxis->setMax(now);
    }
    if (m_messageYAxis->max() < messageRate) {
        m_messageYAxis->setMax(std::ceil(messageRate) + 1.0);
    }
    if (m_shipYAxis->max() < ships + 1) {
        m_shipYAxis->setMax(ships + 1);
    }

    m_messageCount = 0;
    m_messageRateTime = now;
    m_chartTickCount++;

    // Average one-second samples after ten minutes to limit long-running chart growth.
    if ((m_chartTickCount % 60) == 0)
    {
        QDateTime startTime;
        QDateTime endTime;

        if (m_averageTime.isValid())
        {
            startTime = m_averageTime;
            endTime = startTime.addSecs(60);
        }
        else
        {
            endTime = now.addSecs(-10 * 60);
            startTime = endTime.addSecs(-60);
        }

        averageSeries(m_messageSeries, startTime, endTime);
        averageSeries(m_shipSeries, startTime, endTime);
        m_averageTime = endTime;
    }
}

void AISGUI::averageSeries(QLineSeries *series, const QDateTime& startTime, const QDateTime& endTime)
{
    int startIndex = 0;
    int endIndex = -1;

    for (int i = series->count() - 1; i >= 0; i--)
    {
        const QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(series->at(i).x());

        if ((endIndex == -1) && (dateTime <= endTime)) {
            endIndex = i;
        } else if (dateTime < startTime) {
            startIndex = i + 1;
            break;
        }
    }

    const int count = endIndex - startIndex + 1;

    if ((endIndex != -1) && (count > 1))
    {
        double sum = 0.0;

        for (int i = startIndex; i <= endIndex; i++) {
            sum += series->at(i).y();
        }

        const double average = sum / count;
        const qint64 midpoint = startTime.toMSecsSinceEpoch()
            + (endTime.toMSecsSinceEpoch() - startTime.toMSecsSinceEpoch()) / 2;
        series->removePoints(startIndex, count);
        series->insert(startIndex, QPointF(midpoint, average));
    }
}

void AISGUI::vesselSelectionChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
{
    (void) currentColumn;
    (void) previousRow;
    (void) previousColumn;

    m_selectedPhotoId.clear();
    hideShipPhoto();

    if ((currentRow < 0) || (currentRow >= ui->vessels->rowCount())) {
        return;
    }

    const QTableWidgetItem *mmsiItem = ui->vessels->item(currentRow, VESSEL_COL_MMSI);
    const QTableWidgetItem *imoItem = ui->vessels->item(currentRow, VESSEL_COL_IMO);
    const QString mmsi = mmsiItem ? mmsiItem->text().trimmed() : QString();
    const QString imo = imoItem ? imoItem->text().trimmed() : QString();
    bool imoOk = false;
    bool mmsiOk = false;
    const qulonglong imoNumber = imo.toULongLong(&imoOk);
    const qulonglong mmsiNumber = mmsi.toULongLong(&mmsiOk);

    if (imoOk && (imoNumber != 0)) {
        m_selectedPhotoId = QString("imo:%1").arg(imoNumber);
    } else if (mmsiOk && (mmsiNumber != 0)) {
        m_selectedPhotoId = QString("mmsi:%1").arg(mmsiNumber);
    } else {
        return;
    }

    m_vesselFinder.getShipPhoto(imo, mmsi);
}

void AISGUI::shipPhoto(const VesselFinderPhoto *photo)
{
    // A request can complete after another row has been selected.
    if (!photo || (photo->m_id != m_selectedPhotoId)) {
        return;
    }

    if (photo->m_pixmap.isNull())
    {
        hideShipPhoto();
        return;
    }

    ui->shipPhoto->setPixmap(photo->m_pixmap.scaled(
        320,
        320,
        Qt::KeepAspectRatio,
        Qt::SmoothTransformation
    ));

    const QString link = photo->m_link.toHtmlEscaped();
    const QString credit = photo->m_photographer.isEmpty()
        ? QString("View on VesselFinder")
        : QString("Photo by %1").arg(photo->m_photographer.toHtmlEscaped());
    ui->photoCredit->setText(QString("<a href=\"%1\">%2</a>").arg(link, credit));
    ui->photoPanel->setVisible(true);
}

AISGUI::~AISGUI()
{
    m_chartTimer.stop();
    m_timer.stop();
    disconnect(&m_vesselFinder, &VesselFinder::shipPhoto, this, &AISGUI::shipPhoto);
    deleteAllVessels();
    delete ui;
}

void AISGUI::setWorkspaceIndex(int index)
{
    m_settings.m_workspaceIndex = index;
    m_feature->setWorkspaceIndex(index);
}

void AISGUI::blockApplySettings(bool block)
{
    m_doApplySettings = !block;
}

void AISGUI::displaySettings()
{
    setTitleColor(m_settings.m_rgbColor);
    setWindowTitle(m_settings.m_title);
    setTitle(m_settings.m_title);
    blockApplySettings(true);

    ui->displayChart->setChecked(m_settings.m_displayChart);
    ui->chart->setVisible(m_settings.m_displayChart);

    // Order and size columns
    QHeaderView *header = ui->vessels->horizontalHeader();
    for (int i = 0; i < AIS_VESSEL_COLUMNS; i++)
    {
        bool hidden = m_settings.m_vesselColumnSizes[i] == 0;
        header->setSectionHidden(i, hidden);
        vesselsMenu->actions().at(i)->setChecked(!hidden);

        if (m_settings.m_vesselColumnSizes[i] > 0) {
            ui->vessels->setColumnWidth(i, m_settings.m_vesselColumnSizes[i]);
        }

        header->moveSection(header->visualIndex(i), m_settings.m_vesselColumnIndexes[i]);
    }

    getRollupContents()->restoreState(m_rollupState);
    blockApplySettings(false);
    getRollupContents()->arrangeRollups();
}

void AISGUI::onMenuDialogCalled(const QPoint &p)
{
    if (m_contextMenuType == ContextMenuType::ContextMenuChannelSettings)
    {
        BasicFeatureSettingsDialog dialog(this);
        dialog.setTitle(m_settings.m_title);
        dialog.setUseReverseAPI(m_settings.m_useReverseAPI);
        dialog.setReverseAPIAddress(m_settings.m_reverseAPIAddress);
        dialog.setReverseAPIPort(m_settings.m_reverseAPIPort);
        dialog.setReverseAPIFeatureSetIndex(m_settings.m_reverseAPIFeatureSetIndex);
        dialog.setReverseAPIFeatureIndex(m_settings.m_reverseAPIFeatureIndex);
        dialog.setDefaultTitle(m_displayedName);

        dialog.move(p);
        new DialogPositioner(&dialog, false);
        dialog.exec();

        m_settings.m_title = dialog.getTitle();
        m_settings.m_useReverseAPI = dialog.useReverseAPI();
        m_settings.m_reverseAPIAddress = dialog.getReverseAPIAddress();
        m_settings.m_reverseAPIPort = dialog.getReverseAPIPort();
        m_settings.m_reverseAPIFeatureSetIndex = dialog.getReverseAPIFeatureSetIndex();
        m_settings.m_reverseAPIFeatureIndex = dialog.getReverseAPIFeatureIndex();

        setTitle(m_settings.m_title);
        setTitleColor(m_settings.m_rgbColor);

        m_settingsKeys.append("title");
        m_settingsKeys.append("useReverseAPI");
        m_settingsKeys.append("reverseAPIAddress");
        m_settingsKeys.append("reverseAPIPort");
        m_settingsKeys.append("reverseAPIFeatureSetIndex");
        m_settingsKeys.append("reverseAPIFeatureIndex");

        applySettings();
    }

    resetContextMenuType();
}

void AISGUI::removeOldVessels()
{
    // Remove if we haven't received a message in 10 minutes
    QDateTime currentDateTime = QDateTime::currentDateTime();
    for (int row = ui->vessels->rowCount() - 1; row >= 0; row--)
    {
        QDateTime lastDateTime = ui->vessels->item(row, VESSEL_COL_LAST_UPDATE)->data(Qt::DisplayRole).toDateTime();
        if (lastDateTime.isValid())
        {
            qint64 diff = lastDateTime.secsTo(currentDateTime);
            if (diff > 10*60)
            {
                QString mmsi = ui->vessels->item(row, VESSEL_COL_MMSI)->text();
                // Remove from map
                sendToMap(mmsi, "",
                    "", "",
                    "", 0.0f, 0.0f,
                    0.0f, 0.0f, QDateTime(),
                    0.0f);
                // Remove from table
                ui->vessels->removeRow(row);
                // Remove from hash and free memory
                delete m_vessels.take(mmsi);
            }
        }
    }
}

void AISGUI::applySettings(bool force)
{
    if (m_doApplySettings)
    {
        AIS::MsgConfigureAIS* message = AIS::MsgConfigureAIS::create(m_settings, m_settingsKeys, force);
        m_ais->getInputMessageQueue()->push(message);
    }

    m_settingsKeys.clear();
}

void AISGUI::resizeTable()
{
    // Fill table with a row of dummy data that will size the columns nicely
    int row = ui->vessels->rowCount();
    ui->vessels->setRowCount(row + 1);
    ui->vessels->setItem(row, VESSEL_COL_MMSI, new QTableWidgetItem("123456789"));
    ui->vessels->setItem(row, VESSEL_COL_COUNTRY, new QTableWidgetItem("flag"));
    ui->vessels->setItem(row, VESSEL_COL_TYPE, new QTableWidgetItem("Base station"));
    ui->vessels->setItem(row, VESSEL_COL_LATITUDE, new QTableWidgetItem("90.000000-"));
    ui->vessels->setItem(row, VESSEL_COL_LONGITUDE, new QTableWidgetItem("180.00000-"));
    ui->vessels->setItem(row, VESSEL_COL_COURSE, new QTableWidgetItem("360.0"));
    ui->vessels->setItem(row, VESSEL_COL_SPEED, new QTableWidgetItem("120"));
    ui->vessels->setItem(row, VESSEL_COL_HEADING, new QTableWidgetItem("360"));
    ui->vessels->setItem(row, VESSEL_COL_STATUS, new QTableWidgetItem("Under way using engine"));
    ui->vessels->setItem(row, VESSEL_COL_IMO, new QTableWidgetItem("123456789"));
    ui->vessels->setItem(row, VESSEL_COL_NAME, new QTableWidgetItem("12345678901234567890"));
    ui->vessels->setItem(row, VESSEL_COL_CALLSIGN, new QTableWidgetItem("1234567"));
    ui->vessels->setItem(row, VESSEL_COL_SHIP_TYPE, new QTableWidgetItem("Passenger"));
    ui->vessels->setItem(row, VESSEL_COL_LENGTH, new QTableWidgetItem("400"));
    ui->vessels->setItem(row, VESSEL_COL_DESTINATION, new QTableWidgetItem("12345678901234567890"));
    ui->vessels->setItem(row, VESSEL_COL_POSITION_UPDATE, new QTableWidgetItem("12/12/2022 12:00"));
    ui->vessels->setItem(row, VESSEL_COL_LAST_UPDATE, new QTableWidgetItem("12/12/2022 12:00"));
    ui->vessels->setItem(row, VESSEL_COL_MESSAGES, new QTableWidgetItem("1000"));
    ui->vessels->resizeColumnsToContents();
    ui->vessels->removeRow(row);
}

// Columns in table reordered
void AISGUI::vessels_sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex)
{
    (void) oldVisualIndex;

    m_settings.m_vesselColumnIndexes[logicalIndex] = newVisualIndex;
    m_settingsKeys.append("vesselColumnIndexes");
    applySettings();
}

// Column in table resized (when hidden size is 0)
void AISGUI::vessels_sectionResized(int logicalIndex, int oldSize, int newSize)
{
    (void) oldSize;

    m_settings.m_vesselColumnSizes[logicalIndex] = newSize;
    m_settingsKeys.append("vesselColumnSizes");
    applySettings();
}

// Right click in table header - show column select menu
void AISGUI::vesselsColumnSelectMenu(QPoint pos)
{
    vesselsMenu->popup(ui->vessels->horizontalHeader()->viewport()->mapToGlobal(pos));
}

// Hide/show column when menu selected
void AISGUI::vesselsColumnSelectMenuChecked(bool checked)
{
    (void) checked;

    QAction* action = qobject_cast<QAction*>(sender());
    if (action != nullptr)
    {
        int idx = action->data().toInt(nullptr);
        ui->vessels->setColumnHidden(idx, !action->isChecked());
    }
}

// Create column select menu item
QAction *AISGUI::createCheckableItem(QString &text, int idx, bool checked, const char *slot)
{
    QAction *action = new QAction(text, this);
    action->setCheckable(true);
    action->setChecked(checked);
    action->setData(QVariant(idx));
    connect(action, SIGNAL(triggered()), this, slot);
    return action;
}

// Send to Map feature
void AISGUI::sendToMap(const QString &name, const QString &label,
    const QString &image, const QString &text,
    const QString &model, float modelOffset, float labelOffset,
    float latitude, float longitude, QDateTime positionDateTime,
    float heading
    )
{
    QList<ObjectPipe*> mapPipes;
    MainCore::instance()->getMessagePipes().getMessagePipes(m_ais, "mapitems", mapPipes);

    for (const auto& pipe : mapPipes)
    {
        MessageQueue *messageQueue = qobject_cast<MessageQueue*>(pipe->m_element);
        SWGSDRangel::SWGMapItem *swgMapItem = new SWGSDRangel::SWGMapItem();
        swgMapItem->setName(new QString(name));
        swgMapItem->setLatitude(latitude);
        swgMapItem->setLongitude(longitude);
        swgMapItem->setAltitude(0);
        swgMapItem->setAltitudeReference(1); // CLAMP_TO_GROUND

        if (positionDateTime.isValid())
        {
            swgMapItem->setPositionDateTime(new QString(positionDateTime.toString(Qt::ISODateWithMs)));
            swgMapItem->setOrientationDateTime(new QString(positionDateTime.toString(Qt::ISODateWithMs)));
            swgMapItem->setAvailableUntil(new QString(positionDateTime.addSecs(10*60).toString(Qt::ISODateWithMs)));
        }

        swgMapItem->setImageRotation(heading);
        swgMapItem->setText(new QString(text));

        if (image.isEmpty()) {
            swgMapItem->setImage(new QString(""));
        } else {
            swgMapItem->setImage(new QString(QString("qrc:///ais/map/%1").arg(image)));
        }

        swgMapItem->setModel(new QString(model));
        swgMapItem->setModelAltitudeOffset(modelOffset);
        swgMapItem->setLabel(new QString(label));
        swgMapItem->setLabelAltitudeOffset(labelOffset);
        swgMapItem->setFixedPosition(false);
        swgMapItem->setOrientation(1);
        swgMapItem->setHeading(heading);
        swgMapItem->setPitch(0.0);
        swgMapItem->setRoll(0.0);

        MainCore::MsgMapItem *msg = MainCore::MsgMapItem::create(m_ais, swgMapItem);
        messageQueue->push(msg);
    }
}

// Update table with received message
void AISGUI::updateVessels(AISMessage *ais, QDateTime dateTime)
{
    QTableWidgetItem *mmsiItem;
    QTableWidgetItem *countryItem;
    QTableWidgetItem *typeItem;
    QTableWidgetItem *latitudeItem;
    QTableWidgetItem *longitudeItem;
    QTableWidgetItem *courseItem;
    QTableWidgetItem *speedItem;
    QTableWidgetItem *headingItem;
    QTableWidgetItem *statusItem;
    QTableWidgetItem *imoItem;
    QTableWidgetItem *nameItem;
    QTableWidgetItem *callsignItem;
    QTableWidgetItem *shipTypeItem;
    QTableWidgetItem *lengthItem;
    QTableWidgetItem *destinationItem;
    QTableWidgetItem *positionUpdateItem;
    QTableWidgetItem *lastUpdateItem;
    QTableWidgetItem *messagesItem;

    QString previousType;
    QString previousShipType;
    Vessel *vessel;

    // See if vessel is already in table
    QString messageMMSI = QString("%1").arg(ais->m_mmsi, 9, 10, QChar('0'));
    bool found = false;
    for (int row = 0; row < ui->vessels->rowCount(); row++)
    {
        QString itemMMSI = ui->vessels->item(row, VESSEL_COL_MMSI)->text();
        if (messageMMSI == itemMMSI)
        {
            // Update existing item
            mmsiItem = ui->vessels->item(row, VESSEL_COL_MMSI);
            countryItem = ui->vessels->item(row, VESSEL_COL_COUNTRY);
            typeItem = ui->vessels->item(row, VESSEL_COL_TYPE);
            latitudeItem = ui->vessels->item(row, VESSEL_COL_LATITUDE);
            longitudeItem = ui->vessels->item(row, VESSEL_COL_LONGITUDE);
            courseItem = ui->vessels->item(row, VESSEL_COL_COURSE);
            speedItem = ui->vessels->item(row, VESSEL_COL_SPEED);
            headingItem = ui->vessels->item(row, VESSEL_COL_HEADING);
            statusItem = ui->vessels->item(row, VESSEL_COL_STATUS);
            imoItem = ui->vessels->item(row, VESSEL_COL_IMO);
            nameItem = ui->vessels->item(row, VESSEL_COL_NAME);
            callsignItem = ui->vessels->item(row, VESSEL_COL_CALLSIGN);
            shipTypeItem = ui->vessels->item(row, VESSEL_COL_SHIP_TYPE);
            lengthItem = ui->vessels->item(row, VESSEL_COL_LENGTH);
            destinationItem = ui->vessels->item(row, VESSEL_COL_DESTINATION);
            positionUpdateItem = ui->vessels->item(row, VESSEL_COL_POSITION_UPDATE);
            lastUpdateItem = ui->vessels->item(row, VESSEL_COL_LAST_UPDATE);
            messagesItem = ui->vessels->item(row, VESSEL_COL_MESSAGES);
            vessel = m_vessels.value(messageMMSI);
            found = true;
            break;
        }
    }
    if (!found)
    {
        // Add new vessel
        ui->vessels->setSortingEnabled(false);
        int row = ui->vessels->rowCount();
        ui->vessels->setRowCount(row + 1);

        mmsiItem = new QTableWidgetItem();
        countryItem = new QTableWidgetItem();
        typeItem = new QTableWidgetItem();
        latitudeItem = new QTableWidgetItem();
        longitudeItem = new QTableWidgetItem();
        courseItem = new QTableWidgetItem();
        speedItem = new QTableWidgetItem();
        headingItem = new QTableWidgetItem();
        statusItem = new QTableWidgetItem();
        imoItem = new QTableWidgetItem();
        nameItem = new QTableWidgetItem();
        callsignItem = new QTableWidgetItem();
        shipTypeItem = new QTableWidgetItem();
        lengthItem = new QTableWidgetItem();
        destinationItem = new QTableWidgetItem();
        positionUpdateItem = new QTableWidgetItem();
        lastUpdateItem = new QTableWidgetItem();
        messagesItem = new QTableWidgetItem();
        ui->vessels->setItem(row, VESSEL_COL_MMSI, mmsiItem);
        ui->vessels->setItem(row, VESSEL_COL_COUNTRY, countryItem);
        ui->vessels->setItem(row, VESSEL_COL_TYPE, typeItem);
        ui->vessels->setItem(row, VESSEL_COL_LATITUDE, latitudeItem);
        ui->vessels->setItem(row, VESSEL_COL_LONGITUDE, longitudeItem);
        ui->vessels->setItem(row, VESSEL_COL_COURSE, courseItem);
        ui->vessels->setItem(row, VESSEL_COL_SPEED, speedItem);
        ui->vessels->setItem(row, VESSEL_COL_HEADING, headingItem);
        ui->vessels->setItem(row, VESSEL_COL_STATUS, statusItem);
        ui->vessels->setItem(row, VESSEL_COL_IMO, imoItem);
        ui->vessels->setItem(row, VESSEL_COL_NAME, nameItem);
        ui->vessels->setItem(row, VESSEL_COL_CALLSIGN, callsignItem);
        ui->vessels->setItem(row, VESSEL_COL_SHIP_TYPE, shipTypeItem);
        ui->vessels->setItem(row, VESSEL_COL_LENGTH, lengthItem);
        ui->vessels->setItem(row, VESSEL_COL_DESTINATION, destinationItem);
        ui->vessels->setItem(row, VESSEL_COL_POSITION_UPDATE, positionUpdateItem);
        ui->vessels->setItem(row, VESSEL_COL_LAST_UPDATE, lastUpdateItem);
        ui->vessels->setItem(row, VESSEL_COL_MESSAGES, messagesItem);
        messagesItem->setData(Qt::DisplayRole, 0);

        vessel = new Vessel();
        m_vessels.insert(messageMMSI, vessel);
    }

    previousType = typeItem->text();
    previousShipType = shipTypeItem->text();

    QString mmsi = QString("%1").arg(ais->m_mmsi, 9, 10, QChar('0'));
    mmsiItem->setText(mmsi);
    QIcon *flag = MMSI::getFlagIcon(mmsi);
    if (flag)
    {
        countryItem->setSizeHint(QSize(40, 20));
        countryItem->setIcon(*flag);
    }

    lastUpdateItem->setData(Qt::DisplayRole, dateTime);
    messagesItem->setData(Qt::DisplayRole, messagesItem->data(Qt::DisplayRole).toInt() + 1);

    if ((ais->m_id <= 3) || (ais->m_id == 5) || (ais->m_id == 18) || (ais->m_id == 19)) {
        typeItem->setText("Vessel");
    } else if (ais->m_id == 4) {
        typeItem->setText("Base station");
    } else if (ais->m_id == 9) {
        typeItem->setText("Aircraft");
    } else if (ais->m_id == 21) {
        typeItem->setText("Aid-to-nav");
    }
    if (ais->m_id == 21)
    {
         AISAidsToNavigationReport *aids = dynamic_cast<AISAidsToNavigationReport*>(ais);
         if (aids) {
             nameItem->setText(aids->m_name);
         }
    }
    if (ais->m_id == 5)
    {
         AISShipStaticAndVoyageData *vd = dynamic_cast<AISShipStaticAndVoyageData*>(ais);
         if (vd)
         {
             if (vd->m_imo != 0) {
                 imoItem->setData(Qt::DisplayRole, vd->m_imo);
             }
             nameItem->setText(vd->m_name);
             callsignItem->setText(vd->m_callsign);
             shipTypeItem->setText(AISMessage::typeToString(vd->m_type));
             lengthItem->setData(Qt::DisplayRole, vd->m_a + vd->m_b);
             destinationItem->setText(vd->m_destination);
         }
    }
    else
    {
        if (ais->hasPosition())
        {
            latitudeItem->setData(Qt::DisplayRole, ais->getLatitude());
            longitudeItem->setData(Qt::DisplayRole, ais->getLongitude());
            positionUpdateItem->setData(Qt::DisplayRole, dateTime);
        }
        if (ais->hasCourse()) {
            courseItem->setData(Qt::DisplayRole, ais->getCourse());
        }
        if (ais->hasSpeed()) {
            speedItem->setData(Qt::DisplayRole, ais->getSpeed());
        }
        if (ais->hasHeading()) {
            headingItem->setData(Qt::DisplayRole, ais->getHeading());
        }
        AISPositionReport *pr = dynamic_cast<AISPositionReport*>(ais);
        if (pr) {
            statusItem->setText(AISPositionReport::getStatusString(pr->m_status));
        }
        AISLongRangePositionReport *lrpr = dynamic_cast<AISLongRangePositionReport*>(ais);
        if (lrpr) {
            statusItem->setText(AISPositionReport::getStatusString(lrpr->m_status));
        }
    }
    if (ais->m_id == 19)
    {
        AISExtendedClassBPositionReport *ext = dynamic_cast<AISExtendedClassBPositionReport*>(ais);
        if (ext) {
             shipTypeItem->setText(AISMessage::typeToString(ext->m_type));
        }
    }
    if (ais->m_id == 24)
    {
        AISStaticDataReport *dr = dynamic_cast<AISStaticDataReport*>(ais);
        if (dr)
        {
            if (dr->m_partNumber == 0)
            {
                nameItem->setText(dr->m_name);
            }
            else if (dr->m_partNumber == 1)
            {
                callsignItem->setText(dr->m_callsign);
                shipTypeItem->setText(AISMessage::typeToString(dr->m_type));
            }
        }
    }
    ui->vessels->setSortingEnabled(true);

    QVariant latitudeV = latitudeItem->data(Qt::DisplayRole);
    QVariant longitudeV = longitudeItem->data(Qt::DisplayRole);
    QString type = typeItem->text();

    if (!latitudeV.isNull() && !longitudeV.isNull() && !type.isEmpty())
    {
        // Image and model to use on map
        QString shipType = shipTypeItem->text();
        int length = lengthItem->data(Qt::DisplayRole).toInt();
        QString status = statusItem->text();

        // Only update model if change in type - so we don't keeping picking new
        // random models.
        // Check if image is empty to handle case where ShipStaticData is received
        // before position
        if ((previousType != type) || (previousShipType != shipType) || vessel->m_image.isEmpty()) {
            getImageAndModel(type, shipType, length, status, vessel);
        }

        float labelOffset = m_labelOffset.value(vessel->m_model);
        float modelOffset = 0.0f;
        if (m_modelOffset.contains(vessel->m_model)) {
            modelOffset = m_modelOffset.value(vessel->m_model);
        }

        // Text to display in info box
        QStringList text;
        QVariant courseV = courseItem->data(Qt::DisplayRole);
        QVariant speedV = speedItem->data(Qt::DisplayRole);
        QVariant headingV = headingItem->data(Qt::DisplayRole);
        QString name = nameItem->text();
        QString callsign = callsignItem->text();
        QString destination = destinationItem->text();
        float heading = 0.0f;
        if (!name.isEmpty()) {
            text.append(QString("Name: %1").arg(name));
        }
        if (!callsign.isEmpty()) {
            text.append(QString("Callsign: %1").arg(callsign));
        }
        if (!destination.isEmpty()) {
            text.append(QString("Destination: %1").arg(destination));
        }
        if (!courseV.isNull())
        {
            float course = courseV.toFloat();
            text.append(QString("Course: %1%2").arg(course).arg(QChar(0xb0)));
            heading = course;
        }
        if (!speedV.isNull()) {
            text.append(QString("Speed: %1 knts").arg(speedV.toFloat()));
        }
        if (!headingV.isNull())
        {
            heading = headingV.toFloat();
            text.append(QString("Heading: %1%2").arg(heading).arg(QChar(0xb0)));
        }
        if (!shipType.isEmpty()) {
            text.append(QString("Ship type: %1").arg(shipType));
        }
        if (!status.isEmpty()) {
            text.append(QString("Status: %1").arg(status));
        }
        // Send to map feature
        sendToMap(mmsiItem->text(), callsign,
            vessel->m_image, text.join("<br>"),
            vessel->m_model, modelOffset, labelOffset,
            latitudeV.toFloat(), longitudeV.toFloat(), positionUpdateItem->data(Qt::DisplayRole).toDateTime(),
            heading);
    }
}

void AISGUI::getImageAndModel(const QString &type, const QString &shipType, int length, const QString &status, Vessel *vessel)
{
    if (type == "Aircraft")
    {
        // I presume search and rescue aircraft are more likely to be helicopters
        vessel->m_image = "helicopter.png";
        vessel->m_model = "helicopter.glb";
    }
    else if (type == "Base station")
    {
        vessel->m_image = "anchor.png";
        vessel->m_model = "antenna.glb";
    }
    else if (type == "Aid-to-nav")
    {
        vessel->m_image = "bouy.png";
        vessel->m_model = "buoy.glb";
    }
    else
    {
        vessel->m_image = "ship.png";
        if (status == "Under way sailing") {
            vessel->m_model = m_sailboatModels[m_random.bounded(m_sailboatModels.size())];
        } else {
            vessel->m_model = m_shipModels[m_random.bounded(m_shipModels.size())];
        }

        if (!shipType.isEmpty())
        {
            if (shipType == "Ship")
            {
                if (length < 40)
                {
                    vessel->m_model = "ship_27m.glbe";
                }
                else if (length < 60)
                {
                    vessel->m_model = "dredger_53m.glbe";
                }
                else
                {
                    vessel->m_model = "ship_65m.glbe";
                }
            }
            else if ((shipType == "Tug") || (shipType == "Port tender") || (shipType == "Pilot vessel") || (shipType == "Vessel - Towing"))
            {
                vessel->m_image = "tug.png";
                if (length < 25)
                {
                    vessel->m_model = "tug_20m.glbe";
                }
                else
                {
                    int rand = m_random.bounded(1, 4); // bounded() excludes the upper bound
                    vessel->m_model = QString("tug_30m_%1.glbe").arg(rand);
                }
            }
            else if ((shipType == "Law enforcement vessel") || (shipType == "Search and rescue vessel"))
            {
                vessel->m_model = "coastguard.glbe";
            }
            else if (shipType == "Cargo")
            {
                vessel->m_image = "cargo.png";
                if (length < 120)
                {
                    vessel->m_model = "cargo_75m.glbe";
                }
                else /*if (length < 200)*/
                {
                    vessel->m_model = "cargo_190m.glbe";
                }
                /*else
                {
                    vessel->m_model = "cargo_230m.glbe";
                }*/
            }
            else if (shipType == "Tanker")
            {
                vessel->m_image = "tanker.png";
                if (length < 120)
                {
                    vessel->m_model = "tanker_50m.glbe";
                }
                else if (length < 210)
                {
                    vessel->m_model = "tanker_180m.glbe";
                }
                else if (length < 300)
                {
                    int rand = m_random.bounded(1, 4);
                    vessel->m_model = QString("tanker_245m_%1.glbe").arg(rand);
                }
                else
                {
                    int rand = m_random.bounded(1, 3);
                    vessel->m_model = QString("tanker_380m_%1.glbe").arg(rand);
                }
            }
            else if (shipType == "Passenger")
            {
                vessel->m_model = "passenger_100m.glbe";
            }
            else if (shipType == "Vessel - Dredging or underwater operations")
            {
                vessel->m_model = "dredger_53m.glbe";
            }
            else if (shipType == "Vessel - Fishing")
            {
                vessel->m_model = "trawler_22m.glbe";
            }
            else if (shipType == "Vessel - Sailing")
            {
                if (length < 13)
                {
                    vessel->m_model = "sailboat_8m.glbe";
                }
                else
                {
                    vessel->m_model = "sailboat_17m.glbe";
                }
            }
            else if (shipType.contains("Pleasure craft"))
            {
                if (length < 9)
                {
                    vessel->m_model = "speedboat_8m.glbe";
                }
                /*else if (length < 18)
                {
                    vessel->m_model = "yacht_10m.glbe";
                }*/
                else if (length < 32)
                {
                    vessel->m_model = "yacht_20m.glbe";
                }
                else
                {
                    vessel->m_model = "yacht_42m.glbe";
                }
            }
        }
    }
}

void AISGUI::on_vessels_cellDoubleClicked(int row, int column)
{
    if (column == VESSEL_COL_MMSI)
    {
        // Get MMSI of vessel in row double clicked
        QString mmsi = ui->vessels->item(row, VESSEL_COL_MMSI)->text();
        // Search for MMSI on www.vesselfinder.com
        QDesktopServices::openUrl(QUrl(QString("https://www.vesselfinder.com/vessels?name=%1").arg(mmsi)));
    }
    else if ((column == VESSEL_COL_LATITUDE) || (column == VESSEL_COL_LONGITUDE) || (column == VESSEL_COL_SHIP_TYPE))
    {
        // Get MMSI of vessel in row double clicked
        QString mmsi = ui->vessels->item(row, VESSEL_COL_MMSI)->text();
        // Find MMSI on Map
        FeatureWebAPIUtils::mapFind(mmsi);
    }
    else if (column == VESSEL_COL_IMO)
    {
        QString imo = ui->vessels->item(row, VESSEL_COL_IMO)->text();
        if (!imo.isEmpty() && (imo != "0"))
        {
            // Search for IMO on www.vesselfinder.com
            QDesktopServices::openUrl(QUrl(QString("https://www.vesselfinder.com/vessels?name=%1").arg(imo)));
        }
    }
    else if (column == VESSEL_COL_NAME)
    {
        QString name = ui->vessels->item(row, VESSEL_COL_NAME)->text();
        if (!name.isEmpty())
        {
            // Search for name on www.vesselfinder.com
            QDesktopServices::openUrl(QUrl(QString("https://www.vesselfinder.com/vessels?name=%1").arg(name)));
        }
    }
    else if (column == VESSEL_COL_DESTINATION)
    {
        QString destination = ui->vessels->item(row, VESSEL_COL_DESTINATION)->text();
        if (!destination.isEmpty())
        {
            // Find destination on Map
            FeatureWebAPIUtils::mapFind(destination);
        }
    }
}

// Table cells context menu
void AISGUI::vessels_customContextMenuRequested(QPoint pos)
{
    QTableWidgetItem *item =  ui->vessels->itemAt(pos);
    if (item)
    {
        int row = item->row();
        QString mmsi = ui->vessels->item(row, VESSEL_COL_MMSI)->text();
        QString imo = ui->vessels->item(row, VESSEL_COL_IMO)->text();
        QString name = ui->vessels->item(row, VESSEL_COL_NAME)->text();
        QVariant latitudeV = ui->vessels->item(row, VESSEL_COL_LATITUDE)->data(Qt::DisplayRole);
        QVariant longitudeV = ui->vessels->item(row, VESSEL_COL_LONGITUDE)->data(Qt::DisplayRole);
        QString destination = ui->vessels->item(row, VESSEL_COL_DESTINATION)->text();

        QMenu* tableContextMenu = new QMenu(ui->vessels);
        connect(tableContextMenu, &QMenu::aboutToHide, tableContextMenu, &QMenu::deleteLater);

        // Copy current cell

        QAction* copyAction = new QAction("Copy", tableContextMenu);
        const QString text = item->text();
        connect(copyAction, &QAction::triggered, this, [text]()->void {
            QClipboard *clipboard = QGuiApplication::clipboard();
            clipboard->setText(text);
        });
        tableContextMenu->addAction(copyAction);
        tableContextMenu->addSeparator();

        // View vessel on various websites

        QAction* mmsiAISHubAction = new QAction(QString("View MMSI %1 on aishub.net...").arg(mmsi), tableContextMenu);
        connect(mmsiAISHubAction, &QAction::triggered, this, [mmsi]()->void {
            QDesktopServices::openUrl(QUrl(QString("https://www.aishub.net/vessels?Ship%5Bmmsi%5D=%1&mmsi=%1").arg(mmsi)));
        });
        tableContextMenu->addAction(mmsiAISHubAction);

        QAction* mmsiAction = new QAction(QString("View MMSI %1 on vesselfinder.com...").arg(mmsi), tableContextMenu);
        connect(mmsiAction, &QAction::triggered, this, [mmsi]()->void {
            QDesktopServices::openUrl(QUrl(QString("https://www.vesselfinder.com/vessels?name=%1").arg(mmsi)));
        });
        tableContextMenu->addAction(mmsiAction);

        if (!imo.isEmpty())
        {
            QAction* imoAction = new QAction(QString("View IMO %1 on vesselfinder.com...").arg(imo), tableContextMenu);
            connect(imoAction, &QAction::triggered, this, [imo]()->void {
                QDesktopServices::openUrl(QUrl(QString("https://www.vesselfinder.com/vessels?name=%1").arg(imo)));
            });
            tableContextMenu->addAction(imoAction);
        }

        if (!name.isEmpty())
        {
            QAction* nameAction = new QAction(QString("View %1 on vesselfinder.com...").arg(name), tableContextMenu);
            connect(nameAction, &QAction::triggered, this, [name]()->void {
                QDesktopServices::openUrl(QUrl(QString("https://www.vesselfinder.com/vessels?name=%1").arg(name)));
            });
            tableContextMenu->addAction(nameAction);
        }

        // Find on Map
        if (!latitudeV.isNull())
        {
            tableContextMenu->addSeparator();

            QAction* findMapFeatureAction = new QAction(QString("Find MMSI %1 on map").arg(mmsi), tableContextMenu);
            connect(findMapFeatureAction, &QAction::triggered, this, [mmsi]()->void {
                FeatureWebAPIUtils::mapFind(mmsi);
            });
            tableContextMenu->addAction(findMapFeatureAction);
        }

        if (!destination.isEmpty())
        {
            tableContextMenu->addSeparator();

            QAction* findDestinationFeatureAction = new QAction(QString("Find %1 on map").arg(destination), tableContextMenu);
            connect(findDestinationFeatureAction, &QAction::triggered, this, [destination]()->void {
                FeatureWebAPIUtils::mapFind(destination);
            });
            tableContextMenu->addAction(findDestinationFeatureAction);
        }

        tableContextMenu->popup(ui->vessels->viewport()->mapToGlobal(pos));
    }
}
