/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * 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 "common_event_sticky_manager.h"
#include "errors.h"
#include "event_log_wrapper.h"

namespace OHOS {
namespace EventFwk {
constexpr int32_t LENGTH = 80;

int CommonEventStickyManager::FindStickyEvents(
    const SubscribeInfoPtr &subscribeInfo, std::vector<CommonEventRecordPtr> &commonEventRecords)
{
    EVENT_LOGD(LOG_TAG_STICKY, "enter");

    if (subscribeInfo == nullptr) {
        EVENT_LOGE(LOG_TAG_STICKY, "subscribeInfo is null");
        return ERR_INVALID_VALUE;
    }

    auto events = subscribeInfo->GetMatchingSkills().GetEvents();
    if (events.size() == 0) {
        EVENT_LOGW(LOG_TAG_STICKY, "No subscribed events");
        return ERR_INVALID_VALUE;
    }

    FindStickyEventsLocked(events, commonEventRecords);

    return ERR_OK;
}

bool CommonEventStickyManager::GetStickyCommonEvent(const std::string &event, CommonEventData &eventData)
{
    EVENT_LOGD(LOG_TAG_STICKY, "enter");

    if (event.empty()) {
        EVENT_LOGE(LOG_TAG_STICKY, "Invalid event name");
        return false;
    }

    return GetStickyCommonEventLocked(event, eventData);
}

int CommonEventStickyManager::UpdateStickyEvent(const CommonEventRecord &eventRecord)
{
    EVENT_LOGD(LOG_TAG_STICKY, "enter");

    auto commonEventRecordPtr = std::make_shared<CommonEventRecord>(eventRecord);
    if (commonEventRecordPtr == nullptr) {
        EVENT_LOGE(LOG_TAG_STICKY, "Failed to create CommonEventRecord");
        return ERR_INVALID_VALUE;
    }

    std::string event = commonEventRecordPtr->commonEventData->GetWant().GetAction();

    return UpdateStickyEventLocked(event, commonEventRecordPtr);
}
#ifdef CEM_SUPPORT_DUMP
void CommonEventStickyManager::DumpState(
    const std::string &event, const int32_t &userId, std::vector<std::string> &state)
{
    EVENT_LOGD(LOG_TAG_STICKY, "enter");

    std::vector<CommonEventRecordPtr> records;

    std::lock_guard<ffrt::mutex> lock(mutex_);

    GetStickyCommonEventRecords(event, userId, records);

    if (records.size() == 0) {
        state.emplace_back("Sticky Events:\tNo information");
        return;
    }

    size_t num = 0;
    for (auto record : records) {
        num++;

        std::string no = std::to_string(num);
        if (num == 1) {
            no = "Sticky Events:\tTotal " + std::to_string(records.size()) + " information\nNO " + no + "\n";
        } else {
            no = "NO " + no + "\n";
        }

        char systime[LENGTH];
        strftime(systime, sizeof(char) * LENGTH, "%Y%m%d %I:%M %p", &record->recordTime);

        std::string recordTime = "\tTime: " + std::string(systime) + "\n";
        std::string pid = "\tPID: " + std::to_string(record->eventRecordInfo.pid) + "\n";
        std::string uid = "\tUID: " + std::to_string(record->eventRecordInfo.uid) + "\n";
        std::string bundleName = "\tBundleName: " + record->eventRecordInfo.bundleName + "\n";

        std::string permission = "\tRequiredPermission: ";
        std::string separator;
        size_t permissionNum = 0;
        for (auto permissionVec : record->publishInfo->GetSubscriberPermissions()) {
            if (permissionNum == 0) {
                separator = "";
            } else {
                separator = ", ";
            }
            permission = permission + separator + permissionVec;
            permissionNum++;
        }
        permission = permission + "\n";

        std::string isSticky;
        if (record->publishInfo->IsSticky()) {
            isSticky = "\tIsSticky: true\n";
        } else {
            isSticky = "\tIsSticky: false\n";
        }

        std::string isOrdered;
        if (record->publishInfo->IsOrdered()) {
            isOrdered = "\tIsOrdered: true\n";
        } else {
            isOrdered = "\tIsOrdered: false\n";
        }
        std::string isSystemEvent = record->isSystemEvent ? "true" : "false";
        isSystemEvent = "\tIsSystemEvent: " + isSystemEvent + "\n";

        std::string action = "\t\tAction: " + record->commonEventData->GetWant().GetAction() + "\n";

        std::string entities = "\t\tEntity: ";
        size_t entityNum = 0;
        for (auto entitiesVec : record->commonEventData->GetWant().GetEntities()) {
            if (entityNum == 0) {
                separator = "";
            } else {
                separator = ", ";
            }
            entities = entities + separator + entitiesVec;
            entityNum++;
        }
        entities = entities + "\n";

        std::string scheme = "\t\tScheme: " + record->commonEventData->GetWant().GetScheme() + "\n";
        std::string uri = "\t\tUri: " + record->commonEventData->GetWant().GetUriString() + "\n";
        std::string flags = "\t\tFlags: " + std::to_string(record->commonEventData->GetWant().GetFlags()) + "\n";
        std::string type = "\t\tType: " + record->commonEventData->GetWant().GetType() + "\n";
        std::string bundle = "\t\tBundleName: " + record->commonEventData->GetWant().GetBundle() + "\n";
        std::string ability =
            "\t\tAbilityName: " + record->commonEventData->GetWant().GetElement().GetAbilityName() + "\n";

        std::string want = "\tWant:\n" + action + entities + scheme + uri + flags + type + bundle + ability;
        std::string code = "\tCode: " + std::to_string(record->commonEventData->GetCode()) + "\n";
        std::string data = "\tData: " + record->commonEventData->GetData() + "\n";

        std::string dumpInfo = no + recordTime + pid + uid + bundleName + permission + isSticky + isOrdered +
                               isSystemEvent + want + code + data;

        state.emplace_back(dumpInfo);
    }
}
#endif
void CommonEventStickyManager::FindStickyEventsLocked(
    const std::vector<std::string> &events, std::vector<CommonEventRecordPtr> &commonEventRecords)
{
    std::lock_guard<ffrt::mutex> lock(mutex_);

    for (auto event : events) {
        auto it = commonEventRecords_.find(event);
        if (it != commonEventRecords_.end()) {
            commonEventRecords.emplace_back(it->second);
        }
    }
}

bool CommonEventStickyManager::GetStickyCommonEventLocked(const std::string &event, CommonEventData &eventData)
{
    std::lock_guard<ffrt::mutex> lock(mutex_);

    auto it = commonEventRecords_.find(event);
    if (it != commonEventRecords_.end()) {
        eventData = *(it->second->commonEventData);
        return true;
    }

    return false;
}

int CommonEventStickyManager::UpdateStickyEventLocked(const std::string &event, const CommonEventRecordPtr &record)
{
    if (event.empty()) {
        EVENT_LOGE(LOG_TAG_STICKY, "Invalid event name");
        return ERR_INVALID_VALUE;
    }

    if (record == nullptr) {
        EVENT_LOGE(LOG_TAG_STICKY, "Invalid common event record");
        return ERR_INVALID_VALUE;
    }

    std::lock_guard<ffrt::mutex> lock(mutex_);

    commonEventRecords_[event] = record;

    return ERR_OK;
}

void CommonEventStickyManager::GetStickyCommonEventRecords(
    const std::string &event, const int32_t &userId, std::vector<CommonEventRecordPtr> &records)
{
    if (event.empty()) {
        for (auto record : commonEventRecords_) {
            if ((userId == ALL_USER) || (record.second->userId == userId)) {
                records.emplace_back(record.second);
            }
        }
    } else {
        auto recordItem = commonEventRecords_.find(event);
        if (recordItem == commonEventRecords_.end()) {
            return;
        }
        if ((userId == ALL_USER) || (userId == recordItem->second->userId)) {
            records.emplace_back(recordItem->second);
        }
    }
}

int32_t CommonEventStickyManager::RemoveStickyCommonEvent(const std::string &event, uint32_t callerUid)
{
    std::lock_guard<ffrt::mutex> lock(mutex_);

    auto it = commonEventRecords_.find(event);
    if (it != commonEventRecords_.end()) {
        if (it->second->eventRecordInfo.uid == callerUid) {
            commonEventRecords_.erase(it);
        }
    }
    return ERR_OK;
}
}  // namespace EventFwk
}  // namespace OHOS