/*
 * 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 "text.h"
#import "EventData.h"
#import "Container.h"
#import <objc/runtime.h>

UIWIDGET ui_textarea_create(UiObject *obj, UiTextAreaArgs args) {
    NSTextView *textview = [[NSTextView alloc] init];
    textview.autoresizingMask = NSViewWidthSizable;
    textview.minSize = NSMakeSize(0, 0);
    textview.maxSize = NSMakeSize(FLT_MAX, FLT_MAX);
    
    NSScrollView *scrollview = [[NSScrollView alloc] init];
    scrollview.hasVerticalScroller = YES;
    scrollview.documentView = textview;
    
    UiLayout layout = UI_INIT_LAYOUT(args);
    ui_container_add(obj, scrollview, &layout, TRUE);
    
    
    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_TEXT);
    if(var) {
        UiText *text = var->value;
        text->obj = (__bridge void*)textview;
        ui_textarea_restore(text);
        
        text->save = ui_textarea_save;
        text->destroy = ui_textarea_destroy;
        text->restore = ui_textarea_restore;
        text->set = ui_textarea_set;
        text->get = ui_textarea_get;
        text->getsubstr = ui_textarea_getsubstr;
        text->insert = ui_textarea_insert;
        text->setposition = ui_textarea_setposition;
        text->position = ui_textarea_position;
        text->setselection = ui_textarea_setselection;
        text->selection = ui_textarea_selection;
        text->length = ui_textarea_length;
        text->remove = ui_textarea_remove;
    }
    
    return (__bridge void*)scrollview;
}




void ui_textarea_save(UiText *text) {
    
}

void ui_textarea_destroy(UiText *text) {
    (void)(__bridge_transfer NSTextStorage*)text->data1;
}

void ui_textarea_restore(UiText *text) {
    NSTextView *textview = (__bridge NSTextView*)text->obj;
    NSTextStorage *textStorage;
    if(text->data1) {
        textStorage = (__bridge NSTextStorage*)text->data1;
        
    } else {
        textStorage = [[NSTextStorage alloc] init];
    }
    [textview.layoutManager replaceTextStorage:textStorage];
    text->data1 = (__bridge_retained void*)textStorage;
}

void ui_textarea_set(UiText *text, const char *str) {
    
}

char* ui_textarea_get(UiText *text) {
    return NULL;
}

char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
    return NULL;
}

void ui_textarea_insert(UiText *text, int pos, char *str) {
    
}

void ui_textarea_setposition(UiText *text, int pos) {
    
}

int ui_textarea_position(UiText *text) {
    return 0;
}

void ui_textarea_setselection(UiText *text, int begin, int end) {
    
}

void ui_textarea_selection(UiText *text, int *begin, int *end) {
    
}

int ui_textarea_length(UiText *text) {
    return 0;
}

void ui_textarea_remove(UiText *text, int begin, int end) {
    
}
