//
//  GlyphsFilterCoreProtocol.h
//  GlyphsCore
//
//  Created by Georg Seifert on 08.05.09.
//  Copyright 2009 schriftgestaltung.de. All rights reserved.
//

@class GSLayer;
@class GSFont;
@class GSFontMaster;
@class GSDialogController;
@class GSInstance;
@class GSGlyph;
@class GSFontMaster;
@class GSShape;

typedef NS_ENUM(uint8_t, GSExportContext) {
	GSExportContextStaticInstance = 1 << 1,
	GSExportContextVariable = 1 << 2,
};

#import <GlyphsCore/GSBase.h>

NS_ASSUME_NONNULL_BEGIN

/** The protocol defining the filter plugin interface */
@protocol GlyphsFilterCore

/// Glyphs will call that before the filter is invoked.
- (nullable NSError *)setup;

/** This method will be called if one layer is active in Edit View (the nodes are visible) Pay attention to the selection.

 @param layer   The active Layer
 @param error   if something goes wrong, retunes an NSError by reference.

 @return YES if successful, otherwise NO. In the later case the error will hold a explanation.
 */

- (BOOL)runFilterWithLayer:(GSLayer *)layer error:(out NSError **)error;

#ifndef LIBCORE
/// This method will be called if the hole glyph is supposed to be processed.
- (BOOL)runFilterWithLayers:(NSArray *)layers error:(out NSError **)error;
#endif

@optional

/// do main initializations
- (void)loadPlugin;

/// @name Export Filter Support

/// This will be call if the filter should be run from on export of the font. (see: Instance > Custom Properties)
- (BOOL)filterNeedsRemoveOverlap;

/** Is called to process font when called from a 'Filter' custom parameter

 @param font      The font to process
 @param arguments A list of arguments
 */
- (void)processFont:(GSFont *)font withArguments:(nullable NSArray *)arguments;

/** Is called to process font when called from a 'Filter' custom parameter

 @param font      The font to process
 @param arguments A list of arguments
 @param exportContext info about the export. e.g. if it is a variable font
 */
- (void)processFont:(GSFont *)font withArguments:(nullable NSArray *)arguments context:(GSExportContext)exportContext;

/** Is called to process a layer when to preview a Filter custom parameter

 @param layer     The layer to process
 @param arguments A list of arguments
 */
- (void)processLayer:(GSLayer *)layer withArguments:(nullable NSArray *)arguments;

/** Is called to process font when called from a 'PreInterpolationFilter' custom parameter
 @param glyph     The glyph to process
 @param arguments A list of arguments
 */
- (void)processGlyph:(GSGlyph *)glyph withArguments:(nullable NSArray *)arguments;

/** This method will be called if one layer is active in Edit View

 This is actually only used for the remove Overlap filter and might be removed when that one goes away.

 @param layer  a Layer
 @param options A dict containing some options. This might be "CheckSelection" : YES/NO.
 @param error   if something goes wrong, retunes an NSError by reference.

 @return YES if successful, otherwise NO. In the later case the error will hold a explanation.
 */
- (BOOL)runFilterWithLayer:(GSLayer *)layer options:(NSDictionary *)options error:(out NSError **)error;

/**
 Implement this method the filter is only meant to be use in instances > Filter parameters
 */
- (BOOL)visibleInMenu;

@end

@protocol GlyphsCallback <NSObject>

- (NSUInteger)callOrder;

/** Is called to process a font before output

@param font     The glyph to process
@param instance  the exported instance
@return NO if there was an error
*/

- (BOOL)processFont:(GSFont *)font instance:(GSInstance *)instance supporting:(GSDrawStackFeature)supportedFeatures error:(out NSError **)error;

/** Is called to process a glyph before output

 @param glyph     The glyph to process
 @param instance  the exported instance
 @return NO if there was an error
 */
- (BOOL)processGlyph:(GSGlyph *)glyph instance:(GSInstance *)instance supporting:(GSDrawStackFeature)supportedFeatures error:(out NSError **)error;

/** Is called to process the layer preparing it for preview

 @param layer     The layer to process
 @param extraHandles on return, a list of GSHandle objects that are shown as small black dots in the UI
 @return NO if there was an error
 */
- (BOOL)processLayer:(nonnull GSLayer *)layer supporting:(GSDrawStackFeature)supportedFeatures extraHandles:(nullable NSMutableArray *)extraHandles error:(out NSError **)error;

/** Is called to process a layer before output

 @param layer     The layer to process
 @param instance  the exported instance
 @return NO if there was an error
 */
- (BOOL)processLayer:(GSLayer *)layer instance:(GSInstance *)instance error:(out NSError **)error;

/** Is called to process some shapes before output

 This is used when color layers where each set of path with the same attributes has to be processed separately

 @param shapes     The shapes to process
 @param extraHandles on return, a list of GSHandle objects that are shown as small black dots in the UI
 @param didChange if there where any changes
 @return NO if there was an error
*/

+ (NSArray *)processShapes:(NSArray<GSShape *> *)shapes extraHandles:(nullable NSMutableArray *)extraHandles grid:(GSGridSettings)grid didChange:(BOOL*)didChange error:(out NSError **)error;

- (BOOL)interpolateMaster:(GSFontMaster *)master font:(GSFont *)font interpolation:(NSDictionary *)interpolation error:(out NSError **)error;

- (BOOL)interpolateLayer:(GSLayer *)layer glyph:(GSGlyph *)glyph interpolation:(NSDictionary *)interpolation scale:(CGFloat)scale error:(out NSError **)error;

- (void)font:(GSFont *)font scaledBy:(CGFloat)scale;

/**
 This is called when a document is opened.

 Please don’t don’t take too long as this will let the use user wait.

 To get to the path, use `font.parent.fileURL`.

 @param font The font object that was opened
 */

- (void)fontDidOpen:(GSFont *)font;

@optional

+ (NSArray *)processShapes:(NSArray<GSShape *> *)shapes supporting:(GSDrawStackFeature)supportedFeatures extraHandles:(nullable NSMutableArray *)extraHandles grid:(GSGridSettings)grid didChange:(BOOL*)didChange error:(out NSError **)error;

@end

NS_ASSUME_NONNULL_END
