/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2024 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 "TabView.h"
#import "BoxContainer.h"
#import "GridLayout.h"

@implementation UiTopTabView

@synthesize container = _container;

- (id)init:(UiObject*)obj args:(UiTabViewArgs*)args {
    self = [super init];
    _obj = obj;
    _subcontainer = args->subcontainer;
    _padding = args->padding;
    _spacing = args->spacing;
    _columnspacing = args->columnspacing;
    _rowspacing = args->rowspacing;
    _onchange = args->onchange;
    _onchangedata = args->onchangedata;
    _var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_INTEGER);
    
    if(args->tabview == UI_TABVIEW_INVISIBLE || args->tabview == UI_TABVIEW_NAVIGATION_SIDE) {
        self.tabViewType = NSNoTabsNoBorder;
    }
    
    return self;
}

- (void) addView:(NSView*)view layout:(UiLayout*)layout {
    // noop
}

- (NSView<Container>*) createTab:(int)index title:(NSString*)title {
    NSTabViewItem *item = [[NSTabViewItem alloc]initWithIdentifier:nil];
    [item setLabel:title];
    if(index < 0) {
        [self addTabViewItem:item];
    } else {
        [self insertTabViewItem:item atIndex:index];
    }
    
    BoxContainer *content = [[BoxContainer alloc]init];
    item.view = content;
    
    GridLayout *sub;
    switch(_subcontainer) {
        default: sub = [[BoxContainer alloc] init:NSUserInterfaceLayoutOrientationVertical spacing:_spacing]; break;
        case UI_CONTAINER_HBOX: sub = [[BoxContainer alloc] init:NSUserInterfaceLayoutOrientationHorizontal spacing:_spacing]; break;
        case UI_CONTAINER_GRID: {
            sub = [[GridLayout alloc] init];
            sub.columnspacing = _columnspacing;
            sub.rowspacing = _rowspacing;
            break;
        }
    }
    UiLayout layout = {
        .margin = _padding,
        .margin_left = _padding, .margin_right = _padding, .margin_top = _padding, .margin_bottom = _padding,
        .fill = TRUE };
    [content addView:sub layout:&layout];
    
    return sub;
}

- (void) selectTab:(int)index {
    [self selectTabViewItemAtIndex:index];
}

- (void) removeTab:(int)index {
    NSTabViewItem *item = [self tabViewItemAtIndex:index];
    if(item != nil) {
        [self removeTabViewItem:item];
    }
}

- (UiObject*) addTab:(int)index title:(NSString*)title {
    NSView<Container> *sub = [self createTab:index title:title];
    
    UiObject *newobj = uic_object_new_toplevel();
    newobj->widget = (__bridge void*)sub;
    
    UiContainerX *container = ui_create_container(newobj, sub);
    uic_object_push_container(newobj, container);
    
    return newobj;
}

@end
