/*
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#pragma once
#include "cpu.hpp"
#include "dimm.hpp"
#include "pcieslot.hpp"
#include "smbios_mdrv2.hpp"
#include "system.hpp"

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/container/flat_map.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <sdbusplus/server.hpp>
#include <sdbusplus/timer.hpp>
#include <xyz/openbmc_project/Smbios/MDR_V2/server.hpp>

#include <filesystem>
#include <memory>

namespace phosphor
{
namespace smbios
{

using RecordVariant =
    std::variant<std::string, uint64_t, uint32_t, uint16_t, uint8_t>;

static constexpr const char* defaultObjectPath =
    "/xyz/openbmc_project/Smbios/MDR_V2";
static constexpr const char* smbiosInterfaceName =
    "xyz.openbmc_project.Smbios.GetRecordType";
static constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
static constexpr const char* mapperPath = "/xyz/openbmc_project/object_mapper";
static constexpr const char* mapperInterface =
    "xyz.openbmc_project.ObjectMapper";
static constexpr const char* defaultInventoryPath =
    "/xyz/openbmc_project/inventory/system";
static constexpr const char* systemInterface =
    "xyz.openbmc_project.Inventory.Item.System";
static constexpr const char* boardInterface =
    "xyz.openbmc_project.Inventory.Item.Board";
constexpr const int limitEntryLen = 0xff;

// Avoid putting multiple interfaces with same name on same object
static std::string placeGetRecordType(const std::string& objectPath)
{
    if (objectPath != defaultObjectPath)
    {
        // Place GetRecordType interface on object itself, not parent
        return objectPath;
    }

    std::filesystem::path path(objectPath);

    // As there is only one default, safe to place it on the common parent
    return path.parent_path().string();
}

class MDRV2 :
    sdbusplus::server::object_t<
        sdbusplus::server::xyz::openbmc_project::smbios::MDRV2>
{
  public:
    MDRV2() = delete;
    MDRV2(const MDRV2&) = delete;
    MDRV2& operator=(const MDRV2&) = delete;
    MDRV2(MDRV2&&) = delete;
    MDRV2& operator=(MDRV2&&) = delete;

    virtual ~MDRV2()
    {
        if (smbiosInterface)
        {
            if (objServer)
            {
                // Must manually undo add_interface()
                objServer->remove_interface(smbiosInterface);
            }
        }
    }

    MDRV2(std::shared_ptr<boost::asio::io_context> io,
          std::shared_ptr<sdbusplus::asio::connection> conn,
          std::shared_ptr<sdbusplus::asio::object_server> obj,
          std::string filePath, std::string objectPath,
          std::string inventoryPath) :
        sdbusplus::server::object_t<
            sdbusplus::server::xyz::openbmc_project::smbios::MDRV2>(
            *conn, objectPath.c_str()),
        timer(*io), bus(conn), objServer(std::move(obj)),
        smbiosInterface(objServer->add_interface(placeGetRecordType(objectPath),
                                                 smbiosInterfaceName)),
        smbiosFilePath(std::move(filePath)),
        smbiosObjectPath(std::move(objectPath)),
        smbiosInventoryPath(std::move(inventoryPath))
    {
        lg2::info("SMBIOS data file path: {F}", "F", smbiosFilePath);
        lg2::info("SMBIOS control object: {O}", "O", smbiosObjectPath);
        lg2::info("SMBIOS inventory path: {I}", "I", smbiosInventoryPath);

        smbiosDir.agentVersion = smbiosAgentVersion;
        smbiosDir.dirVersion = smbiosDirVersion;
        smbiosDir.dirEntries = 1;
        directoryEntries(smbiosDir.dirEntries);
        smbiosDir.status = 1;
        smbiosDir.remoteDirVersion = 0;

        std::copy(smbiosTableId.begin(), smbiosTableId.end(),
                  smbiosDir.dir[smbiosDirIndex].common.id.dataInfo);

        smbiosDir.dir[smbiosDirIndex].dataStorage = smbiosTableStorage;

        agentSynchronizeData();

        smbiosInterface->register_method("GetRecordType", [this](size_t type) {
            return getRecordType(type);
        });
        smbiosInterface->initialize();
    }

    std::vector<uint8_t> getDirectoryInformation(uint8_t dirIndex) override;

    std::vector<uint8_t> getDataInformation(uint8_t idIndex) override;

    bool sendDirectoryInformation(
        uint8_t dirVersion, uint8_t dirIndex, uint8_t returnedEntries,
        uint8_t remainingEntries, std::vector<uint8_t> dirEntry) override;

    std::vector<uint8_t> getDataOffer() override;

    bool sendDataInformation(uint8_t idIndex, uint8_t flag, uint32_t dataLen,
                             uint32_t dataVer, uint32_t timeStamp) override;

    int findIdIndex(std::vector<uint8_t> dataInfo) override;

    bool agentSynchronizeData() override;

    std::vector<uint32_t>
        synchronizeDirectoryCommonData(uint8_t idIndex, uint32_t size) override;

    uint8_t directoryEntries(uint8_t value) override;

    std::vector<boost::container::flat_map<std::string, RecordVariant>>
        getRecordType(size_t type);

  private:
    boost::asio::steady_timer timer;

    std::shared_ptr<sdbusplus::asio::connection> bus;
    std::shared_ptr<sdbusplus::asio::object_server> objServer;

    Mdr2DirStruct smbiosDir;

    bool readDataFromFlash(MDRSMBIOSHeader* mdrHdr, uint8_t* data);
    bool checkSMBIOSVersion(uint8_t* dataIn);

    const std::array<uint8_t, 16> smbiosTableId{
        40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 0x42};
    uint8_t smbiosTableStorage[smbiosTableStorageSize] = {};

    bool smbiosIsUpdating(uint8_t index);
    bool smbiosIsAvailForUpdate(uint8_t index);
    inline uint8_t smbiosValidFlag(uint8_t index);
    void systemInfoUpdate(void);

    std::optional<size_t> getTotalCpuSlot(void);
    std::optional<size_t> getTotalDimmSlot(void);
    std::optional<size_t> getTotalPcieSlot(void);
    std::vector<std::unique_ptr<Cpu>> cpus;
    std::vector<std::unique_ptr<Dimm>> dimms;
    std::vector<std::unique_ptr<Pcie>> pcies;
    std::unique_ptr<System> system;
    std::shared_ptr<sdbusplus::asio::dbus_interface> smbiosInterface;

    std::string smbiosFilePath;
    std::string smbiosObjectPath;
    std::string smbiosInventoryPath;
    std::unique_ptr<sdbusplus::bus::match_t> motherboardConfigMatch;
};

} // namespace smbios
} // namespace phosphor
