/*
 * 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.
 */
#ifndef PKG_MANAGER_IMPL_H
#define PKG_MANAGER_IMPL_H

#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include "pkg_lz4file.h"
#include "pkg_manager.h"
#include "pkg_pkgfile.h"
#include "pkg_stream.h"
#include "pkg_utils.h"

namespace Hpackage {
class PkgManagerImpl : public PkgManager {
public:
    PkgManagerImpl();

    ~PkgManagerImpl() override;

    void RegisterPkgFileCreator(const std::string &fileType, PkgFileConstructor constructor) override;

    int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
        std::vector<std::pair<std::string, ZipFileInfo>> &files) override;

    int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
        std::vector<std::pair<std::string, ComponentInfo>> &files) override;

    int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
        std::vector<std::pair<std::string, Lz4FileInfo>> &files) override;

    int32_t VerifyPackage(const std::string &packagePath, const std::string &keyPath, const std::string &version,
        const PkgBuffer &digest, VerifyCallback cb) override;

    int32_t LoadPackage(const std::string &packagePath, const std::string &keyPath,
        std::vector<std::string> &fileIds) override;

    int32_t ExtractFile(const std::string &path, PkgManager::StreamPtr output) override;

    const FileInfo *GetFileInfo(const std::string &path) override;

    const PkgInfo *GetPackageInfo(const std::string &packagePath) override;

    PkgEntryPtr GetPkgEntry(const std::string &path);

    int32_t DecompressBuffer(FileInfoPtr info, const PkgBuffer &buffer, StreamPtr stream) const override;

    int32_t CompressBuffer(FileInfoPtr info, const PkgBuffer &buffer, StreamPtr stream) const override;

    int32_t LoadPackageWithoutUnPack(const std::string &packagePath, std::vector<std::string> &fileIds) override;

    int32_t LoadPackageWithStream(const std::string &packagePath, const std::string &keyPath,
        std::vector<std::string> &fileIds, uint8_t type, StreamPtr stream) override;

    int32_t ParsePackage(StreamPtr stream, std::vector<std::string> &fileIds, int32_t type) override;

    int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName, const PkgBuffer &buffer) override;

    int32_t CreatePkgStream(PkgStreamPtr &stream, const std::string &fileName,
        PkgStream::ExtractFileProcessor processor, const void *context) override;

    int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName,
        uint64_t fileLen, Updater::RingBuffer *buffer) override;

    int32_t VerifyOtaPackage(const std::string &packagePath) override;

    int32_t VerifyBinFile(const std::string &packagePath, const std::string &keyPath,
        const std::string &version, const PkgBuffer &digest) override;

    void SetPkgDecodeProgress(PkgDecodeProgress decodeProgress) override
    {
        decodeProgress_ = decodeProgress;
    }

    void PostDecodeProgress(int type, size_t writeDataLen, const void *context) override;

    PkgManager::StreamPtr GetPkgFileStream(const std::string &fileName) override;

    int32_t CreatePkgStream(PkgStreamPtr &stream, const std::string &fileName, size_t size, int32_t type) override;

    void ClosePkgStream(PkgStreamPtr &stream) override;

    int32_t ParseComponents(const std::string &packagePath, std::vector<std::string> &fileName) override;

private:
    PkgFilePtr CreatePackage(PkgStreamPtr stream, PkgFile::PkgType type, PkgInfoPtr header = nullptr);

    template<class T>
    PkgFilePtr CreatePackage(const std::string &path,
        PkgInfoPtr header, std::vector<std::pair<std::string, T>> &files, size_t &offset);

    int32_t ExtraAndLoadPackage(const std::string &path, const std::string &name, PkgFile::PkgType,
        std::vector<std::string> &fileIds);
    int32_t LoadPackage(const std::string &packagePath, std::vector<std::string> &fileIds, PkgFile::PkgType type);

    int32_t LoadPackageWithStream(const std::string &packagePath,
        std::vector<std::string> &fileIds, PkgFile::PkgType type, PkgStreamPtr stream);

    std::string GetPkgName(const std::string &path);

    PkgFile::PkgType GetPkgTypeByName(const std::string &path);

    int32_t Sign(PkgStreamPtr stream, size_t offset, const PkgInfoPtr &info);

    int32_t Verify(uint8_t digestMethod, const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature);

    int32_t DoGenerateFileDigest(PkgStreamPtr stream, uint8_t flags, const size_t fileLen, PkgBuffer &buff,
        std::pair<DigestAlgorithm::DigestAlgorithmPtr, DigestAlgorithm::DigestAlgorithmPtr> &algorithm);

    int32_t GenerateFileDigest(PkgStreamPtr stream,
        uint8_t digestMethod, uint8_t flags, std::vector<std::vector<uint8_t>> &digestInfos, size_t hashBufferLen = 0);

    void ClearPkgFile();

    int32_t SetSignVerifyKeyName(const std::string &keyName);

    int32_t DoCreatePkgStream(PkgStreamPtr &stream, const std::string &fileName, int32_t type);

private:
    bool unzipToFile_ {false};
    std::vector<PkgFilePtr> pkgFiles_ {};
    std::map<std::string, PkgFileConstructor> pkgFileCreator_ {};
    std::mutex mapLock_ {};
    std::map<std::string, PkgStreamPtr> pkgStreams_ {};
    std::string signVerifyKeyName_ {};
    PkgDecodeProgress decodeProgress_ { nullptr };
};
} // namespace Hpackage
#endif // PKG_MANAGER_IMPL_H
