//
//  RNTAlanTextManager.m
//
//  Created by Alan AI, Inc on 30.10.2019.
//

#import "AlanSDK.h"
#import "AlanManager.h"
#import "RNTAlanTextManager.h"
#import <React/RCTViewManager.h>

@interface RNTAlanTextManager()
@property (nonatomic, strong) UIView* container;
@end

@implementation RNTAlanTextManager

RCT_EXPORT_MODULE(RNTAlanText)

- (UIView *)view
{
    if( self.container == nil )
    {
        self.container = [UIView new];
    }
    
    AlanText* alanText = AlanManager.shared.alanText;
    if( alanText == nil )
    {
        alanText = [[AlanText alloc] initWithFrame:CGRectZero];
        [alanText setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.container addSubview:alanText];
            
        NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:alanText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.container attribute:NSLayoutAttributeTop multiplier:1 constant:0];
        NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:alanText attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.container attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:alanText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.container attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
        NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:alanText attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.container attribute:NSLayoutAttributeRight multiplier:1 constant:0];

        [self.container addConstraints:@[top, bottom, left, right]];
        
        AlanManager.shared.alanText = alanText;
    }
    
    return self.container;
}

@end
