114cf0368Sopenharmony_ci/*
214cf0368Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
314cf0368Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
414cf0368Sopenharmony_ci * you may not use this file except in compliance with the License.
514cf0368Sopenharmony_ci * You may obtain a copy of the License at
614cf0368Sopenharmony_ci *
714cf0368Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
814cf0368Sopenharmony_ci *
914cf0368Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1014cf0368Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1114cf0368Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1214cf0368Sopenharmony_ci * See the License for the specific language governing permissions and
1314cf0368Sopenharmony_ci * limitations under the License.
1414cf0368Sopenharmony_ci*/
1514cf0368Sopenharmony_ci#define LOG_TAG "TextNapi"
1614cf0368Sopenharmony_ci#include "text_napi.h"
1714cf0368Sopenharmony_ci
1814cf0368Sopenharmony_ci#include "text.h"
1914cf0368Sopenharmony_ci#include "unified_record_napi.h"
2014cf0368Sopenharmony_ci
2114cf0368Sopenharmony_cinamespace OHOS {
2214cf0368Sopenharmony_cinamespace UDMF {
2314cf0368Sopenharmony_cinapi_value TextNapi::Constructor(napi_env env)
2414cf0368Sopenharmony_ci{
2514cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "TextNapi");
2614cf0368Sopenharmony_ci    napi_property_descriptor properties[] = {
2714cf0368Sopenharmony_ci        /* Text extends UnifiedRecord */
2814cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getType", UnifiedRecordNapi::GetType),
2914cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getValue", UnifiedRecordNapi::GetValue),
3014cf0368Sopenharmony_ci        /* Text properties */
3114cf0368Sopenharmony_ci        DECLARE_NAPI_GETTER_SETTER("details", GetDetails, SetDetails),
3214cf0368Sopenharmony_ci    };
3314cf0368Sopenharmony_ci    size_t count = sizeof(properties) / sizeof(properties[0]);
3414cf0368Sopenharmony_ci    return NapiDataUtils::DefineClass(env, "Text", properties, count, TextNapi::New);
3514cf0368Sopenharmony_ci}
3614cf0368Sopenharmony_ci
3714cf0368Sopenharmony_cinapi_value TextNapi::New(napi_env env, napi_callback_info info)
3814cf0368Sopenharmony_ci{
3914cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "TextNapi");
4014cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
4114cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info);
4214cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
4314cf0368Sopenharmony_ci
4414cf0368Sopenharmony_ci    auto *text = new (std::nothrow) TextNapi();
4514cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, text != nullptr, Status::E_ERROR, "no memory for text!");
4614cf0368Sopenharmony_ci    text->value_ = std::make_shared<Text>();
4714cf0368Sopenharmony_ci    ASSERT_CALL(ctxt->env, napi_wrap(env, ctxt->self, text, Destructor, nullptr, nullptr), text);
4814cf0368Sopenharmony_ci    return ctxt->self;
4914cf0368Sopenharmony_ci}
5014cf0368Sopenharmony_ci
5114cf0368Sopenharmony_civoid TextNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
5214cf0368Sopenharmony_ci{
5314cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "TextNapi");
5414cf0368Sopenharmony_ci    ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
5514cf0368Sopenharmony_ci    auto *text = new (std::nothrow) TextNapi();
5614cf0368Sopenharmony_ci    ASSERT_ERR_VOID(env, text != nullptr, Status::E_ERROR, "no memory for text!");
5714cf0368Sopenharmony_ci    text->value_ = std::static_pointer_cast<Text>(in);
5814cf0368Sopenharmony_ci    ASSERT_CALL_DELETE(env, napi_wrap(env, out, text, Destructor, nullptr, nullptr), text);
5914cf0368Sopenharmony_ci}
6014cf0368Sopenharmony_ci
6114cf0368Sopenharmony_civoid TextNapi::Destructor(napi_env env, void *data, void *hint)
6214cf0368Sopenharmony_ci{
6314cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "TextNapi finalize.");
6414cf0368Sopenharmony_ci    auto *text = static_cast<TextNapi *>(data);
6514cf0368Sopenharmony_ci    ASSERT_VOID(text != nullptr, "finalize null!");
6614cf0368Sopenharmony_ci    delete text;
6714cf0368Sopenharmony_ci}
6814cf0368Sopenharmony_ci
6914cf0368Sopenharmony_ciTextNapi *TextNapi::GetText(napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
7014cf0368Sopenharmony_ci{
7114cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "TextNapi");
7214cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info);
7314cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
7414cf0368Sopenharmony_ci    return static_cast<TextNapi *>(ctxt->native);
7514cf0368Sopenharmony_ci}
7614cf0368Sopenharmony_ci
7714cf0368Sopenharmony_cinapi_value TextNapi::GetDetails(napi_env env, napi_callback_info info)
7814cf0368Sopenharmony_ci{
7914cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "TextNapi");
8014cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
8114cf0368Sopenharmony_ci    auto text = GetText(env, info, ctxt);
8214cf0368Sopenharmony_ci    ASSERT_ERR(
8314cf0368Sopenharmony_ci        ctxt->env, (text != nullptr && text->value_ != nullptr), Status::E_ERROR, "invalid object!");
8414cf0368Sopenharmony_ci    ctxt->status = NapiDataUtils::SetValue(env, text->value_->GetDetails(), ctxt->output);
8514cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set details failed!");
8614cf0368Sopenharmony_ci    return ctxt->output;
8714cf0368Sopenharmony_ci}
8814cf0368Sopenharmony_ci
8914cf0368Sopenharmony_cinapi_value TextNapi::SetDetails(napi_env env, napi_callback_info info)
9014cf0368Sopenharmony_ci{
9114cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "TextNapi");
9214cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
9314cf0368Sopenharmony_ci    UDDetails details;
9414cf0368Sopenharmony_ci    auto input = [env, ctxt, &details](size_t argc, napi_value *argv) {
9514cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
9614cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
9714cf0368Sopenharmony_ci        ctxt->status = NapiDataUtils::GetValue(env, argv[0], details);
9814cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
9914cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: parameter details type must be Record<string, string>");
10014cf0368Sopenharmony_ci    };
10114cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
10214cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
10314cf0368Sopenharmony_ci    auto text = static_cast<TextNapi *>(ctxt->native);
10414cf0368Sopenharmony_ci    ASSERT_ERR(
10514cf0368Sopenharmony_ci        ctxt->env, (text != nullptr && text->value_ != nullptr), Status::E_ERROR, "invalid object!");
10614cf0368Sopenharmony_ci    text->value_->SetDetails(details);
10714cf0368Sopenharmony_ci    return nullptr;
10814cf0368Sopenharmony_ci}
10914cf0368Sopenharmony_ci} // namespace UDMF
11014cf0368Sopenharmony_ci} // namespace OHOS