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

static NSDictionary *standardIconNames;

#define UI_ICON_ENTRY(x) @#x : x

void ui_icon_init(void) {
    standardIconNames = @{
        UI_ICON_ENTRY(NSImageNameAddTemplate),
        UI_ICON_ENTRY(NSImageNameBluetoothTemplate),
        UI_ICON_ENTRY(NSImageNameBonjour),
        UI_ICON_ENTRY(NSImageNameBookmarksTemplate),
        UI_ICON_ENTRY(NSImageNameCaution),
        UI_ICON_ENTRY(NSImageNameComputer),
        UI_ICON_ENTRY(NSImageNameEnterFullScreenTemplate),
        UI_ICON_ENTRY(NSImageNameExitFullScreenTemplate),
        UI_ICON_ENTRY(NSImageNameFolder),
        UI_ICON_ENTRY(NSImageNameFolderBurnable),
        UI_ICON_ENTRY(NSImageNameFolderSmart),
        UI_ICON_ENTRY(NSImageNameFollowLinkFreestandingTemplate),
        UI_ICON_ENTRY(NSImageNameHomeTemplate),
        UI_ICON_ENTRY(NSImageNameIChatTheaterTemplate),
        UI_ICON_ENTRY(NSImageNameLockLockedTemplate),
        UI_ICON_ENTRY(NSImageNameLockUnlockedTemplate),
        UI_ICON_ENTRY(NSImageNameNetwork),
        UI_ICON_ENTRY(NSImageNamePathTemplate),
        UI_ICON_ENTRY(NSImageNameQuickLookTemplate),
        UI_ICON_ENTRY(NSImageNameRefreshFreestandingTemplate),
        UI_ICON_ENTRY(NSImageNameRefreshTemplate),
        UI_ICON_ENTRY(NSImageNameRemoveTemplate),
        UI_ICON_ENTRY(NSImageNameRevealFreestandingTemplate),
        UI_ICON_ENTRY(NSImageNameShareTemplate),
        UI_ICON_ENTRY(NSImageNameSlideshowTemplate),
        UI_ICON_ENTRY(NSImageNameStatusAvailable),
        UI_ICON_ENTRY(NSImageNameStatusNone),
        UI_ICON_ENTRY(NSImageNameStatusPartiallyAvailable),
        UI_ICON_ENTRY(NSImageNameStatusUnavailable),
        UI_ICON_ENTRY(NSImageNameStopProgressFreestandingTemplate),
        UI_ICON_ENTRY(NSImageNameStopProgressTemplate),
        UI_ICON_ENTRY(NSImageNameTrashEmpty),
        UI_ICON_ENTRY(NSImageNameTrashFull),
        UI_ICON_ENTRY(NSImageNameActionTemplate),
        UI_ICON_ENTRY(NSImageNameSmartBadgeTemplate),
        UI_ICON_ENTRY(NSImageNameIconViewTemplate),
        UI_ICON_ENTRY(NSImageNameListViewTemplate),
        UI_ICON_ENTRY(NSImageNameColumnViewTemplate),
        UI_ICON_ENTRY(NSImageNameFlowViewTemplate),
        UI_ICON_ENTRY(NSImageNameInvalidDataFreestandingTemplate),
        UI_ICON_ENTRY(NSImageNameGoForwardTemplate),
        UI_ICON_ENTRY(NSImageNameGoBackTemplate),
        UI_ICON_ENTRY(NSImageNameGoRightTemplate),
        UI_ICON_ENTRY(NSImageNameGoLeftTemplate),
        UI_ICON_ENTRY(NSImageNameRightFacingTriangleTemplate),
        UI_ICON_ENTRY(NSImageNameLeftFacingTriangleTemplate),
        UI_ICON_ENTRY(NSImageNameMobileMe),
        UI_ICON_ENTRY(NSImageNameMultipleDocuments),
        UI_ICON_ENTRY(NSImageNameUserAccounts),
        UI_ICON_ENTRY(NSImageNamePreferencesGeneral),
        UI_ICON_ENTRY(NSImageNameAdvanced),
        UI_ICON_ENTRY(NSImageNameInfo),
        UI_ICON_ENTRY(NSImageNameFontPanel),
        UI_ICON_ENTRY(NSImageNameColorPanel),
        UI_ICON_ENTRY(NSImageNameUser),
        UI_ICON_ENTRY(NSImageNameUserGroup),
        UI_ICON_ENTRY(NSImageNameEveryone),
        UI_ICON_ENTRY(NSImageNameUserGuest),
        UI_ICON_ENTRY(NSImageNameMenuOnStateTemplate),
        UI_ICON_ENTRY(NSImageNameMenuMixedStateTemplate),
        UI_ICON_ENTRY(NSImageNameApplicationIcon)
    };
}

NSImage* ui_cocoa_named_icon(const char *name) {
    NSString *imageName = [[NSString alloc] initWithUTF8String:name];
    //NSString *imgName = [standardIconNames objectForKey:imageName];
    //if(imgName) {
    //    imageName = imgName;
    //}
    //return [NSImage imageNamed:imageName];
    return [NSImage imageWithSystemSymbolName:imageName accessibilityDescription:nil];
}

static UIIMAGE create_image(NSImage *image) {
    UiImage *img = malloc(sizeof(UiImage));
    img->nsimage = (__bridge_retained void*)image;
    img->ref = 1;
    return img;
}

void ui_image_ref(UIIMAGE img) {
    UiImage *image = img;
    image->ref++;
}

void ui_image_unref(UIIMAGE img) {
    UiImage *image = img;
    if(--image->ref == 0) {
        CFRelease(image->nsimage);
        free(img);
    }
}

static int load_image(UiGeneric *obj, NSImage *img) {
    UIIMAGE image = create_image(img);
    
    if(obj->set) {
        obj->set(obj, image, UI_IMAGE_OBJECT_TYPE);
        ui_image_unref(image);
    } else {
        obj->value = image;
        obj->type = UI_IMAGE_OBJECT_TYPE;
    }
    
    return 0;
}

int ui_image_load_file(UiGeneric *obj, const char *path) {
    NSString *str = [[NSString alloc]initWithUTF8String:path];
    NSImage *img = [[NSImage alloc]initWithContentsOfFile:str];
    if(img == nil) {
        return 1;
    }
    return load_image(obj, img);
}

int ui_image_load_data(UiGeneric *obj, const void *imgdata, size_t size) {
    NSData *data = [NSData dataWithBytes:(void*)imgdata length:size];
    NSImage *img = [[NSImage alloc] initWithData:data];
    if(img == nil) {
        return 1;
    }
    return load_image(obj, img);
}
