/*
 * Strawberry Music Player
 * This file was part of Clementine.
 * Copyright 2012, David Sansome <me@davidsansome.com>
 * Copyright 2019-2025, Jonas Kvinge <jonas@jkvinge.net>
 *
 * Strawberry 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 3 of the License, or
 * (at your option) any later version.
 *
 * Strawberry 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 Strawberry.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "moodbarpipeline.h"

#include <cstdlib>
#include <cmath>

#include <memory>

#include <glib.h>
#include <glib-object.h>
#include <gst/gst.h>

#include <QObject>
#include <QCoreApplication>
#include <QThread>
#include <QString>
#include <QUrl>

#include "core/logging.h"
#include "core/signalchecker.h"
#include "utilities/threadutils.h"
#include "moodbar/moodbarbuilder.h"
#ifdef HAVE_GSTFASTSPECTRUM
#  include "engine/gstfastspectrum.h"
#endif

using namespace Qt::Literals::StringLiterals;
using std::make_unique;

namespace {
constexpr int kBands = 128;
}

MoodbarPipeline::MoodbarPipeline(const QUrl &url, QObject *parent)
    : QObject(parent),
      url_(url),
      pipeline_(nullptr),
      convert_element_(nullptr),
      success_(false),
      running_(false) {}

MoodbarPipeline::~MoodbarPipeline() {

  Cleanup();

}

GstElement *MoodbarPipeline::CreateElement(const QByteArray &factory_name) {

  GstElement *element = gst_element_factory_make(factory_name.constData(), nullptr);

  if (element) {
    gst_bin_add(GST_BIN(pipeline_), element);
  }
  else {
    qLog(Warning) << "Unable to create gstreamer element" << factory_name;
  }

  return element;

}

QByteArray MoodbarPipeline::ToGstUrl(const QUrl &url) {

  if (url.isLocalFile() && !url.host().isEmpty()) {
    QString str = "file:////"_L1 + url.host() + url.path();
    return str.toUtf8();
  }

  return url.toEncoded();

}

void MoodbarPipeline::Start() {

  Q_ASSERT(QThread::currentThread() == thread());
  Q_ASSERT(QThread::currentThread() != qApp->thread());

  Utilities::SetThreadIOPriority(Utilities::IoPriority::IOPRIO_CLASS_IDLE);

  if (pipeline_) {
    return;
  }

  pipeline_ = gst_pipeline_new("moodbar-pipeline");

  GstElement *decodebin = CreateElement("uridecodebin");
  GstElement *convert_element = CreateElement("audioconvert");
#ifdef HAVE_GSTFASTSPECTRUM
  GstElement *spectrum = CreateElement("strawberry-fastspectrum");
#else
  GstElement *spectrum = CreateElement("spectrum");
#endif
  GstElement *fakesink = CreateElement("fakesink");

  if (!decodebin || !convert_element || !spectrum || !fakesink) {
    gst_object_unref(GST_OBJECT(pipeline_));
    pipeline_ = nullptr;
    Q_EMIT Finished(false);
    return;
  }

  // Join them together
  if (!gst_element_link_many(convert_element, spectrum, fakesink, nullptr)) {
    qLog(Error) << "Failed to link elements";
    gst_object_unref(GST_OBJECT(pipeline_));
    pipeline_ = nullptr;
    Q_EMIT Finished(false);
    return;
  }

  convert_element_ = convert_element;

  builder_ = make_unique<MoodbarBuilder>();

  // Set properties

  QByteArray gst_url = ToGstUrl(url_);
  g_object_set(decodebin, "uri", gst_url.constData(), nullptr);
  g_object_set(spectrum, "bands", kBands, nullptr);

#ifdef HAVE_GSTFASTSPECTRUM
  GstStrawberryFastSpectrum *fastspectrum = reinterpret_cast<GstStrawberryFastSpectrum*>(spectrum);
  // This callback runs on a GStreamer streaming thread.
  // Skip processing once the pipeline has been stopped: the worker thread may already be tearing down builder_ in Finish(), and touching it here would be a data race / use-after-free.
  fastspectrum->output_callback = [this](double *magnitudes, const int size) {
    if (running_ && builder_) builder_->AddFrame(magnitudes, size);
  };
#else
  GObjectClass *spectrum_class = G_OBJECT_GET_CLASS(spectrum);
  if (g_object_class_find_property(spectrum_class, "message")) {
    g_object_set(spectrum, "message", TRUE, nullptr);
  }
  else if (g_object_class_find_property(spectrum_class, "post-messages")) {
    g_object_set(spectrum, "post-messages", TRUE, nullptr);
  }
#endif

  // Connect signals
  CHECKED_GCONNECT(decodebin, "pad-added", &NewPadCallback, this);
  GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline_));
  if (bus) {
    gst_bus_set_sync_handler(bus, BusCallbackSync, this, nullptr);
    gst_object_unref(bus);
  }

  // Start playing
  running_ = true;
  gst_element_set_state(pipeline_, GST_STATE_PLAYING);

}

void MoodbarPipeline::ReportError(GstMessage *msg) {

  GError *error = nullptr;
  gchar *debugs = nullptr;

  gst_message_parse_error(msg, &error, &debugs);
  QString message;
  if (error) {
    message = QString::fromLocal8Bit(error->message);
    g_error_free(error);
  }
  g_free(debugs);

  qLog(Error) << "Error processing" << url_ << ":" << message;

}

void MoodbarPipeline::NewPadCallback(GstElement *element, GstPad *pad, gpointer self) {

  Q_UNUSED(element)

  MoodbarPipeline *instance = reinterpret_cast<MoodbarPipeline*>(self);

  if (!instance->running_) {
    qLog(Warning) << "Received gstreamer callback after pipeline has stopped.";
    return;
  }

  GstPad *const audiopad = gst_element_get_static_pad(instance->convert_element_, "sink");
  if (!audiopad) return;

  if (GST_PAD_IS_LINKED(audiopad)) {
    qLog(Warning) << "audiopad is already linked, unlinking old pad";
    gst_pad_unlink(audiopad, GST_PAD_PEER(audiopad));
  }

  gst_pad_link(pad, audiopad);
  gst_object_unref(audiopad);

  int rate = 0;
  GstCaps *caps = gst_pad_get_current_caps(pad);
  if (caps) {
    GstStructure *structure = gst_caps_get_structure(caps, 0);
    if (structure) {
      gst_structure_get_int(structure, "rate", &rate);
    }
    gst_caps_unref(caps);
  }

  if (instance->builder_) {
    instance->builder_->Init(kBands, rate);
  }
  else {
    qLog(Error) << "Builder does not exist";
  }

}

GstBusSyncReply MoodbarPipeline::BusCallbackSync(GstBus *bus, GstMessage *message, gpointer self) {

  Q_UNUSED(bus)

  MoodbarPipeline *instance = reinterpret_cast<MoodbarPipeline*>(self);

  if (!instance->running_) return GST_BUS_PASS;

  switch (GST_MESSAGE_TYPE(message)) {
    case GST_MESSAGE_EOS:
      instance->Stop(true);
      break;

    case GST_MESSAGE_ERROR:
      instance->ReportError(message);
      instance->Stop(false);
      break;

#ifndef HAVE_GSTFASTSPECTRUM
    case GST_MESSAGE_ELEMENT:{
      const GstStructure *s = gst_message_get_structure(message);
      if (s && gst_structure_has_name(s, "spectrum") && instance->builder_) {
        const GValue *magnitudes_val = gst_structure_get_value(s, "magnitude");
        if (magnitudes_val) {
          const guint n = gst_value_list_get_size(magnitudes_val);
          double mags[kBands]{};
          const guint count = n <= static_cast<guint>(kBands) ? n : static_cast<guint>(kBands);
          for (guint i = 0; i < count; ++i) {
            const GValue *v = gst_value_list_get_value(magnitudes_val, i);
            mags[i] = std::pow(10.0, static_cast<double>(g_value_get_float(v)) / 10.0);
          }
          instance->builder_->AddFrame(mags, static_cast<int>(count));
        }
      }
      return GST_BUS_DROP;
    }
#endif

    default:
      break;
  }

  return GST_BUS_PASS;

}

void MoodbarPipeline::Stop(const bool success) {

  running_ = false;

  QMetaObject::invokeMethod(this, "Finish", Qt::QueuedConnection, Q_ARG(bool, success));

}

void MoodbarPipeline::Finish(const bool success) {

  Q_ASSERT(QThread::currentThread() == thread());
  Q_ASSERT(QThread::currentThread() != qApp->thread());

  success_ = success;

  // Drive the pipeline to GST_STATE_NULL first so the streaming threads (which may still be invoking the spectrum output callback) are joined before we read and reset builder_.
  // Doing this the other way around races the callback against builder_.reset() and is a use-after-free.
  Cleanup();

  if (builder_) {
    data_ = builder_->Finish(1000);
    builder_.reset();
  }

  Q_EMIT Finished(success);

}

void MoodbarPipeline::Shutdown() {

  Q_ASSERT(QThread::currentThread() == thread());

  Cleanup();

}

void MoodbarPipeline::Cleanup() {

  running_ = false;

  if (pipeline_) {
    GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline_));
    if (bus) {
      gst_bus_set_sync_handler(bus, nullptr, nullptr, nullptr);
      gst_object_unref(bus);
    }

    gst_element_set_state(pipeline_, GST_STATE_NULL);
    gst_object_unref(pipeline_);
    pipeline_ = nullptr;
  }

}
