/*
 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2014 ownCloud GmbH
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "cocoainitializer.h"

#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSProcessInfo.h>
#import <AppKit/NSApplication.h>

#include "application.h"

#include <QTimer>

/* In theory, we should be able to just capture QFileOpenEvents
 * when we open our custom URLs in our Application class and be
 * done with it, but in practice the QFileOpenEvent often doesn't
 * get sent for our URLs. We have this in place to work around
 * the issue.
 *
 * This class sets a callback selector on URL-related events
 * before the application is fully done launching. This lets us
 * properly receive and process "open url" events even if the
 * client was closed when these events were sent. */

@interface URLEventHandler : NSObject
@end

@implementation URLEventHandler
- (id)init {
    self = [super init];

    if (self) {
        NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
        [defaultCenter addObserver:self
                        selector:@selector(applicationWillFinishLaunching:)
                        name:NSApplicationWillFinishLaunchingNotification
                        object:nil];
    }
    return self;
}

- (void)applicationWillFinishLaunching:(NSNotification *)aNotification {
    [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
                                                       andSelector:@selector(handleURLEvent:withReplyEvent:)
                                                     forEventClass:kInternetEventClass
                                                        andEventID:kAEGetURL];
}

- (void)handleURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
    NSURL* url = [NSURL URLWithString:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];
    const auto qtUrl = QUrl::fromNSURL(url);
    if (qApp) {
        QTimer::singleShot(0, qApp, [qtUrl] {
            qCInfo(OCC::lcApplication) << "macOS AppleEvent custom URI scheme request:"
                                       << "scheme=" << qtUrl.scheme()
                                       << "host=" << qtUrl.host()
                                       << "path=" << qtUrl.path();
            if (auto application = qobject_cast<OCC::Application *>(qApp)) {
                application->handleUriSchemeRequest(qtUrl);
            } else {
                qCWarning(OCC::lcApplication) << "Could not dispatch macOS AppleEvent custom URI scheme request because qApp is not an Application.";
            }
        });
    } else {
        qCWarning(OCC::lcApplication) << "Could not dispatch macOS AppleEvent custom URI scheme request because qApp is not available yet.";
    }
}

@end

namespace OCC {
namespace Mac {

class CocoaInitializer::Private {
public:
    NSAutoreleasePool* autoReleasePool;
    URLEventHandler* handler;
    id<NSObject> appNapActivity = nil;
};

CocoaInitializer::CocoaInitializer() {
    d = new CocoaInitializer::Private();
    d->handler = [[URLEventHandler alloc] init];
    NSApplicationLoad();
    d->autoReleasePool = [[NSAutoreleasePool alloc] init];

    // As an LSUIElement agent we usually have no visible window, which makes macOS
    // throttle us via App Nap and lets RunningBoard suspend (and later jetsam-reap)
    // the process while it is idle in the background. Holding an NSActivityBackground
    // assertion for our whole lifetime keeps the process out of App Nap so syncing
    // continues; it still allows normal idle system/display sleep.
    NSProcessInfo *const processInfo = [NSProcessInfo processInfo];
    d->appNapActivity = [[processInfo beginActivityWithOptions:NSActivityBackground
                                                        reason:@"Keep syncing while running in the background"] retain];
}

CocoaInitializer::~CocoaInitializer() {
    if (d->appNapActivity) {
        [[NSProcessInfo processInfo] endActivity:d->appNapActivity];
        [d->appNapActivity release];
    }
    [d->autoReleasePool release];
    delete d;
}

} // namespace Mac
} // namespace OCC
