//
//  GXTemplateEngine.h
//  GaiaXiOS
//
//  Copyright (c) 2021, Alibaba Group Holding Limited.
//
//  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.

#import <Foundation/Foundation.h>
#import <GaiaXiOS/GXRootViewProtocal.h>
#import <UIKit/UIKit.h>

@class GXTemplateItem;
@class GXTemplateData;

#define TheGXTemplateEngine [GXTemplateEngine sharedInstance]

NS_ASSUME_NONNULL_BEGIN

@interface GXTemplateEngine : NSObject

//Whether the node needs to be flattened，default is YES
@property (nonatomic, assign) BOOL isNeedFlat;

///Get the engine singleton
+ (instancetype)sharedInstance;


/// Creat template view
/// @param templateItem template information
/// @param measureSize measure size
///
/// @return  view object generated by template
- (UIView <GXRootViewProtocal> * _Nullable)creatViewByTemplateItem:(GXTemplateItem *)templateItem
                                                       measureSize:(CGSize)measureSize;

/// Data binding (若无更新size的诉求，请使用bindData:onView:)
/// @param data template data
/// @param measureSize measure size
/// @param view the root view
- (void)bindData:(GXTemplateData *)data measureSize:(CGSize)measureSize onRootView:(UIView *)view;

/// Data binding
/// @param data template data
/// @param view the root view
- (void)bindData:(GXTemplateData *)data onView:(UIView *)view;

/// Get view in the root view by nodeId
/// @param nodeId node id
/// @param rootView the root view
///
/// @return the target view，can be nil
- (UIView * _Nullable)queryViewByNodeId:(NSString *)nodeId rootView:(UIView *)rootView;

@end


@interface GXTemplateEngine (Calculate)

/// Get the real size of the template
/// @param templateItem template information
/// @param measureSize measure size
///
/// @return template real size
- (CGSize)sizeWithTemplateItem:(GXTemplateItem *)templateItem
                   measureSize:(CGSize)measureSize;


/// Get the real size of the template
/// @param templateItem template information
/// @param measureSize measure size
/// @param data template data
///
/// @return template real size
- (CGSize)sizeWithTemplateItem:(GXTemplateItem *)templateItem
                   measureSize:(CGSize)measureSize
                          data:(GXTemplateData *)data;

@end


@interface GXTemplateEngine (Template)

/// Get template metadata
/// @param templateItem template information
///
/// @return template metadata
- (NSDictionary * _Nullable)loadTemplateContentWithTemplateItem:(GXTemplateItem *)templateItem;

@end

NS_ASSUME_NONNULL_END
