1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "component_tree.h"
17
18#include "tree_manager.h"
19#include "wukong_define.h"
20
21namespace OHOS {
22namespace WuKong {
23namespace {
24const uint8_t COMPONENT_TYPE_POSION = 48;
25const uint8_t COMPONENT_WIDTH_POSION = 36;
26const uint8_t COMPONENT_HEIGHT_POSION = 24;
27const uint8_t COMPONENT_CONTENT_POSION = 8;
28const uint8_t COMPONENT_RESERVED_POSION = 0;
29}  // namespace
30bool ComponentTree::RecursUpdateInfo(const std::shared_ptr<ComponentTree>& source)
31{
32    if (source == nullptr) {
33        ERROR_LOG("the argument source is nullptr.");
34        return false;
35    }
36    // Copy source count info to new component node.
37    expectedInputCount_ = source->expectedInputCount_;
38    inputCount_ = source->inputCount_;
39    inputTypeCountMap_ = source->inputTypeCountMap_;
40
41    // Recurs component tree for copy count info.
42    for (auto child : children_) {
43        for (auto sourceChild : source->children_) {
44            if (child->IsEqual(sourceChild)) {
45                std::static_pointer_cast<ComponentTree>(child)->RecursUpdateInfo(
46                    std::static_pointer_cast<ComponentTree>(sourceChild));
47                break;
48            }
49        }
50    }
51    return true;
52}
53
54/**
55 * @brief component Node Id specification format
56 * @details
57 * |----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
58 * |TYPE-16            |WIDTH-12      |HEIGHT-12     |CONTENT - 16       |RESERVED |
59 */
60bool ComponentTree::SetNodeId()
61{
62    const uint8_t typeCount = 2;
63    const uint8_t contentCount = 2;
64    nodeId_ = 0;
65    auto elementInfo = TreeManager::GetInstance()->GetNewElementInfoList(index_);
66    if (elementInfo == nullptr) {
67        ERROR_LOG("get new element info is nullptr");
68        return false;
69    }
70
71    rect_ = elementInfo->GetRectInScreen();
72    isVisible_ = elementInfo->IsVisible();
73    // type is component type or component id of the ElementInfo
74    type_ = elementInfo->GetComponentType();
75    uint64_t type = GetSubName(type_, typeCount);
76    // w is width of the ElementInfo
77    uint64_t w = (uint64_t)(rect_.GetRightBottomXScreenPostion() - rect_.GetLeftTopXScreenPostion());
78    // h is width of the ElementInfo
79    uint64_t h = (uint64_t)(rect_.GetRightBottomYScreenPostion() - rect_.GetLeftTopYScreenPostion());
80    // the ElementInfo content of 2 length
81    uint64_t str = GetSubName(elementInfo->GetContent(), contentCount);
82
83    // the ElementInfo name of 4 length
84    TRACK_LOG_STR("component Type: (%d), Width: (%d), Height: (%d)", (uint32_t)type, (int32_t)w, (int32_t)h);
85    nodeId_ |= type << COMPONENT_TYPE_POSION;
86    nodeId_ |= w << COMPONENT_WIDTH_POSION;
87    nodeId_ |= h << COMPONENT_HEIGHT_POSION;
88    nodeId_ |= str << COMPONENT_CONTENT_POSION;
89    nodeId_ |= index_ << COMPONENT_RESERVED_POSION;
90    inspectorKey_ =  elementInfo->GetInspectorKey();
91    TRACK_LOG_STR("component Node ID: (0x%016llX), inspectorKey: (%s)", nodeId_, inspectorKey_.c_str());
92    return true;
93}
94}  // namespace WuKong
95}  // namespace OHOS
96