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

#import "platform/platform.h"
#import "platform/IosNamespace.h"
#import "common/ios/ios_forms/FormsConst.h"

@interface FormsCPObject : FormsObject

@end
@implementation FormsCPObject

- (void) forms_destroy_object:(IosAsync *)ios_async
{
    self.forms.set_id_obj(self.obj_id, nil);
    self.forms.ios_event.enqueue(FORMS_CP_DELETE_OBJECT, self.obj_id);
}

@end

@interface FormsCPImageSet : FormsCPObject

@property (nonatomic) UIImage *image;

+ (id) formsCPImageSetWithId:(int)new_id forms:(Forms *)forms
    lightContentImageData:(byte *)lightContentImageData lightContentImageDataLength:(int)lightContentImageDataLength
    darkContentImageData:(byte *)darkContentImageData darkContentImageDataLength:(int)darkContentImageDataLength;

- (id) initWithId:(int)new_id forms:(Forms *)forms
    lightContentImageData:(byte *)lightContentImageData lightContentImageDataLength:(int)lightContentImageDataLength
    darkContentImageData:(byte *)darkContentImageData darkContentImageDataLength:(int)darkContentImageDataLength;

@end
@implementation FormsCPImageSet

- (id) initWithId:(int)new_id forms:(Forms *)forms
    lightContentImageData:(byte *)lightContentImageData lightContentImageDataLength:(int)lightContentImageDataLength
    darkContentImageData:(byte *)darkContentImageData darkContentImageDataLength:(int)darkContentImageDataLength
{
    self = [super initWithId:new_id forms:forms];
    if (self) {
        if (darkContentImageData != nil) {
            _image = [[CPImageSet alloc] initWithLightContentImage:[UIImage imageWithData:[NSData dataWithBytes:lightContentImageData length:lightContentImageDataLength]]
                darkContentImage:[UIImage imageWithData:[NSData dataWithBytes:darkContentImageData length:darkContentImageDataLength]]];
        }
        else {
            _image = [UIImage imageWithData:[NSData dataWithBytes:lightContentImageData length:lightContentImageDataLength]];
        }
    }
    return self;
}

+ (id) formsCPImageSetWithId:(int)new_id forms:(Forms *)forms
    lightContentImageData:(byte *)lightContentImageData lightContentImageDataLength:(int)lightContentImageDataLength
    darkContentImageData:(byte *)darkContentImageData darkContentImageDataLength:(int)darkContentImageDataLength
{
    return [[self alloc] initWithId:new_id forms:forms
        lightContentImageData:lightContentImageData lightContentImageDataLength:lightContentImageDataLength
        darkContentImageData:darkContentImageData darkContentImageDataLength:darkContentImageDataLength];
}

@end

@interface FormsCPCompletion : FormsCPObject

@property (nonatomic,copy) void (^completionHandler)();

+ (id) formsCPCompletionWithForms:(Forms *)forms completionHandler:(void (^)())completionHandler;

- (id) initWithForms:(Forms *)forms completionHandler:(void (^)())completionHandler;

@end
@implementation FormsCPCompletion

- (id) initWithForms:(Forms *)forms completionHandler:(void (^)())completionHandler
{
    self = [super initWithForms:forms];
    if (self) {
        _completionHandler = completionHandler;
    }
    return self;
}

+ (id) formsCPCompletionWithForms:(Forms *)forms completionHandler:(void (^)())completionHandler
{
    return [[self alloc] initWithForms:forms completionHandler:completionHandler];
}

- (void) forms_cp_completion_complete:(IosAsync *)ios_async
{
    self.forms.trc([NSString stringWithFormat:@"forms_cp_completion_complete(%i)", self.obj_id]);
    self.completionHandler();
}

@end

@interface FormsCPTemplate : FormsCPObject

@property (nonatomic) CPTemplate *template;

+ (id) formsCPTemplateWithId:(int)new_id forms:(Forms *)forms template:(CPTemplate *)template;

- (id) initWithId:(int)new_id forms:(Forms *)forms template:(CPTemplate *)template;

@end
@implementation FormsCPTemplate

- (id) initWithId:(int)new_id forms:(Forms *)forms template:(CPTemplate *)template
{
    self = [super initWithId:new_id forms:forms];
    if (self) {
        _template = template;
        template.userInfo = self;
    }
    return self;
}

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

@end

@interface FormsCPListItem : FormsCPObject

@property (nonatomic) CPListItem *item;

+ (id) formsCPListItemWithId:(int)new_id forms:(Forms *)forms text:(NSString *)text detailText:(NSString *)detailText;
+ (id) formsCPListItemWithId:(int)new_id forms:(Forms *)forms text:(NSString *)text detailText:(NSString *)detailText image:(FormsCPImageSet *)image;

- (id) initWithId:(int)new_id forms:(Forms *)forms text:(NSString *)text detailText:(NSString *)detailText;
- (id) initWithId:(int)new_id forms:(Forms *)forms text:(NSString *)text detailText:(NSString *)detailText image:(FormsCPImageSet *)image;

@end
@implementation FormsCPListItem

- (void) forms_cp_list_item_set_text:(IosAsync *)ios_async
{
    NSString *text = ios_async.get_string();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_item_set_text(%i) \"%@\"", self.obj_id, text]);
    if ([self.item respondsToSelector:@selector(setText:)]) {
        [self.item setText:text];
    }
}

- (void) forms_cp_list_item_set_detail_text:(IosAsync *)ios_async
{
    NSString *detailText = ios_async.get_string();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_item_set_detail_text(%i) \"%@\"", self.obj_id, detailText]);
    if ([self.item respondsToSelector:@selector(setDetailText:)]) {
        [self.item setDetailText:detailText];
    }
}

- (id) initWithId:(int)new_id forms:(Forms *)forms text:(NSString *)text detailText:(NSString *)detailText
{
    self = [super initWithId:new_id forms:forms];
    if (self) {
        _item = [[CPListItem alloc] initWithText:text detailText:detailText];
    }
    return self;
}

- (id) initWithId:(int)new_id forms:(Forms *)forms text:(NSString *)text detailText:(NSString *)detailText image:(FormsCPImageSet *)image
{
    self = [super initWithId:new_id forms:forms];
    if (self) {
        _item = [[CPListItem alloc] initWithText:text detailText:detailText image:image.image];
    }
    return self;
}

+ (id) formsCPListItemWithId:(int)new_id forms:(Forms *)forms text:(NSString *)text detailText:(NSString *)detailText
{
    return [[self alloc] initWithId:new_id forms:forms text:text detailText:detailText];
}

+ (id) formsCPListItemWithId:(int)new_id forms:(Forms *)forms text:(NSString *)text detailText:(NSString *)detailText image:(FormsCPImageSet *)image
{
    return [[self alloc] initWithId:new_id forms:forms text:text detailText:detailText image:image];
}

@end

@interface FormsCPListSection : FormsCPObject

@property (nonatomic) CPListSection *section;
@property (nonatomic) NSArray<FormsCPListItem *> *items;

+ (id) formsCPListSectionWithId:(int)new_id forms:(Forms *)forms items:(NSArray<FormsCPListItem *> *)items header:(NSString *)header sectionIndexTitle:(NSString *)sectionIndexTitle;

- (id) initWithId:(int)new_id forms:(Forms *)forms items:(NSArray<FormsCPListItem *> *)items header:(NSString *)header sectionIndexTitle:(NSString *)sectionIndexTitle;

@end
@implementation FormsCPListSection

- (id) initWithId:(int)new_id forms:(Forms *)forms items:(NSArray<FormsCPListItem *> *)items header:(NSString *)header sectionIndexTitle:(NSString *)sectionIndexTitle
{
    NSMutableArray<CPListItem *> *listItems = [[NSMutableArray alloc] init];
    int i;

    for (i = 0; i < items.count; i++) {
        [listItems addObject:items[i].item];
    }
    self = [super initWithId:new_id forms:forms];
    if (self) {
        _items = items;
        _section = [[CPListSection alloc] initWithItems:listItems header:header sectionIndexTitle:sectionIndexTitle];
    }
    return self;
}

+ (id) formsCPListSectionWithId:(int)new_id forms:(Forms *)forms items:(NSArray<FormsCPListItem *> *)items header:(NSString *)header sectionIndexTitle:(NSString *)sectionIndexTitle
{
    return [[self alloc] initWithId:new_id forms:forms items:items header:header sectionIndexTitle:sectionIndexTitle];
}

@end

@interface FormsCPListTemplate : FormsCPTemplate <CPListTemplateDelegate>

@property (nonatomic) NSMutableArray<FormsCPListSection *> *addedSections;
@property (nonatomic) NSArray<FormsCPListSection *> *sections;

+ (id) formsCPListTemplateWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title sections:(NSArray<FormsCPListSection *> *)sections;

- (id) initWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title sections:(NSArray<FormsCPListSection *> *)sections;

@end
@implementation FormsCPListTemplate

- (void) forms_cp_list_section_add:(IosAsync *)ios_async
{
    int obj_id = ios_async.get_int();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_section_add(%i)", obj_id]);
    [self.addedSections addObject:(FormsCPListSection *)(self.forms.get_id_obj(obj_id))];
}

- (void) forms_cp_list_template_update_sections:(IosAsync *)ios_async
{
    NSMutableArray<CPListSection *> *listSections = [[NSMutableArray alloc] init];
    int i;

    self.sections = self.addedSections;
    for (i = 0; i < self.sections.count; i++) {
        [listSections addObject:self.sections[i].section];
    }
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_template_update_sections(%i) %i", self.obj_id, self.addedSections.count]);
    [((CPListTemplate *)(self.template)) updateSections:listSections];
    self.addedSections = [[NSMutableArray alloc] init];
}

- (void) listTemplate:(CPListTemplate *)listTemplate didSelectListItem:(CPListItem *)item completionHandler:(void (^)())completionHandler
{
    bool found;
    int i, j;

    found = false;
    j = 0;
    i = 0;
    while ((j < self.sections.count) && !found) {
        if (i < self.sections[j].items.count) {
            if (self.sections[j].items[i].item == item) {
                found = true;
            }
            else {
                i++;
            }
        }
        else {
            j++;
            i = 0;
        }
    }
    if (found) {
        FormsCPCompletion *completion = [FormsCPCompletion formsCPCompletionWithForms:self.forms completionHandler:completionHandler];
        self.forms.ios_event.enqueue(FORMS_CP_LIST_TEMPLATE_DID_SELECT_LIST_ITEM, ((FormsCPTemplate *)(listTemplate.userInfo)).obj_id, self.sections[j].items[i].obj_id, completion.obj_id);
    }
    else {
        self.forms.trc([NSString stringWithFormat:@"forms_cp_list_template_did_select_list_item(%i) not found", self.obj_id]);
        completionHandler();
    }
}

- (id) initWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title sections:(NSArray<FormsCPListSection *> *)sections
{
    NSMutableArray<CPListSection *> *listSections = [[NSMutableArray alloc] init];
    int i;

    for (i = 0; i < sections.count; i++) {
        [listSections addObject:sections[i].section];
    }
    CPListTemplate *listTemplate = [[CPListTemplate alloc] initWithTitle:title sections:listSections];
    self = [super initWithId:new_id forms:forms template:listTemplate];
    if (self) {
        _sections = sections;
        _addedSections = [[NSMutableArray alloc] init];
        listTemplate.delegate = self;
    }
    return self;
}

+ (id) formsCPListTemplateWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title sections:(NSArray<FormsCPListSection *> *)sections
{
    return [[self alloc] initWithId:new_id forms:forms title:title sections:sections];
}

@end

@interface FormsCPGridButton : FormsCPObject

@property (nonatomic) CPGridButton *gridButton;

+ (id) formsCPGridButtonWithId:(int)new_id forms:(Forms *)forms titleVariants:(NSArray<NSString *> *)titleVariants image:(FormsCPImageSet *)image;

- (id) initWithId:(int)new_id forms:(Forms *)forms titleVariants:(NSArray<NSString *> *)titleVariants image:(FormsCPImageSet *)image;

@end
@implementation FormsCPGridButton

- (void) forms_cp_grid_button_set_enabled:(IosAsync *)ios_async
{
    bool enabled = ios_async.get_bool();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_grid_button_set_enabled(%i) %i", self.obj_id, enabled]);
    [self.gridButton setEnabled:enabled];
}

- (id) initWithId:(int)new_id forms:(Forms *)forms titleVariants:(NSArray<NSString *> *)titleVariants image:(FormsCPImageSet *)image
{
    self = [super initWithId:new_id forms:forms];
    if (self) {
        _gridButton = [[CPGridButton alloc] initWithTitleVariants:titleVariants image:image.image handler:^(CPGridButton *gridButton) {
            forms.ios_event.enqueue(FORMS_CP_GRID_BUTTON_HANDLE, new_id);
        }];
    }
    return self;
}

+ (id) formsCPGridButtonWithId:(int)new_id forms:(Forms *)forms titleVariants:(NSArray<NSString *> *)titleVariants image:(FormsCPImageSet *)image
{
    return [[self alloc] initWithId:new_id forms:forms titleVariants:titleVariants image:image];
}

@end

@interface FormsCPGridTemplate : FormsCPTemplate

@property (nonatomic) NSMutableArray<FormsCPGridButton *> *addedGridButtons;

+ (id) formsCPGridTemplateWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title gridButtons:(NSArray<FormsCPGridButton *> *)gridButtons;

- (id) initWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title gridButtons:(NSArray<FormsCPGridButton *> *)gridButtons;

@end
@implementation FormsCPGridTemplate

- (void) forms_cp_grid_button_add:(IosAsync *)ios_async
{
    int obj_id = ios_async.get_int();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_grid_button_add(%i)", obj_id]);
    [self.addedGridButtons addObject:(FormsCPGridButton *)(self.forms.get_id_obj(obj_id))];
}

- (void) forms_cp_grid_template_update_title:(IosAsync *)ios_async
{
    NSString *title = ios_async.get_string();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_grid_template_update_title(%i) \"%@\"", self.obj_id, title]);
    if ([((CPGridTemplate *)(self.template)) respondsToSelector:@selector(updateTitle:)]) {
        [((CPGridTemplate *)(self.template)) updateTitle:title];
    }
}

- (void) forms_cp_grid_template_update_grid_buttons:(IosAsync *)ios_async
{
    NSMutableArray<CPGridButton *> *buttons = [[NSMutableArray alloc] init];
    int i;

    for (i = 0; i < self.addedGridButtons.count; i++) {
        [buttons addObject:self.addedGridButtons[i].gridButton];
    }
    self.forms.trc([NSString stringWithFormat:@"forms_cp_grid_template_update_grid_buttons(%i) %i", self.obj_id, self.addedGridButtons.count]);
    if ([((CPGridTemplate *)(self.template)) respondsToSelector:@selector(updateGridButtons:)]) {
        [((CPGridTemplate *)(self.template)) updateGridButtons:buttons];
    }
    self.addedGridButtons = [[NSMutableArray alloc] init];
}

- (id) initWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title gridButtons:(NSArray<FormsCPGridButton *> *)gridButtons
{
    NSMutableArray<CPGridButton *> *buttons = [[NSMutableArray alloc] init];
    int i;

    for (i = 0; i < gridButtons.count; i++) {
        [buttons addObject:gridButtons[i].gridButton];
    }
    CPGridTemplate *gridTemplate = [[CPGridTemplate alloc] initWithTitle:title gridButtons:buttons];
    self = [super initWithId:new_id forms:forms template:gridTemplate];
    if (self) {
        _addedGridButtons = [[NSMutableArray alloc] init];
    }
    return self;
}

+ (id) formsCPGridTemplateWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title gridButtons:(NSArray<FormsCPGridButton *> *)gridButtons
{
    return [[self alloc] initWithId:new_id forms:forms title:title gridButtons:gridButtons];
}

@end

@interface FormsCPAlertAction : FormsCPObject

@property (nonatomic) CPAlertAction *action;

+ (id) formsCPAlertActionWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title style:(int)style;

- (id) initWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title style:(int)style;

@end
@implementation FormsCPAlertAction

- (id) initWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title style:(int)style
{
    CPAlertActionStyle alertActionStyle = (style == 2) ? CPAlertActionStyleDestructive : ((style == 1) ? CPAlertActionStyleCancel : CPAlertActionStyleDefault);
    self = [super initWithId:new_id forms:forms];
    if (self) {
        _action = [[CPAlertAction alloc] initWithTitle:title style:alertActionStyle handler:^(CPAlertAction *action) {
            forms.ios_event.enqueue(FORMS_CP_ALERT_ACTION_HANDLE, new_id);
        }];
    }
    return self;
}

+ (id) formsCPAlertActionWithId:(int)new_id forms:(Forms *)forms title:(NSString *)title style:(int)style
{
    return [[self alloc] initWithId:new_id forms:forms title:title style:style];
}

@end

@interface FormsCPAlertTemplate : FormsCPTemplate

+ (id) formsCPAlertTemplateWithId:(int)new_id forms:(Forms *)forms titleVariants:(NSArray<NSString *> *)titleVariants actions:(NSArray<FormsCPAlertAction *> *)actions;

- (id) initWithId:(int)new_id forms:(Forms *)forms titleVariants:(NSArray<NSString *> *)titleVariants actions:(NSArray<FormsCPAlertAction *> *)actions;

@end
@implementation FormsCPAlertTemplate

- (id) initWithId:(int)new_id forms:(Forms *)forms titleVariants:(NSArray<NSString *> *)titleVariants actions:(NSArray<FormsCPAlertAction *> *)actions
{
    NSMutableArray<CPAlertAction *> *alertActions = [[NSMutableArray alloc] init];
    int i;

    for (i = 0; i < actions.count; i++) {
        [alertActions addObject:actions[i].action];
    }
    CPAlertTemplate *alertTemplate = [[CPAlertTemplate alloc] initWithTitleVariants:titleVariants actions:alertActions];
    self = [super initWithId:new_id forms:forms template:alertTemplate];
    if (self) {
    }
    return self;
}

+ (id) formsCPAlertTemplateWithId:(int)new_id forms:(Forms *)forms titleVariants:(NSArray<NSString *> *)titleVariants actions:(NSArray<FormsCPAlertAction *> *)actions
{
    return [[self alloc] initWithId:new_id forms:forms titleVariants:titleVariants actions:actions];
}

@end

@interface FormsCPInterfaceController : FormsCPObject <CPInterfaceControllerDelegate>

@property (nonatomic) CPInterfaceController *interfaceController;

+ (id) formsCPInterfaceControllerWithForms:(Forms *)forms interfaceController:(CPInterfaceController *)interfaceController;

- (id) initWithForms:(Forms *)forms interfaceController:(CPInterfaceController *)interfaceController;

@end
@implementation FormsCPInterfaceController

- (void) forms_cp_interface_controller_set_root_template:(IosAsync *)ios_async
{
    int template_id = ios_async.get_int();
    bool animated = ios_async.get_bool();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_interface_controller_set_root_template(%i) %i %i", self.obj_id, template_id, animated]);
    [self.interfaceController setRootTemplate:((FormsCPTemplate *)(self.forms.get_id_obj(template_id))).template animated:animated];
}

- (void) forms_cp_interface_controller_push_template:(IosAsync *)ios_async
{
    int template_id = ios_async.get_int();
    bool animated = ios_async.get_bool();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_interface_controller_push_template(%i) %i %i", self.obj_id, template_id, animated]);
    [self.interfaceController pushTemplate:((FormsCPTemplate *)(self.forms.get_id_obj(template_id))).template animated:animated];
}

- (void) forms_cp_interface_controller_pop_template:(IosAsync *)ios_async
{
    bool animated = ios_async.get_bool();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_interface_controller_pop_template(%i) %i", self.obj_id, animated]);
    [self.interfaceController popTemplateAnimated:animated];
}

- (void) forms_cp_interface_controller_pop_to_root_template:(IosAsync *)ios_async
{
    bool animated = ios_async.get_bool();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_interface_controller_pop_to_root_template(%i) %i", self.obj_id, animated]);
    [self.interfaceController popToRootTemplateAnimated:animated];
}

- (void) forms_cp_interface_controller_pop_to_template:(IosAsync *)ios_async
{
    int template_id = ios_async.get_int();
    bool animated = ios_async.get_bool();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_interface_controller_pop_to_template(%i) %i %i", self.obj_id, template_id, animated]);
    [self.interfaceController popToTemplate:((FormsCPTemplate *)(self.forms.get_id_obj(template_id))).template animated:animated];
}

- (void) forms_cp_interface_controller_present_template:(IosAsync *)ios_async
{
    int template_id = ios_async.get_int();
    bool animated = ios_async.get_bool();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_interface_controller_present_template(%i) %i %i", self.obj_id, template_id, animated]);
    [self.interfaceController presentTemplate:((FormsCPTemplate *)(self.forms.get_id_obj(template_id))).template animated:animated];
}

- (void) forms_cp_interface_controller_dismiss_template:(IosAsync *)ios_async
{
    bool animated = ios_async.get_bool();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_interface_controller_dismiss_template(%i) %i", self.obj_id, animated]);
    [self.interfaceController dismissTemplateAnimated:animated];
}

- (void) templateWillAppear:(CPTemplate *)aTemplate animated:(BOOL)animated
{
    self.forms.ios_event.enqueue(FORMS_CP_TEMPLATE_WILL_APPEAR, self.obj_id, ((FormsCPTemplate *)(aTemplate.userInfo)).obj_id, animated);
}

- (void) templateDidAppear:(CPTemplate *)aTemplate animated:(BOOL)animated
{
    self.forms.ios_event.enqueue(FORMS_CP_TEMPLATE_DID_APPEAR, self.obj_id, ((FormsCPTemplate *)(aTemplate.userInfo)).obj_id, animated);
}

- (void) templateWillDisappear:(CPTemplate *)aTemplate animated:(BOOL)animated
{
    self.forms.ios_event.enqueue(FORMS_CP_TEMPLATE_WILL_DISAPPEAR, self.obj_id, ((FormsCPTemplate *)(aTemplate.userInfo)).obj_id, animated);
}

- (void) templateDidDisappear:(CPTemplate *)aTemplate animated:(BOOL)animated
{
    self.forms.ios_event.enqueue(FORMS_CP_TEMPLATE_DID_DISAPPEAR, self.obj_id, ((FormsCPTemplate *)(aTemplate.userInfo)).obj_id, animated);
}

- (id) initWithForms:(Forms *)forms interfaceController:(CPInterfaceController *)interfaceController
{
    self = [super initWithForms:forms];
    if (self) {
        _interfaceController = interfaceController;
        interfaceController.delegate = self;
    }
    return self;
}

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

@end

@interface FormsCP()

@property (nonatomic) bool created;
@property (nonatomic) NSMutableArray<FormsCPInterfaceController *> *connectedInterfaceControllers;

@property (nonatomic) NSMutableArray<FormsCPListItem *> *addedListItems;
@property (nonatomic) NSMutableArray<FormsCPListSection *> *addedListSections;
@property (nonatomic) NSMutableArray<NSString *> *addedTitleVariants;
@property (nonatomic) NSMutableArray<FormsCPGridButton *> *addedGridButtons;
@property (nonatomic) NSMutableArray<FormsCPAlertAction *> *addedAlertActions;

@end
@implementation FormsCP

- (void) formsCPCreate:(int)new_id
{
    int i;

    self.created = true;
    for (i = 0; i < self.connectedInterfaceControllers.count; i++) {
        if (((NSNull *)[self.connectedInterfaceControllers objectAtIndex:i]) != [NSNull null]) {
            self.forms.ios_event.enqueue(FORMS_CP_DID_CONNECT_CAR_INTERFACE_CONTROLLER, [self.connectedInterfaceControllers objectAtIndex:i].obj_id);
        }
    }
}

- (void) forms_cp_ui_image_create:(IosAsync *)ios_async
{
    int new_id = ios_async.get_int();
    int lightContentImageDataLength;
    byte *lightContentImageData = ios_async.get_byte_array(&lightContentImageDataLength, false);
    self.forms.trc([NSString stringWithFormat:@"forms_cp_ui_image_create(%i) %i", new_id, lightContentImageDataLength]);
    [FormsCPImageSet formsCPImageSetWithId:new_id forms:self.forms
        lightContentImageData:lightContentImageData lightContentImageDataLength:lightContentImageDataLength
        darkContentImageData:nil darkContentImageDataLength:0];
}

- (void) forms_cp_image_set_create:(IosAsync *)ios_async
{
    int new_id = ios_async.get_int();
    int lightContentImageDataLength;
    byte *lightContentImageData = ios_async.get_byte_array(&lightContentImageDataLength, false);
    int darkContentImageDataLength;
    byte *darkContentImageData = ios_async.get_byte_array(&darkContentImageDataLength, false);
    self.forms.trc([NSString stringWithFormat:@"forms_cp_ui_image_create(%i) %i %i", new_id, lightContentImageDataLength, darkContentImageDataLength]);
    [FormsCPImageSet formsCPImageSetWithId:new_id forms:self.forms
        lightContentImageData:lightContentImageData lightContentImageDataLength:lightContentImageDataLength
        darkContentImageData:darkContentImageData darkContentImageDataLength:darkContentImageDataLength];
}

- (void) forms_cp_list_item_create:(IosAsync *)ios_async
{
    int new_id = ios_async.get_int();
    NSString *text = ios_async.get_string();
    NSString *detailText = ios_async.get_string();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_item_create(%i) \"%@\" \"%@\"", new_id, text, detailText]);
    [FormsCPListItem formsCPListItemWithId:new_id forms:self.forms text:text detailText:detailText];
}

- (void) forms_cp_list_item_with_image_create:(IosAsync *)ios_async
{
    int new_id = ios_async.get_int();
    NSString *text = ios_async.get_string();
    NSString *detailText = ios_async.get_string();
    int image_id = ios_async.get_int();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_item_with_image_create(%i) \"%@\" \"%@\" %i", new_id, text, detailText, image_id]);
    [FormsCPListItem formsCPListItemWithId:new_id forms:self.forms text:text detailText:detailText image:(FormsCPImageSet *)(self.forms.get_id_obj(image_id))];
}

- (void) forms_cp_list_item_add:(IosAsync *)ios_async
{
    int obj_id = ios_async.get_int();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_item_add(%i)", obj_id]);
    [self.addedListItems addObject:(FormsCPListItem *)(self.forms.get_id_obj(obj_id))];
}

- (void) forms_cp_list_section_create:(IosAsync *)ios_async
{
    int new_id = ios_async.get_int();
    NSString *header = ios_async.get_string();
    NSString *sectionIndexTitle = ios_async.get_string();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_section_create(%i) %i \"%@\" \"%@\"", new_id, self.addedListItems.count, header, sectionIndexTitle]);
    [FormsCPListSection formsCPListSectionWithId:new_id forms:self.forms items:self.addedListItems header:header sectionIndexTitle:sectionIndexTitle];
    self.addedListItems = [[NSMutableArray alloc] init];
}

- (void) forms_cp_list_section_add:(IosAsync *)ios_async
{
    int obj_id = ios_async.get_int();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_section_add(%i)", obj_id]);
    [self.addedListSections addObject:(FormsCPListSection *)(self.forms.get_id_obj(obj_id))];
}

- (void) forms_cp_list_template_create:(IosAsync *)ios_async
{
    int new_id = ios_async.get_int();
    NSString *title = ios_async.get_string();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_template_create(%i) \"%@\" %i", new_id, title, self.addedListSections.count]);
    [FormsCPListTemplate formsCPListTemplateWithId:new_id forms:self.forms title:title sections:self.addedListSections];
    self.addedListSections = [[NSMutableArray alloc] init];
}

- (void) forms_cp_title_variant_add:(IosAsync *)ios_async
{
    NSString *titleVariant = ios_async.get_string();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_list_section_add() \"%@\"", titleVariant]);
    [self.addedTitleVariants addObject:titleVariant];
}

- (void) forms_cp_grid_button_create:(IosAsync *)ios_async
{
    int new_id = ios_async.get_int();
    int image_id = ios_async.get_int();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_grid_button_create(%i) %i", new_id, self.addedTitleVariants.count]);
    [FormsCPGridButton formsCPGridButtonWithId:new_id forms:self.forms titleVariants:self.addedTitleVariants image:(FormsCPImageSet *)(self.forms.get_id_obj(image_id))];
    self.addedTitleVariants = [[NSMutableArray alloc] init];
}

- (void) forms_cp_grid_button_add:(IosAsync *)ios_async
{
    int obj_id = ios_async.get_int();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_grid_button_add(%i)", obj_id]);
    [self.addedGridButtons addObject:(FormsCPGridButton *)(self.forms.get_id_obj(obj_id))];
}

- (void) forms_cp_grid_template_create:(IosAsync *)ios_async
{
    int new_id = ios_async.get_int();
    NSString *title = ios_async.get_string();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_grid_template_create(%i) \"%@\" %i", new_id, title, self.addedGridButtons.count]);
    [FormsCPGridTemplate formsCPGridTemplateWithId:new_id forms:self.forms title:title gridButtons:self.addedGridButtons];
    self.addedGridButtons = [[NSMutableArray alloc] init];
}

- (void) forms_cp_alert_action_create:(IosAsync *)ios_async
{
    int new_id = ios_async.get_int();
    NSString *title = ios_async.get_string();
    int style = ios_async.get_int();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_alert_action_create(%i) \"%@\" %i", new_id, title, style]);
    [FormsCPAlertAction formsCPAlertActionWithId:new_id forms:self.forms title:title style:style];
}

- (void) forms_cp_alert_action_add:(IosAsync *)ios_async
{
    int obj_id = ios_async.get_int();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_alert_action_add(%i)", obj_id]);
    [self.addedAlertActions addObject:(FormsCPAlertAction *)(self.forms.get_id_obj(obj_id))];
}

- (void) forms_cp_alert_template_create:(IosAsync *)ios_async
{
    int new_id = ios_async.get_int();
    self.forms.trc([NSString stringWithFormat:@"forms_cp_alert_template_create(%i) %i %i", new_id, self.addedTitleVariants.count, self.addedAlertActions.count]);
    [FormsCPAlertTemplate formsCPAlertTemplateWithId:new_id forms:self.forms titleVariants:self.addedTitleVariants actions:self.addedAlertActions];
    self.addedTitleVariants = [[NSMutableArray alloc] init];
    self.addedAlertActions = [[NSMutableArray alloc] init];
}

- (void) didConnectCarInterfaceController:(CPInterfaceController *)interfaceController toWindow:(CPWindow *)window
{
    FormsCPInterfaceController *ic = [FormsCPInterfaceController formsCPInterfaceControllerWithForms:self.forms interfaceController:interfaceController];
    int i;

    i = 0;
    while ((i < self.connectedInterfaceControllers.count) && (((NSNull *)[self.connectedInterfaceControllers objectAtIndex:i]) != [NSNull null])) {
        i++;
    }
    if (i < self.connectedInterfaceControllers.count) {
        [self.connectedInterfaceControllers replaceObjectAtIndex:i withObject:ic];
    }
    else {
        [self.connectedInterfaceControllers addObject:ic];
    }
    if (self.created) {
        self.forms.ios_event.enqueue(FORMS_CP_DID_CONNECT_CAR_INTERFACE_CONTROLLER, ic.obj_id);
    }
}

- (void) didDisconnectCarInterfaceController:(CPInterfaceController *)interfaceController fromWindow:(CPWindow *)window
{
    int i;

    i = 0;
    while ([self.connectedInterfaceControllers objectAtIndex:i].interfaceController != interfaceController) {
        i++;
    }
    if (self.created) {
        self.forms.ios_event.enqueue(FORMS_CP_DID_DISCONNECT_CAR_INTERFACE_CONTROLLER, [self.connectedInterfaceControllers objectAtIndex:i].obj_id);
    }
    [self.connectedInterfaceControllers replaceObjectAtIndex:i withObject:(FormsCPInterfaceController *)[NSNull null]];
}

- (id) initWithId:(int)new_id forms:(Forms *)forms
{
    self = [super initWithId:new_id forms:forms];
    if (self) {
        _created = false;
        _connectedInterfaceControllers = [[NSMutableArray alloc] init];
        _addedListItems = [[NSMutableArray alloc] init];
        _addedListSections = [[NSMutableArray alloc] init];
        _addedTitleVariants = [[NSMutableArray alloc] init];
        _addedGridButtons = [[NSMutableArray alloc] init];
        _addedAlertActions = [[NSMutableArray alloc] init];
    }
    return self;
}

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

@end
