#import <AppKit/AppKit.h>
#import <AVKit/AVKit.h>
#include <stdbool.h>

@interface DS4MediaWindowController : NSWindowController <NSWindowDelegate>
@end

static NSMutableSet<DS4MediaWindowController *> *DS4MediaWindows(void) {
    static NSMutableSet<DS4MediaWindowController *> *windows;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        windows = [NSMutableSet set];
    });
    return windows;
}

@implementation DS4MediaWindowController

- (void)windowWillClose:(NSNotification *)notification {
    AVPlayerView *view = (AVPlayerView *)self.window.contentView;
    [view.player pause];
    [DS4MediaWindows() removeObject:self];
}

@end

bool ds4_media_open(const char *url_bytes, const char *title_bytes, bool video) {
    if (url_bytes == NULL || title_bytes == NULL) {
        return false;
    }
    NSString *url_string = [NSString stringWithUTF8String:url_bytes];
    NSString *title = [NSString stringWithUTF8String:title_bytes];
    NSURL *url = [NSURL URLWithString:url_string];
    if (url == nil || title == nil) {
        return false;
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        NSRect frame = NSMakeRect(0, 0, video ? 800 : 560, video ? 500 : 180);
        NSWindow *window = [[NSWindow alloc]
            initWithContentRect:frame
                      styleMask:NSWindowStyleMaskTitled |
                                NSWindowStyleMaskClosable |
                                NSWindowStyleMaskMiniaturizable |
                                NSWindowStyleMaskResizable
                        backing:NSBackingStoreBuffered
                          defer:NO];
        window.title = title.length > 0 ? title : url.lastPathComponent;
        window.minSize = video ? NSMakeSize(480, 300) : NSMakeSize(420, 150);

        AVPlayerView *view = [[AVPlayerView alloc] initWithFrame:frame];
        view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
        view.player = [AVPlayer playerWithURL:url];
        window.contentView = view;

        DS4MediaWindowController *controller =
            [[DS4MediaWindowController alloc] initWithWindow:window];
        window.delegate = controller;
        [DS4MediaWindows() addObject:controller];
        [controller showWindow:nil];
        [window center];
        [window makeKeyAndOrderFront:nil];
        [view.player play];
    });
    return true;
}
