/*
 * 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 "ListDataSource.h"

@implementation ListDataSource

- (id) init:(NSArray<NSTableColumn*>*) columns var:(UiVar*)var getvalue:(ui_getvaluefunc2) getvaluefunc getvaluedata:(void*)userdata {
    _columns = columns;
    _var = var;
    _getvalue = getvaluefunc;
    _getvaluedata = userdata;
    return self;
}

- (NSInteger) numberOfRowsInTableView:(NSTableView *) tableView {
    if(_var) {
        UiList *list = _var->value;
        if(list->count) {
            return list->count(list);
        }
    }
    return 0;
}

- (id) tableView:(NSTableView *) tableView
objectValueForTableColumn:(NSTableColumn *) tableColumn
             row:(NSInteger) row
{
    id ret = nil;
    UiList *list = _var->value;
    void *elm = list->get(list, (int)row);
    if(elm) {
        // get column index
        NSUInteger colIndex = [_columns indexOfObject:tableColumn];
        if(colIndex == NSNotFound) {
            return nil;
        }
        
        // get UI model type for this column
        UiModelType type = UI_STRING;
        UiModel *model = _model;
        if(model) {
            if(colIndex >= model->columns) {
                return nil;
            }
            type = model->types[colIndex];
        }
        
        // convert the list element
        UiBool freeResult = FALSE;
        void *data = _getvalue(list, elm, (int)row, (int)colIndex, _getvaluedata, &freeResult);
        
        switch(type) {
            case UI_STRING: {
                ret = [[NSString alloc] initWithUTF8String:data];
                break;
            }
            case UI_STRING_FREE: {
                ret = [[NSString alloc] initWithUTF8String:data];
                freeResult = TRUE;
                break;
            }
            case UI_INTEGER: {
                break;
            }
            case UI_ICON: {
                break;
            }
            case UI_ICON_TEXT: {
                break;
            }
            case UI_ICON_TEXT_FREE: {
                break;
            }
            case UI_STRING_EDITABLE: {
                break;
            }
            case UI_BOOL_EDITABLE: {
                break;
            }
        }
        
        if(freeResult) {
            free(data);
        }
    }
    return ret;
}

@end

@implementation ArrayDataSource

- (id)init:(char**)elements size:(size_t)nelm {
    _data = [[NSMutableArray alloc]init];
    for(int i=0;i<nelm;i++) {
        NSString *s = [[NSString alloc]initWithUTF8String:elements[i]];
        _data[i] = s;
    }
    return self;
}

- (NSInteger) numberOfRowsInTableView:(NSTableView *) tableView {
    return _data.count;
}

- (id) tableView:(NSTableView *) tableView
objectValueForTableColumn:(NSTableColumn *) tableColumn
             row:(NSInteger) row
{
    return _data[row];
}

@end
