/*
 * 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 "list.h"
#import "ListDelegate.h"
#import <objc/runtime.h>

static void* getvalue_wrapper(UiList *list, void *elm, int row, int col, void *userdata, UiBool *freeResult) {
    ui_getvaluefunc getvalue = (ui_getvaluefunc)userdata;
    return getvalue(elm, col);
}

static void* str_getvalue(UiList *list, void *elm, int row, int col, void *userdata, UiBool *freeResult) {
    return elm;
}

UIWIDGET ui_listview_create(UiObject* obj, UiListArgs *args) {
    NSScrollView *scrollview = [[NSScrollView alloc] init];
    
    NSTableView *tableview = [[NSTableView alloc] init];
    tableview.autoresizingMask = NSViewWidthSizable;
    tableview.headerView = nil;
    
    scrollview.documentView = tableview;
    
    UiLayout layout = UI_INIT_LAYOUT(args);
    ui_container_add(obj, scrollview, &layout, TRUE);
    
    UiVar *var = uic_widget_var(obj->ctx, obj->ctx, args->list, args->varname, UI_VAR_LIST);
    if(var) {
        ui_getvaluefunc2 getvalue = args->getvalue2;
        void *getvaluedata = args->getvalue2data;
        if(!getvalue) {
            if(args->getvalue) {
                getvalue = getvalue_wrapper;
                getvaluedata = (void*)args->getvalue;
            } else {
                getvalue = str_getvalue; // by default list values are interpreted as strings
            }
        }
        
        NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"column"];
        column.width = 400;
        [tableview addTableColumn:column];
        
        ListDataSource *dataSource = [[ListDataSource alloc] init:tableview.tableColumns var:var getvalue:getvalue getvaluedata:getvaluedata];
        
        tableview.dataSource = dataSource;
        [tableview reloadData];
        
        objc_setAssociatedObject(tableview, "ui_datasource", dataSource, OBJC_ASSOCIATION_RETAIN);
    }
    
    ListDelegate *delegate = [[ListDelegate alloc] init:tableview obj:obj];
    delegate.onactivate = args->onactivate;
    delegate.onactivatedata = args->onactivatedata;
    delegate.onselection = args->onselection;
    delegate.onselectiondata = args->onselectiondata;
    tableview.delegate = delegate;
    objc_setAssociatedObject(tableview, "ui_listdelegate", delegate, OBJC_ASSOCIATION_RETAIN);
    tableview.doubleAction = @selector(activateEvent:);
    tableview.target = delegate;
    
    return (__bridge void*)scrollview;
}
