18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci#include "ObjectProxy.h"
168bf80f4bSopenharmony_ci
178bf80f4bSopenharmony_ci#include <meta/interface/intf_metadata.h>
188bf80f4bSopenharmony_ci#include <meta/interface/property/construct_array_property.h>
198bf80f4bSopenharmony_ci#include <meta/interface/property/property.h>
208bf80f4bSopenharmony_ci
218bf80f4bSopenharmony_ci#include <base/math/vector.h>
228bf80f4bSopenharmony_ci
238bf80f4bSopenharmony_ci#include "Vec2Proxy.h"
248bf80f4bSopenharmony_ci
258bf80f4bSopenharmony_ciObjectProxy::ObjectProxy(META_NS::IProperty::ConstPtr& property, napi_env env)
268bf80f4bSopenharmony_ci    : PropertyProxy(META_NS::IProperty::Ptr(property, const_cast<META_NS::IProperty*>(property.get()))), env_(env)
278bf80f4bSopenharmony_ci{
288bf80f4bSopenharmony_ci    napi_value value;
298bf80f4bSopenharmony_ci    napi_create_object(env, &value);
308bf80f4bSopenharmony_ci    obj = NapiApi::StrongRef { NapiApi::Object(env, value) };
318bf80f4bSopenharmony_ci
328bf80f4bSopenharmony_ci    META_NS::ConstProperty<META_NS::IObject::Ptr> prop(GetProperty());
338bf80f4bSopenharmony_ci
348bf80f4bSopenharmony_ci    if (prop) {
358bf80f4bSopenharmony_ci        auto meta = interface_cast<META_NS::IMetadata>(prop->GetValue());
368bf80f4bSopenharmony_ci        if (meta) {
378bf80f4bSopenharmony_ci            for (auto& p : meta->GetAllProperties()) {
388bf80f4bSopenharmony_ci                AddProperty(p, p->GetName());
398bf80f4bSopenharmony_ci            }
408bf80f4bSopenharmony_ci        }
418bf80f4bSopenharmony_ci    }
428bf80f4bSopenharmony_ci}
438bf80f4bSopenharmony_ci
448bf80f4bSopenharmony_civoid ObjectProxy::AddProperty(BASE_NS::unique_ptr<PropertyProxy> property, BASE_NS::string_view member)
458bf80f4bSopenharmony_ci{
468bf80f4bSopenharmony_ci    duh.Lock();
478bf80f4bSopenharmony_ci
488bf80f4bSopenharmony_ci    properties_.insert_or_assign(member, BASE_NS::move(property));
498bf80f4bSopenharmony_ci
508bf80f4bSopenharmony_ci    Hook(BASE_NS::string(member));
518bf80f4bSopenharmony_ci
528bf80f4bSopenharmony_ci    ScheduleUpdate();
538bf80f4bSopenharmony_ci
548bf80f4bSopenharmony_ci    duh.Unlock();
558bf80f4bSopenharmony_ci}
568bf80f4bSopenharmony_ci
578bf80f4bSopenharmony_civoid ObjectProxy::AddProperty(const META_NS::IProperty::Ptr& property, BASE_NS::string_view member)
588bf80f4bSopenharmony_ci{
598bf80f4bSopenharmony_ci    if (property->IsCompatible(META_NS::UidFromType<BASE_NS::Math::Vec2>())) {
608bf80f4bSopenharmony_ci        AddProperty(BASE_NS::make_unique<Vec2Proxy>(env_, property), member);
618bf80f4bSopenharmony_ci    }
628bf80f4bSopenharmony_ci}
638bf80f4bSopenharmony_ci
648bf80f4bSopenharmony_civoid ObjectProxy::SetValue(NapiApi::FunctionContext<>& info, BASE_NS::string_view memb)
658bf80f4bSopenharmony_ci{
668bf80f4bSopenharmony_ci    duh.Lock();
678bf80f4bSopenharmony_ci    auto i = properties_.find(memb);
688bf80f4bSopenharmony_ci    if (i == properties_.end()) {
698bf80f4bSopenharmony_ci        return;
708bf80f4bSopenharmony_ci    }
718bf80f4bSopenharmony_ci
728bf80f4bSopenharmony_ci    NapiApi::FunctionContext<NapiApi::Object> ctx(info.GetEnv(), info.GetInfo());
738bf80f4bSopenharmony_ci
748bf80f4bSopenharmony_ci    if (i->second->GetProperty()->IsCompatible(META_NS::UidFromType<BASE_NS::Math::Vec2>())) {
758bf80f4bSopenharmony_ci        NapiApi::Object obj = ctx.Arg<0>();
768bf80f4bSopenharmony_ci
778bf80f4bSopenharmony_ci        BASE_NS::Math::Vec2 vec(obj.Get<float>("x"), obj.Get<float>("y"));
788bf80f4bSopenharmony_ci        static_cast<Vec2Proxy&>(*i->second).SetValue(vec);
798bf80f4bSopenharmony_ci
808bf80f4bSopenharmony_ci        ScheduleUpdate();
818bf80f4bSopenharmony_ci    }
828bf80f4bSopenharmony_ci
838bf80f4bSopenharmony_ci    duh.Unlock();
848bf80f4bSopenharmony_ci}
858bf80f4bSopenharmony_ci
868bf80f4bSopenharmony_cinapi_value ObjectProxy::GetValue(NapiApi::FunctionContext<>& info, BASE_NS::string_view memb)
878bf80f4bSopenharmony_ci{
888bf80f4bSopenharmony_ci    duh.Lock();
898bf80f4bSopenharmony_ci    auto i = properties_.find(memb);
908bf80f4bSopenharmony_ci    if (i == properties_.end()) {
918bf80f4bSopenharmony_ci        duh.Unlock();
928bf80f4bSopenharmony_ci        return {};
938bf80f4bSopenharmony_ci    }
948bf80f4bSopenharmony_ci
958bf80f4bSopenharmony_ci    napi_value result = i->second->Value();
968bf80f4bSopenharmony_ci
978bf80f4bSopenharmony_ci    duh.Unlock();
988bf80f4bSopenharmony_ci
998bf80f4bSopenharmony_ci    return result;
1008bf80f4bSopenharmony_ci}
1018bf80f4bSopenharmony_ci
1028bf80f4bSopenharmony_civoid ObjectProxy::UpdateLocalValues() {}
1038bf80f4bSopenharmony_ci
1048bf80f4bSopenharmony_civoid ObjectProxy::UpdateRemoteValues() {}
105