/*
 * Copyright (c) 2026 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 HC_GEN_TEXT_GENERATOR_H
#define HC_GEN_TEXT_GENERATOR_H

#include <fstream>
#include <map>

#include "generator.h"

namespace OHOS {
namespace Hardware {
class TextGen : public Generator {
public:
    explicit TextGen(std::shared_ptr<Ast> ast);

    ~TextGen() override = default;

    bool Output() override;

private:
    bool Initialize();

    bool HeaderOutput();

    bool HeaderOutputTraversal();

    bool ImplOutput();

    bool InitOutput(const std::string &fileSuffix);

    bool DuplicateNodeNameCheck();

    static std::string ToUpperCamelString(const std::string &str);

    static std::string ToLowerCamelString(const std::string &str);

    static std::string ToCamelString(const std::string &str);

    static const std::string &ToUpperString(std::string &str);

    std::string GetHeaderProtectMacro(const std::string &headerFileName);

    uint32_t GenNodeDefinition(const std::shared_ptr<AstObject> &node);

    std::string GenConfigStructName(const std::shared_ptr<AstObject> &node);

    bool GenObjectDefinitionGen(const std::shared_ptr<AstObject> &object);

    bool GenTermDefinition(const std::shared_ptr<AstObject> &term);

    static const std::string &TypeToStr(uint32_t type);

    static bool IsInTemplate(const std::shared_ptr<AstObject> &object);

    bool OutputImplGlobalVariables();

    uint32_t ImplementGenTraversal(const std::shared_ptr<AstObject> &object, int32_t depth);

    uint32_t ImplementCloseBraceGen(const std::shared_ptr<AstObject> &object, int32_t depth);

    static const std::string &Indent(uint32_t times = 1);

    static bool IsInSubClassNode(const std::shared_ptr<AstObject> &object);

    uint32_t ObjectImplementGen(const std::shared_ptr<AstObject> &object, int32_t depth);

    bool TemplateObjectImplGen(const std::shared_ptr<AstObject> &object, int32_t depth);

    std::string GenTemplateVariableName(const std::shared_ptr<AstObject> &object);

    struct Symbol {
        Symbol(const std::shared_ptr<AstObject> &obj, uint32_t c) : object(obj), duplicateCount(c) {}
        ~Symbol() = default;
        std::shared_ptr<AstObject> object;
        uint32_t duplicateCount;
    };

    std::shared_ptr<Symbol> SymbolFind(const std::string &name);

    void SymbolAdd(const std::string &name, const std::shared_ptr<AstObject> &object);

    std::ofstream ofs_;
    std::string outputFileName_;
    std::string outputNameBase_;
    std::string prefix_;
    std::string moduleName_;
    std::string rootVariableName_;
    std::map<std::string, std::shared_ptr<Symbol>> symMap;

    uint32_t PrintTermImplement(const std::shared_ptr<AstObject> &object, int32_t depth);

    bool PrintBaseTypeValue(const std::shared_ptr<AstObject> &object);

    uint32_t PrintArrayImplement(const std::shared_ptr<AstObject> &object, int32_t depth);

    bool PrintArrayImplInSubClass(const std::shared_ptr<AstObject> &object, int32_t depth);

    bool HcsPrintArrayContent(const std::shared_ptr<AstObject> &object, uint32_t indent);

    std::string HcsBuildObjectPath(std::shared_ptr<AstObject> object);

    bool OutputTemplateImpl();

    bool OutputTemplateVariablesDeclare();

    uint32_t ArrayVariablesDeclareGen(const std::shared_ptr<AstObject> &object);

    std::string GenArrayName(const std::shared_ptr<AstObject> &term);

    uint32_t TemplateVariableGen(const std::shared_ptr<AstObject> &nodeObject);
};
} // namespace Hardware
} // namespace OHOS
#endif // HC_GEN_TEXT_GENERATOR_H