/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2020 Ian McInerney <Ian.S.McInerney at ieee.org>
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <kiplatform/environment.h>

#include <memory>
#include <string>

#import <Cocoa/Cocoa.h>
#import <CFNetwork/CFNetwork.h>
#include <wx/osx/core/cfstring.h>
#include <wx/filefn.h>
#include <wx/stdpaths.h>
#include <wx/uri.h>


void KIPLATFORM::ENV::Init()
{
    // Disable the automatic window tabbing OSX does
    [NSWindow setAllowsAutomaticWindowTabbing:NO];

    // No tasks for this platform
}


bool KIPLATFORM::ENV::MoveToTrash( const wxString& aPath, wxString& aError )
{
    bool     isDirectory = wxDirExists( aPath );
    NSURL*   url = [NSURL fileURLWithPath:wxCFStringRef( aPath ).AsNSString() isDirectory:isDirectory];
    NSError* err = nullptr;

    BOOL result = [[NSFileManager defaultManager] trashItemAtURL:url resultingItemURL:nil error:&err];

    // Extract the error string if the operation failed
    if( result == NO )
    {
        NSString* errmsg;

        if( err.localizedRecoverySuggestion == nil )
        {
            errmsg = err.localizedFailureReason;
        }
        else
        {
            errmsg = [err.localizedFailureReason stringByAppendingFormat:@"\n\n%@",
                      err.localizedRecoverySuggestion];
        }

        aError = wxCFStringRef::AsString( (CFStringRef) errmsg );
        return false;
    }

    return true;
}


bool KIPLATFORM::ENV::IsNetworkPath( const wxString& aPath )
{
    // placeholder, we "nerf" behavior if its a network path so return false by default
    return false;
}


wxString KIPLATFORM::ENV::GetDocumentsPath()
{
    return wxStandardPaths::Get().GetDocumentsDir();
}


wxString KIPLATFORM::ENV::GetUserConfigPath()
{
    return wxStandardPaths::Get().GetUserConfigDir();
}


wxString KIPLATFORM::ENV::GetUserDataPath()
{
    return wxStandardPaths::Get().GetUserDataDir();
}


wxString KIPLATFORM::ENV::GetUserLocalDataPath()
{
    return wxStandardPaths::Get().GetUserLocalDataDir();
}


wxString KIPLATFORM::ENV::GetUserCachePath()
{
    NSURL* url = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory
                                                 inDomain:NSUserDomainMask
                                                 appropriateForURL:nil
                                                 create:NO error:nil];

    return wxCFStringRef::AsString( ( CFStringRef) url.path );
}


namespace
{

// Helper to extract proxy config from a resolved proxy dictionary
bool extractProxyFromDict( CFDictionaryRef aProxy, KIPLATFORM::ENV::PROXY_CONFIG& aCfg )
{
    CFStringRef proxyType = (CFStringRef) CFDictionaryGetValue( aProxy, kCFProxyTypeKey );

    if( !proxyType )
        return false;

    // Skip direct connections
    if( CFEqual( proxyType, kCFProxyTypeNone ) )
        return false;

    // Get proxy host
    CFStringRef host = (CFStringRef) CFDictionaryGetValue( aProxy, kCFProxyHostNameKey );

    if( !host )
        return false;

    // Clear any stale data from previous lookups
    aCfg.host.clear();
    aCfg.username.clear();
    aCfg.password.clear();

    // Determine scheme based on proxy type
    wxString scheme;

    if( CFEqual( proxyType, kCFProxyTypeHTTP ) )
        scheme = wxT( "http" );
    else if( CFEqual( proxyType, kCFProxyTypeHTTPS ) )
        scheme = wxT( "https" );
    else if( CFEqual( proxyType, kCFProxyTypeSOCKS ) )
        scheme = wxT( "socks5" );

    // Build scheme://host:port string
    if( !scheme.IsEmpty() )
        aCfg.host = scheme + wxT( "://" );

    wxString hostStr = wxCFStringRef::AsString( host );

    // Get port if present
    CFNumberRef port = (CFNumberRef) CFDictionaryGetValue( aProxy, kCFProxyPortNumberKey );
    int         portNum = 0;

    if( port )
        CFNumberGetValue( port, kCFNumberIntType, &portNum );

    // Bracket IPv6 addresses when port will be appended
    bool isIPv6 = hostStr.Contains( wxT( ":" ) );

    if( isIPv6 && portNum > 0 )
        aCfg.host += wxT( "[" ) + hostStr + wxT( "]" );
    else
        aCfg.host += hostStr;

    if( portNum > 0 )
        aCfg.host += wxString::Format( wxT( ":%d" ), portNum );

    // Get credentials if present
    CFStringRef username = (CFStringRef) CFDictionaryGetValue( aProxy, kCFProxyUsernameKey );
    CFStringRef password = (CFStringRef) CFDictionaryGetValue( aProxy, kCFProxyPasswordKey );

    if( username )
        aCfg.username = wxCFStringRef::AsString( username );

    if( password )
        aCfg.password = wxCFStringRef::AsString( password );

    return true;
}


// Evaluate a PAC script and extract proxy configuration
bool evaluatePACScript( CFURLRef aPacUrl, CFURLRef aTargetUrl, KIPLATFORM::ENV::PROXY_CONFIG& aCfg )
{
    // The caller may be a non-Cocoa std::thread, which has no top-level
    // autorelease pool. Wrap autoreleased Objective-C objects so they are
    // drained before returning, otherwise NSURLSession internals can hit
    // strict pool checks on macOS 26 ARM64 and trigger SIGTRAP.
    @autoreleasepool
    {
        // Synchronously fetch the PAC script
        NSURLRequest* request = [NSURLRequest requestWithURL:(__bridge NSURL*) aPacUrl
                                                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                             timeoutInterval:10.0];

        // The handler's NSData is autoreleased in the delegate queue's pool, which drains
        // as soon as the handler returns, so nothing Objective-C can be handed to the
        // waiting thread. Copy the bytes into a shared buffer instead: the block owns its
        // own reference, so a handler that fires after the wait times out still has
        // somewhere valid to write.
        auto script = std::make_shared<std::string>();

        // Block capture retains the semaphore (OS_OBJECT_USE_OBJC), so releasing our own
        // reference after the wait is safe even if the handler has not yet fired.
        dispatch_semaphore_t semaphore = dispatch_semaphore_create( 0 );

        NSURLSessionDataTask* task = [[NSURLSession sharedSession]
                  dataTaskWithRequest:request
                    completionHandler:^( NSData* data, NSURLResponse* response, NSError* error ) {
                        if( !error && data )
                            script->assign( (const char*) [data bytes], [data length] );

                        dispatch_semaphore_signal( semaphore );
                    }];

        [task resume];

        dispatch_time_t deadline = dispatch_time( DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC );
        long            waitResult = dispatch_semaphore_wait( semaphore, deadline );
        dispatch_release( semaphore );

        if( waitResult != 0 )
        {
            // Timed out. Cancel so the handler runs and drops its share of the buffer;
            // reading the buffer from here on would race with that handler.
            [task cancel];
            return false;
        }

        if( script->empty() )
            return false;

        NSString* scriptString = [[[NSString alloc] initWithBytes:script->data()
                                                           length:script->size()
                                                         encoding:NSUTF8StringEncoding] autorelease];

        if( !scriptString )
            return false;

        CFErrorRef error = nullptr;
        CFArrayRef pacProxies = CFNetworkCopyProxiesForAutoConfigurationScript(
                (__bridge CFStringRef) scriptString, aTargetUrl, &error );

        if( error )
        {
            CFRelease( error );
            return false;
        }

        if( !pacProxies )
            return false;

        bool success = false;

        for( CFIndex i = 0; i < CFArrayGetCount( pacProxies ) && !success; i++ )
        {
            CFDictionaryRef proxy = (CFDictionaryRef) CFArrayGetValueAtIndex( pacProxies, i );
            success = extractProxyFromDict( proxy, aCfg );
        }

        CFRelease( pacProxies );

        return success;
    }
}

} // anonymous namespace


bool KIPLATFORM::ENV::GetSystemProxyConfig( const wxString& aURL, PROXY_CONFIG& aCfg )
{
    // Background update threads (PCM, UPDATE_MANAGER) reach this function
    // from a bare std::thread that has no Cocoa autorelease pool installed.
    // CFNetworkCopySystemProxySettings / CFNetworkCopyProxiesForURL produce
    // autoreleased Objective-C objects internally; without a pool macOS 26
    // ARM64 trips a SIGTRAP shortly after launch. Install a pool for the
    // whole call so the platform code is safe regardless of the caller.
    @autoreleasepool
    {
        CFURLRef url = CFURLCreateWithString( kCFAllocatorDefault, wxCFStringRef( aURL ), nullptr );

        if( !url )
            return false;

        CFDictionaryRef proxySettings = CFNetworkCopySystemProxySettings();

        if( !proxySettings )
        {
            CFRelease( url );
            return false;
        }

        CFArrayRef proxies = CFNetworkCopyProxiesForURL( url, proxySettings );
        CFRelease( proxySettings );

        if( !proxies )
        {
            CFRelease( url );
            return false;
        }

        bool success = false;

        for( CFIndex i = 0; i < CFArrayGetCount( proxies ) && !success; i++ )
        {
            CFDictionaryRef proxy = (CFDictionaryRef) CFArrayGetValueAtIndex( proxies, i );
            CFStringRef     proxyType =
                    (CFStringRef) CFDictionaryGetValue( proxy, kCFProxyTypeKey );

            if( !proxyType )
                continue;

            // Handle PAC (Proxy Auto-Configuration) URLs
            if( CFEqual( proxyType, kCFProxyTypeAutoConfigurationURL ) )
            {
                CFURLRef pacUrl = (CFURLRef) CFDictionaryGetValue( proxy,
                                                                   kCFProxyAutoConfigurationURLKey );

                if( pacUrl )
                    success = evaluatePACScript( pacUrl, url, aCfg );

                continue;
            }

            success = extractProxyFromDict( proxy, aCfg );
        }

        CFRelease( proxies );
        CFRelease( url );

        return success;
    }
}


bool KIPLATFORM::ENV::VerifyFileSignature( const wxString& aPath )
{
    return true;
}


wxString KIPLATFORM::ENV::GetAppUserModelId()
{
    return wxEmptyString;
}


void KIPLATFORM::ENV::SetAppDetailsForWindow( wxWindow* aWindow, const wxString& aRelaunchCommand,
                                              const wxString& aRelaunchDisplayName )
{
}


wxString KIPLATFORM::ENV::GetCommandLineStr()
{
    return wxEmptyString;
}


void KIPLATFORM::ENV::AddToRecentDocs( const wxString& aPath )
{
}