/*

 * Copyright (c) 2021 Huawei Device Co., Ltd.

 *

 * HDF is dual licensed: you can use it either under the terms of

 * the GPL, or the BSD license, at your option.

 * See the LICENSE file in the root of this repository for complete details.

 */



#include "codegen/java_code_emitter.h"



namespace OHOS {

namespace HDI {

void JavaCodeEmitter::EmitLicense(StringBuilder &sb)

{

    if (ast_->GetLicense().IsEmpty()) {

        return;

    }

    sb.Append(ast_->GetLicense()).Append("\n\n");

}



void JavaCodeEmitter::EmitPackage(StringBuilder &sb)

{

    sb.AppendFormat("package %s;\n", ast_->GetPackageName().string());

}



void JavaCodeEmitter::EmitInterfaceMethodCommands(StringBuilder &sb, const String &prefix)

{

    for (size_t i = 0; i < interface_->GetMethodNumber(); i++) {

        AutoPtr<ASTMethod> method = interface_->GetMethod(i);

        sb.Append(prefix).AppendFormat("private static final int COMMAND_%s = IRemoteObject.MIN_TRANSACTION_ID + %d;\n",

            ConstantName(method->GetName()).string(), i);

    }

}



String JavaCodeEmitter::MethodName(const String &name)

{

    if (name.IsEmpty() || islower(name[0])) {

        return name;

    }

    return String::Format("%c%s", tolower(name[0]), name.Substring(1).string());

}



String JavaCodeEmitter::SpecificationParam(StringBuilder &paramSb, const String &prefix)

{

    int maxLineLen = 120;

    int replaceLen = 2;

    String paramStr = paramSb.ToString();

    int preIndex = 0;

    int curIndex = 0;



    String insertStr = String::Format("\n%s", prefix.string());

    for (; curIndex < paramStr.GetLength(); curIndex++) {

        if (curIndex == maxLineLen && preIndex > 0) {

            paramStr.Replace(preIndex, replaceLen, ",");

            paramStr.insert(preIndex + 1, insertStr);

        } else {

            if (paramStr[curIndex] == ',') {

                preIndex = curIndex;

            }

        }

    }

    return paramStr;

}

} // namespace HDI

} // namespace OHOS