// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2021 Juan Palacios <jpalaciosdev@gmail.com>

#include "memorytemp.h"

#include "../gpusensorprovider.h"
#include "../graphitemprofilepart.h"
#include "../graphitemxmlparser.h"
#include "../sensor.h"
#include "common/fileutils.h"
#include "common/stringutils.h"
#include "core/info/igpuinfo.h"
#include "core/info/vendor.h"
#include "core/iprofilepart.h"
#include "core/iprofilepartxmlparser.h"
#include "core/profilepartprovider.h"
#include "core/profilepartxmlparserprovider.h"
#include "core/sysfsdatasource.h"
#include <filesystem>
#include <memory>
#include <optional>
#include <spdlog/spdlog.h>
#include <string>
#include <units.h>
#include <utility>
#include <vector>

namespace AMD::MemoryTemp {

class Provider final : public IGPUSensorProvider::IProvider
{
 public:
  std::vector<std::unique_ptr<ISensor>>
  provideGPUSensors(IGPUInfo const &gpuInfo, ISWInfo const &) const override
  {
    if (gpuInfo.vendor() != Vendor::AMD)
      return {};

    auto path = Utils::File::findHWMonXDirectory(gpuInfo.path().sys / "hwmon");
    if (!path.has_value())
      return {};

    auto tempInput = path.value() / "temp3_input";
    if (!Utils::File::isSysFSEntryValid(tempInput))
      return {};

    int value;
    auto data = Utils::File::readFileLines(tempInput);
    if (!Utils::String::toNumber<int>(value, data.front())) {
      SPDLOG_WARN("Unknown data format on {}", tempInput.string());
      SPDLOG_DEBUG(data.front());
      return {};
    }

    std::optional<
        std::pair<units::temperature::celsius_t, units::temperature::celsius_t>>
        range;

    data = Utils::File::readFileLines(path.value() / "temp3_crit");
    if (!data.empty()) {
      if (Utils::String::toNumber<int>(value, data.front()) &&
          // do not use bogus values, see #103
          (value >= 0 && value < 150000)) {
        range = {units::temperature::celsius_t(0),
                 units::temperature::celsius_t(value / 1000.0)};
      }
    }

    std::vector<std::unique_ptr<IDataSource<int>>> dataSources;
    dataSources.emplace_back(std::make_unique<SysFSDataSource<int>>(
        tempInput, [](std::string const &data, int &output) {
          int value;
          Utils::String::toNumber<int>(value, data);
          output = value / 1000;
        }));

    std::vector<std::unique_ptr<ISensor>> sensors;
    sensors.emplace_back(
        std::make_unique<Sensor<units::temperature::celsius_t, int>>(
            AMD::MemoryTemp::ItemID, std::move(dataSources), std::move(range)));

    return sensors;
  }
};

static bool register_()
{
  GPUSensorProvider::registerProvider(
      std::make_unique<AMD::MemoryTemp::Provider>());

  ProfilePartProvider::registerProvider(AMD::MemoryTemp::ItemID, []() {
    return std::make_unique<GraphItemProfilePart>(AMD::MemoryTemp::ItemID,
                                                  "darkred");
  });

  ProfilePartXMLParserProvider::registerProvider(AMD::MemoryTemp::ItemID, []() {
    return std::make_unique<GraphItemXMLParser>(AMD::MemoryTemp::ItemID);
  });

  return true;
}

static bool const registered_ = register_();

} // namespace AMD::MemoryTemp
