/**
 * Copyright © 2017 IBM 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.
 */

#include "config.h"

#include "event_manager.hpp"

#include "event.hpp"
#include "event_serialize.hpp"

#include <filesystem>

namespace phosphor
{
namespace events
{

void Manager::create(const std::string& eventName,
                     const std::string& eventMessage,
                     const std::string& objectPath,
                     const std::string& propertyName,
                     const std::string& propertyValue)
{
    using namespace std::string_literals;
    namespace fs = std::filesystem;

    auto msg = eventMessage;
    std::vector<std::string> additionalData;

    auto propVal = propertyName + "=" + propertyValue;
    auto path = "path="s + objectPath;

    additionalData.push_back(std::move(path));
    additionalData.push_back(std::move(propVal));

    auto& eventQueue = eventMap[eventName];

    // get the last event entry for this event
    // to generate the id.
    auto id = 0;
    if (eventQueue.size() > 0)
    {
        fs::path eventPath(eventQueue.back()->objectPath);
        id = std::stoi(std::string(eventPath.filename().c_str()));
        id++;
    }

    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
                  std::chrono::system_clock::now().time_since_epoch())
                  .count();

    auto objPath = std::string(OBJ_EVENT) + '/' + eventName + '/' +
                   std::to_string(id);

    // check for capping of the events,if cap reached then erase the oldest
    // event.
    if (eventQueue.size() == MAX_EVENTS)
    {
        fs::path eventPath(EVENTS_PERSIST_PATH);
        eventPath /= eventName;
        eventPath /= std::to_string(eventQueue.front()->timestamp());
        eventQueue.pop();
        std::error_code ec;
        fs::remove(eventPath, ec);
    }

    auto event = std::make_unique<Entry>(objPath,
                                         ms, // Milliseconds since 1970
                                         std::move(msg),
                                         std::move(additionalData));
    serialize(*event, eventName);
    eventQueue.push(std::move(event));
}

void Manager::restore()
{
    if (!fs::exists(EVENTS_PERSIST_PATH) || fs::is_empty(EVENTS_PERSIST_PATH))
    {
        return;
    }

    for (auto& eventFile :
         fs::recursive_directory_iterator(EVENTS_PERSIST_PATH))
    {
        if (!fs::is_regular_file(eventFile))
        {
            continue;
        }

        EventQueue events;

        auto eventPath = eventFile.path().string();
        auto pos1 = eventPath.rfind("/");
        auto pos2 = eventPath.rfind("/", pos1 - 1) + 1;
        auto eventName = eventPath.substr(pos2, (pos1 - pos2));
        auto validEvent = false;
        auto timestamp = eventFile.path().filename().string();
        auto tsNum = std::stoll(timestamp);
        auto objPath = std::string(OBJ_EVENT) + '/' + eventName + '/' +
                       timestamp;

        auto event = std::make_unique<Entry>(objPath, tsNum);
        if (deserialize(eventFile.path(), *event))
        {
            event->emit_object_added();
            events.push(std::move(event));
            validEvent = true;
        }

        if (validEvent)
        {
            eventMap[eventName] = std::move(events);
        }
    }
}

Manager& getManager()
{
    static Manager mgr;
    return mgr;
}

} // namespace events
} // namespace phosphor
