/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * 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 <android-base/stringprintf.h>
#include <binder/Parcel.h>
#include <gui/LayerMetadata.h>
#include <inttypes.h>

#include "android/view/LayerMetadataKey.h"

using android::base::StringPrintf;

namespace android {

LayerMetadata::LayerMetadata() = default;

LayerMetadata::LayerMetadata(std::unordered_map<uint32_t, std::vector<uint8_t>> map)
      : mMap(std::move(map)) {}

LayerMetadata::LayerMetadata(const LayerMetadata& other) = default;

LayerMetadata::LayerMetadata(LayerMetadata&& other) = default;

bool LayerMetadata::merge(const LayerMetadata& other, bool eraseEmpty) {
    bool changed = false;
    for (const auto& entry : other.mMap) {
        auto it = mMap.find(entry.first);
        if (it != mMap.cend() && it->second != entry.second) {
            if (eraseEmpty && entry.second.empty()) {
                mMap.erase(it);
            } else {
                it->second = entry.second;
            }
            changed = true;
        } else if (it == mMap.cend() && !entry.second.empty()) {
            mMap[entry.first] = entry.second;
            changed = true;
        }
    }
    return changed;
}

status_t LayerMetadata::writeToParcel(Parcel* parcel) const {
    parcel->writeInt32(static_cast<int>(mMap.size()));
    status_t status = OK;
    for (const auto& entry : mMap) {
        status = parcel->writeUint32(entry.first);
        if (status != OK) {
            break;
        }
        status = parcel->writeByteVector(entry.second);
        if (status != OK) {
            break;
        }
    }
    return status;
}

status_t LayerMetadata::readFromParcel(const Parcel* parcel) {
    int size = parcel->readInt32();
    status_t status = OK;
    mMap.clear();
    for (int i = 0; i < size; ++i) {
        uint32_t key = parcel->readUint32();
        status = parcel->readByteVector(&mMap[key]);
        if (status != OK) {
            break;
        }
    }
    return status;
}

LayerMetadata& LayerMetadata::operator=(const LayerMetadata& other) {
    mMap = other.mMap;
    return *this;
}

LayerMetadata& LayerMetadata::operator=(LayerMetadata&& other) {
    mMap = std::move(other.mMap);
    return *this;
}

bool LayerMetadata::has(uint32_t key) const {
    return mMap.count(key);
}

int32_t LayerMetadata::getInt32(uint32_t key, int32_t fallback) const {
    if (!has(key)) return fallback;
    const std::vector<uint8_t>& data = mMap.at(key);
    if (data.size() < sizeof(uint32_t)) return fallback;
    Parcel p;
    p.setData(data.data(), data.size());
    return p.readInt32();
}

void LayerMetadata::setInt32(uint32_t key, int32_t value) {
    std::vector<uint8_t>& data = mMap[key];
    Parcel p;
    p.writeInt32(value);
    data.resize(p.dataSize());
    memcpy(data.data(), p.data(), p.dataSize());
}

std::optional<int64_t> LayerMetadata::getInt64(uint32_t key) const {
    if (!has(key)) return std::nullopt;
    const std::vector<uint8_t>& data = mMap.at(key);
    if (data.size() < sizeof(int64_t)) return std::nullopt;
    Parcel p;
    p.setData(data.data(), data.size());
    return p.readInt64();
}

std::string LayerMetadata::itemToString(uint32_t key, const char* separator) const {
    if (!has(key)) return std::string();
    switch (static_cast<view::LayerMetadataKey>(key)) {
        case view::LayerMetadataKey::METADATA_OWNER_UID:
            return StringPrintf("ownerUID%s%d", separator, getInt32(key, 0));
        case view::LayerMetadataKey::METADATA_WINDOW_TYPE:
            return StringPrintf("windowType%s%d", separator, getInt32(key, 0));
        case view::LayerMetadataKey::METADATA_TASK_ID:
            return StringPrintf("taskId%s%d", separator, getInt32(key, 0));
        case view::LayerMetadataKey::METADATA_OWNER_PID:
            return StringPrintf("ownerPID%s%d", separator, getInt32(key, 0));
        case view::LayerMetadataKey::METADATA_DEQUEUE_TIME:
            return StringPrintf("dequeueTime%s%" PRId64, separator, *getInt64(key));
        case view::LayerMetadataKey::METADATA_GAME_MODE:
            return StringPrintf("gameMode%s%d", separator, getInt32(key, 0));
        default:
            return StringPrintf("%d%s%dbytes", key, separator,
                                static_cast<int>(mMap.at(key).size()));
    }
}

} // namespace android
