/*---------------------------------------------------------------------------*/
/* FormsObject.m                                                             */
/* copyright (c) innovaphone 2015                                            */
/*                                                                           */
/*---------------------------------------------------------------------------*/

#import "platform/platform.h"
#import "platform/IosNamespace.h"
#import "FormsConst.h"

@interface FormsIntArray()

@property (nonatomic) NSMutableArray *array_;

@end
@implementation FormsIntArray

+ (id) array
{
    return [[self alloc] init];
}

- (id) init
{
    self = [super init];
    if (self) {
        FormsIntArray *__weak weakSelf = self;
        _get = ^ int (int index) { NSNumber *value = (NSNumber *)(weakSelf.array_[index]); return [value intValue]; };
        _set = ^ void (int index, int value) { [weakSelf.array_ replaceObjectAtIndex:index withObject:[NSNumber numberWithInt:value]]; };
        _size = ^ int () { return (int)(weakSelf.array_.count); };
        _add = ^ void (int value) { [weakSelf.array_ addObject:[NSNumber numberWithInt:value]]; };
        _clear = ^ void () { return [weakSelf.array_ removeAllObjects]; };

        _array_ = [NSMutableArray array];
    }
    return self;
}

- (int) objectAtIndexedSubscript:(int)index
{
    return [self.array_[index] intValue];
}

- (void) setObject:(int)value atIndexedSubscript:(int)index
{
    self.array_[index] = [NSNumber numberWithInt:value];
}

@end
@interface FormsStringArray()

@property (nonatomic) NSMutableArray *array_;

@end
@implementation FormsStringArray

+ (id) array
{
    return [[self alloc] init];
}

- (id) init
{
    self = [super init];
    if (self) {
        FormsStringArray *__weak weakSelf = self;
        _get = ^ NSString * (int index) { NSString *str = (NSString *)(weakSelf.array_[index]); return ((((NSNull *) str) == [NSNull null]) ? nil : str); };
        _set = ^ void (int index, NSString *str) { [weakSelf.array_ replaceObjectAtIndex:index withObject:(str == nil) ? [NSNull null] : str]; };
        _size = ^ int () { return (int)(weakSelf.array_.count); };
        _add = ^ void (NSString *str) { [weakSelf.array_ addObject:(str == nil) ? [NSNull null] : str]; };
        _clear = ^ void () { return [weakSelf.array_ removeAllObjects]; };
        _setArray = ^ void (NSArray *stringArray) { [weakSelf.array_ setArray:stringArray]; };

        _array_ = [NSMutableArray array];
    }
    return self;
}

- (NSString *) objectAtIndexedSubscript:(int)index
{
    NSString *str = self.array_[index];
    return ((((NSNull *) str) == [NSNull null]) ? nil : str);
}

- (void) setObject:(NSString *)str atIndexedSubscript:(int)index
{
    self.array_[index] = (str == nil) ? [NSNull null] : str;
}

@end
@interface FormsStringDictionary()

@property (nonatomic) NSMutableDictionary *dictionary_;

@end
@implementation FormsStringDictionary

+ (id) dictionary
{
    return [[self alloc] init];
}

- (id) init
{
    self = [super init];
    if (self) {
        FormsStringDictionary *__weak weakSelf = self;
        _get = ^ NSString * (NSString *key) { NSString *str = (NSString *)([weakSelf.dictionary_ objectForKey:key]); return ((((NSNull *) str) == [NSNull null]) ? nil : str); };
        _put = ^ void (NSString *key, NSString *str) { [weakSelf.dictionary_ setObject:(str == nil) ? [NSNull null] : str forKey:key]; };

        _dictionary_ = [NSMutableDictionary dictionary];
    }
    return self;
}

@end
@implementation FormsObject

+ (id) formsObjectWithId:(int)new_id
{
    return [[self alloc] initWithId:new_id];
}

+ (id) formsObjectWithId:(int)new_id forms:(Forms *)forms
{
    return [[self alloc] initWithId:new_id forms:forms];
}

+ (id) formsObjectWithForms:(Forms *)forms
{
    return [[self alloc] initWithForms:forms];
}

- (id) initWithId:(int)new_id
{
    self = [super init];
    if (self) {
        _obj_id = new_id;
    }
    return self;
}

- (id) initWithId:(int)new_id forms:(Forms *)forms
{
    self = [super init];
    if (self) {
        _obj_id = new_id;
        _forms = forms;
        forms.set_id_obj(new_id, self);
    }
    return self;
}

- (id) initWithForms:(Forms *)forms
{
    self = [super init];
    if (self) {
        _obj_id = forms.new_id(self);
        _forms = forms;
    }
    return self;
}

- (void) forms_delete_object:(IosAsync *)ios_async
{
    self.forms.free_id(self.obj_id);
}

@end
