/*
 * Copyright (c) 2022 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 "io/memory_file.h"

#include <cstdint>
#include <memory>

#include <base/containers/allocator.h>
#include <base/containers/shared_ptr.h>
#include <base/namespace.h>
#include <core/io/intf_file.h>
#include <core/log.h>
#include <core/namespace.h>

CORE_BEGIN_NAMESPACE()
using BASE_NS::CloneData;

uint64_t MemoryFileStorage::Write(uint64_t index, const void* buffer, uint64_t count)
{
    if (index >= buffer_.size()) {
        return 0;
    }
    if (CloneData(buffer_.data() + index, buffer_.size() - index, buffer, static_cast<size_t>(count))) {
        return count;
    }
    return 0;
}

MemoryFile::MemoryFile(BASE_NS::shared_ptr<MemoryFileStorage>&& buffer, Mode mode)
    : buffer_(BASE_NS::move(buffer)), mode_(mode)
{}

IFile::Mode MemoryFile::GetMode() const
{
    return mode_;
}

void MemoryFile::Close()
{}

uint64_t MemoryFile::Read(void* buffer, uint64_t count)
{
    if (mode_ == Mode::INVALID) {
        return {};
    }
    uint64_t toRead = count;
    if ((index_ + toRead) > buffer_->GetStorage().size()) {
        toRead = buffer_->GetStorage().size() - index_;
    }

    if (toRead > 0) {
        if (toRead <= SIZE_MAX) {
            if (CloneData(buffer,
                    static_cast<size_t>(count),
                    &(buffer_->GetStorage().data()[index_]),
                    static_cast<size_t>(toRead))) {
                index_ += toRead;
            }
        } else {
            CORE_ASSERT_MSG(false, "Unable to read chunks bigger than (SIZE_MAX) bytes.");
            toRead = 0;
        }
    }

    return toRead;
}

uint64_t MemoryFile::Write(const void* buffer, uint64_t count)
{
    if (mode_ == Mode::READ_WRITE) {
        const uint64_t requiredSize = index_ + count;
        if (requiredSize < index_ || requiredSize > SIZE_MAX) {
            return 0;
        }
        if (requiredSize > buffer_->Size()) {
            buffer_->Resize(static_cast<size_t>(requiredSize));
        }
        const uint64_t written = buffer_->Write(index_, buffer, count);
        index_ += written;
        return written;
    }
    return {};
}

uint64_t MemoryFile::Append(const void* buffer, uint64_t count, uint64_t /*chunkSize*/)
{
    if (mode_ == Mode::READ_WRITE) {
        auto exSize = buffer_->Size();
        if (count > SIZE_MAX - exSize) {
            return 0;
        }
        buffer_->Resize(exSize + count);
        return buffer_->Write(exSize, buffer, count);
    }
    return {};
}

uint64_t MemoryFile::GetLength() const
{
    return buffer_->GetStorage().size();
}

bool MemoryFile::Seek(uint64_t aOffset)
{
    if (aOffset < buffer_->GetStorage().size()) {
        index_ = aOffset;
        return true;
    }

    return false;
}

uint64_t MemoryFile::GetPosition() const
{
    return index_;
}
CORE_END_NAMESPACE()