/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2025 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import "Toolbar.h"
#import "EventData.h"
#import "image.h"
#import <objc/runtime.h>

#include "../common/toolbar.h"


void ui_toolbar_init(void) {
    
}



/* ---------------------      UiToolbar      --------------------- */

@implementation UiToolbar

- (UiToolbar*) initWithObject:(UiObject*)object {
    self = [super initWithIdentifier:@"UiToolbar"];
    _obj = object;
    
    allowedItems = [[NSMutableArray alloc]initWithCapacity:16];
    defaultItems = [[NSMutableArray alloc]initWithCapacity:16];
    
    CxMap *toolbarItems = uic_get_toolbar_items();
    CxMapIterator i = cxMapIteratorKeys(toolbarItems);
    cx_foreach(CxHashKey *, key, i) {
        NSString *s = [[NSString alloc]initWithBytes:key->data length:key->len encoding:NSUTF8StringEncoding];
        [allowedItems addObject:s];
    }
    [allowedItems addObject: NSToolbarFlexibleSpaceItemIdentifier];
    [allowedItems addObject: NSToolbarSpaceItemIdentifier];
    
    CxList *tbitems[3];
    tbitems[0] =  uic_get_toolbar_defaults(UI_TOOLBAR_LEFT);
    tbitems[1] =  uic_get_toolbar_defaults(UI_TOOLBAR_CENTER);
    tbitems[2] =  uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT);
    for(int t=0;t<3;t++) {
        CxIterator iter = cxListIterator(tbitems[t]);
        cx_foreach(char *, name, iter) {
            NSString *s = [[NSString alloc] initWithUTF8String:name];
            [defaultItems addObject:s];
        }
    }
    
    [self setDelegate:self];
    [self setAllowsUserCustomization:YES];
    return self;
}

// implementation of NSToolbarDelegate methods

- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
    return allowedItems;
}

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
    return defaultItems;
}

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
  itemForItemIdentifier:(NSString *)itemIdentifier
  willBeInsertedIntoToolbar:(BOOL)flag {
    CxMap *items = uic_get_toolbar_items();
    UiToolbarItemI *item = cxMapGet(items, itemIdentifier.UTF8String);
    if(!item) {
        return nil;
    }
    
    switch(item->type) {
        default: return nil;
        case UI_TOOLBAR_ITEM: {
            return ui_nstoolbaritem_create_item(_obj, (UiToolbarItem*)item, itemIdentifier);
        }
        case UI_TOOLBAR_TOGGLEITEM: {
            return ui_nstoolbaritem_create_toggle(_obj, (UiToolbarToggleItem*)item, itemIdentifier);
        }
    }
    
    return nil;
}

@end

NSToolbarItem* ui_nstoolbaritem_create_item(UiObject *obj, UiToolbarItem *item, NSString *identifier) {
    NSToolbarItem *button = [[NSToolbarItem alloc] initWithItemIdentifier: identifier];
    button.bordered = YES;
    
    if(item->args.label) {
        NSString *label = [[NSString alloc] initWithUTF8String:item->args.label];
        button.paletteLabel = label;
        button.label = label;
    }
    if(item->args.icon) {
        //button.image = [NSImage imageNamed: [[NSString alloc] initWithUTF8String:item->args.icon]];
        button.image = ui_cocoa_named_icon(item->args.icon);
    }
    
    if(item->args.onclick) {
        EventData *event = [[EventData alloc] init:item->args.onclick userdata:item->args.onclickdata];
        event.obj = obj;
        button.target = event;
        button.action = @selector(handleEvent:);
        objc_setAssociatedObject(button, "eventdata", event, OBJC_ASSOCIATION_RETAIN);
    }
    return button;
}

NSToolbarItem* ui_nstoolbaritem_create_toggle(UiObject *obj, UiToolbarToggleItem *item, NSString *identifier) {
    NSToolbarItem *tbItem = [[NSToolbarItem alloc] initWithItemIdentifier: identifier];
    if(item->args.label) {
        NSString *label = [[NSString alloc] initWithUTF8String:item->args.label];
        [tbItem setLabel:label];
    }
    return tbItem;
}
