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

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

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

@implementation RNTAlanButtonManager

RCT_EXPORT_MODULE(RNTAlanButton)

RCT_CUSTOM_VIEW_PROPERTY(projectid, NSString, AlanButton)
{
    if( self.container == nil )
    {
        self.container = [UIView new];
    }
    
    AlanButton* alanButton = AlanManager.shared.alanButton;
    if( alanButton == nil )
    {
        NSString* key = [NSString stringWithFormat:@"%@", json];
        AlanConfig* config = [[AlanConfig alloc] initWithKey:key isButtonDraggable:NO];
        alanButton = [[AlanButton alloc] initWithConfig:config];
        [alanButton setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.container addSubview:alanButton];

        NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:alanButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.container attribute:NSLayoutAttributeTop multiplier:1 constant:0];
        NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:alanButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.container attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:alanButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.container attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
        NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:alanButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.container attribute:NSLayoutAttributeRight multiplier:1 constant:0];
            
        [self.container addConstraints:@[top, bottom, left, right]];
        
        AlanManager.shared.alanButton = alanButton;
    }
}

- (UIView *)view
{
    if( self.container == nil )
    {
        self.container = [UIView new];
    }
    return self.container;
}

@end
