import platform,sys,os,pathlib

fallbackCompanionPluginName = 'Font Proofer Companion'

# Make the plugin version queryable from the RoboFont scripting window.
#
# The version lives in the uniquely-named, side-effect-free sibling module
# fontProoferCompanionVersion.py (kept out of this file because extension.py runs
# startup logic below and can't be safely re-imported, and out of the compiled
# fpcompanion package so it stays readable when no compiled build exists for the
# user's Python). But RoboFont does NOT put an installed extension's lib/ folder
# on sys.path for the scripting window, so `import fontProoferCompanionVersion`
# would fail there on its own.
#
# So load that sibling module by path now — at startup, before the fpcompanion
# logic below, so it works even when the compiled fpcompanion can't load — and
# register it in sys.modules under its unique name. RoboFont shares one Python
# interpreter, so a later `import fontProoferCompanionVersion` in the scripting
# window then resolves to this already-loaded module. The module is side-effect
# free, and we never expose the colliding `extension` name.
try :
    import importlib.util
    _fpcVersionModuleName = 'fontProoferCompanionVersion'
    if _fpcVersionModuleName not in sys.modules :
        _fpcLibFolder = os.path.dirname( os.path.realpath( __file__ ) )
        _fpcVersionModulePath = os.path.join( _fpcLibFolder, _fpcVersionModuleName + '.py' )
        _fpcVersionSpec = importlib.util.spec_from_file_location( _fpcVersionModuleName, _fpcVersionModulePath )
        _fpcVersionModule = importlib.util.module_from_spec( _fpcVersionSpec )
        _fpcVersionSpec.loader.exec_module( _fpcVersionModule )
        sys.modules[ _fpcVersionModuleName ] = _fpcVersionModule
except Exception :
    pass

def showUnsupportedPythonAlert(pythonVersion):
    """
    Pure-AppKit alert, scheduled onto the next main run loop pass
    so it never blocks the host app's startup / extension loading.
    """
    from AppKit import NSAlert, NSAlertStyleCritical
    from Foundation import NSOperationQueue
    def showAlert():
        alert = NSAlert.alloc().init()
        alert.setMessageText_( f'{fallbackCompanionPluginName} couldn’t load' )
        alert.setInformativeText_( f'Python {pythonVersion} isn’t supported by this version of {fallbackCompanionPluginName} (yet).\n\nPlease check for a plugin update, or switch to a supported Python version.' )
        alert.setAlertStyle_(NSAlertStyleCritical)
        alert.addButtonWithTitle_('OK')
        alert.runModal()
    NSOperationQueue.mainQueue().addOperationWithBlock_(showAlert)

arch = {'arm':'arm64','i386':'x86_64'}.get(platform.processor())
binaryFolder = None
if arch :
    binaryFolder = os.path.join(os.path.join(pathlib.Path(os.path.dirname(os.path.realpath(__file__))).parent,'resources/fpcompanion/versions'),arch+"_"+(str(sys.version_info[0])+str(sys.version_info[1])))

if binaryFolder is None or not os.path.isdir(binaryFolder) :
    showUnsupportedPythonAlert( str(sys.version_info[0])+'.'+str(sys.version_info[1]) )
    print( f'{fallbackCompanionPluginName} doesn’t support Python {sys.version_info[0]}.{sys.version_info[1]} on this architecture (yet), so it won’t load.' )
else :
    sys.path.insert(0,binaryFolder)
    from fpcompanion import companion
    fpcInstance = companion.FontProoferCompanion()
    fpcInstance.start()
