//
//  GSWindowController.h
//  Glyphs
//
//  Created by Georg Seifert on Thu Mar 07 2007.
//  Copyright (c) 2010 schriftgestaltung.de. All rights reserved.
//

#import <GlyphsApp/GlyphsPaletteProtocol.h>
#import <GlyphsApp/GlyphsToolDrawProtocol.h>
#import <GlyphsApp/GlyphsToolEventProtocol.h>
#import <GlyphsApp/GlyphsToolProtocol.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(uint8_t, GSFontInfoTabIndex) {
	GSFontInfoTabIndexFont = 0,
	GSFontInfoTabIndexMasters = 1,
	GSFontInfoTabIndexExports = 2,
	GSFontInfoTabIndexFeatures = 3,
	GSFontInfoTabIndexDocument = 4,
	GSFontInfoTabIndexNotes = 5,
};

@class GSDocument;
@class GSFontViewController;
@class GSEditViewController;
@class GSGlyphViewController;
@class GSGlyphEditView;
@class GSLayer;

@class GSTabBarControl;
@class GSInfoWindowController;
@class GSGenerateGlyphsWindow;
@class GSSelectDocumentController;

@protocol TabBarControlDelegate;

@protocol GSGlyphViewControllerProtocol;

@interface GSWindowController : NSWindowController <NSToolbarDelegate, NSAnimationDelegate, NSWindowDelegate /* <TabBarControlDelegate */>

/// Convenience getter casting the return value of -[GSWindowController document] to GSDocument.
@property (nonatomic, readonly, nullable) GSDocument *glyphsDocument;

@property (strong, nonatomic, nullable) GSTabBarControl *tabBarControl;
@property (strong, nonatomic, nullable) NSData *lastClosedTab;
@property (nonatomic) NSUInteger lastClosedTabIndex;
//@property (unsafe_unretained, nonatomic) IBOutlet NSView *contentView;
//@property (weak, nonatomic) IBOutlet NSScrollView *panelScrollView;

@property (strong, nonatomic) GSSelectDocumentController *selectDocumentController;

@property (retain, nonatomic, null_resettable) NSArray <GlyphsTool> *toolInstances;

@property (retain, nonatomic, nullable) NSArray *toolGroups;

@property (assign, nonatomic) NSInteger selectedToolGroupIndex;

@property (unsafe_unretained, nonatomic, nullable) NSResponder<GlyphsTool, GlyphsToolDraw> *toolDrawDelegate;
@property (unsafe_unretained, nonatomic, nullable) NSResponder<GlyphsTool, GlyphsToolEvent> *toolEventDelegate;
@property (readonly, nonatomic, nullable) NSResponder<GlyphsTool, GlyphsToolEvent> *toolEventDelegateSelected;

@property (readonly, nonatomic) NSResponder<GlyphsTool, GlyphsToolEvent> *toolEventHandler;
@property (unsafe_unretained, nonatomic, nullable) id toolTempSelection;

@property (unsafe_unretained, nonatomic, nullable) GSGlyphEditView *graphicView;

@property (nonatomic) BOOL showLayer;

@property (weak, nonatomic) IBOutlet NSArrayController *glyphsController;
@property (strong, nonatomic) IBOutlet NSArrayController *fontMastersController;
@property (unsafe_unretained, readonly) NSArray *allLayers;

/** the currently active layer of the window

 This is only valid if one layer has direct focus. If more than one are selected, this returns nil.

 @return the active layer.
 */
@property (readonly, nonatomic, nullable) GSLayer *activeLayer;

@property (unsafe_unretained, readonly) NSArray *selectedLayers;

@property (readonly, nonatomic, nullable) NSArray *selectedLayersFast;

/** The index of the active master

 @return The index of the active master
 */
@property (nonatomic) NSUInteger masterIndex;
@property (strong, nonatomic) NSString *exportPath;
/// if the Shift key is currently pressed
@property (nonatomic) bool ShiftKey;
/// if the alt/option key is currently pressed
@property (nonatomic) bool AltKey;
/// if the Ctrl key is currently pressed
@property (nonatomic) bool CtrlKey;
/// if the Command key is currently pressed
@property (nonatomic) bool CommandKey;
/// if the Space key is currently pressed
@property (nonatomic) bool SpaceKey;


@property (strong, nonatomic) NSViewController *replaceViewController;
@property (weak, nonatomic) IBOutlet NSTextField *searchText;
@property (weak, nonatomic) IBOutlet NSTextField *replaceText;
@property (strong, nonatomic) GSGenerateGlyphsWindow *generateGlyphsWindowController;
@property (weak, nonatomic) IBOutlet NSButton *showSidebarButton;

@property (strong, readonly) GSFontViewController *fontViewController;
#if TARGET_OS_IOS // not sure if this is the best solution, but since tabs are managed differently on ios, this is required at the moment
@property (weak) GSEditViewController *currentEditViewController;
#endif

#ifndef GLYPHS_LITE
/// Same as `-setMasterIndex:` except that this method accepts indices into the display masters of the currently active glyph view controller.
- (void)setDisplayMasterIndex:(NSUInteger)masterIdx;
#endif

- (void)updateFont;

- (void)reloadToolPlugins:(NSArray<Class> *)newPluginClasses;

- (void)reloadPalettePlugins:(NSArray<Class> *)paletteClasses;

- (nullable NSUndoManager *)activeMasterUndoManager;

- (void)restoreDisplayStrings:(NSArray *)displayStrings;

- (IBAction)generateGlyphs:(id)sender;

#ifndef GLYPHS_LITE
- (IBAction)updateGlyphInfo:(id)sender;
#endif

- (IBAction)generateAnchors:(id)sender;

- (void)updateAnchorsForLayer:(GSLayer *)layer;

- (IBAction)reGenerateAnchors:(id)sender;

- (void)resetAnchorsForLayer:(GSLayer *)layer;

#ifndef GLYPHS_LITE
- (IBAction)generateInstances:(id)sender;
#endif

- (IBAction)makeComponentGlyph:(id)sender;

- (NSArray *)displayStrings;

- (NSArray *)displayStrings:(GSFormatVersion)format;

- (void)runFilterAction:(id)sender;

- (void)setupPlugins;

- (void)checkTempTool;

//- (IBAction)replace:(id)sender;

@property (nonatomic, readonly, nullable) GSInfoWindowController *fontInfoWindowController;

- (IBAction)showFontInfo:(id)sender;

- (IBAction)exportFontAction:(id)sender;

- (IBAction)exportAllFontsAction:(id)sender;

- (void)showGlyphInFontView:(GSGlyph *)glyph;

- (void)showTabAtIndex:(NSInteger)idx;

- (id)viewControllerAtIndex:(NSInteger)idx;

- (IBAction)showPreviousTab:(id)sender;

- (IBAction)showNextTab:(id)sender;

- (IBAction)newEditPage:(id)sender;

- (IBAction)closeEditPage:(id)sender;

- (void)closeTabItem:(NSViewController *)tab;


/** The activate a tool based on its class

 @param aClass the class of the tool that should be activated.
 */
- (void)setToolForClass:(Class)aClass;

- (nullable id)toolForClass:(Class)aClass;

- (IBAction)updateMetrics:(id)sender;

#if TARGET_OS_IOS
- (IBAction)updateAllMetrics:(id)sender;

- (IBAction)decomposeComponents:(id)sender;
#endif

- (IBAction)assignBackground:(id)sender;

- (IBAction)cancelDialog:(nullable id)sender;

- (void)showUndoNotification:(NSNotification *)notification;

- (IBAction)showNotificationBox:(NSString *)notification;

- (void)closeNotificationBox:(id)userInfo;

- (IBAction)importAfm:(id)sender;

- (void)toolsToolbarClicked:(id)sender;

- (void)modifierChanged:(NSEvent *)anEvent;

- (nullable NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window;

#ifndef GLYPHS_LITE
- (void)setPanelInspectors:(nullable NSArray *)panelInspectors;
#endif


/** The index of the tool that was active before the current.

 @see selectedToolGroupIndex
 */
@property (nonatomic, weak) NSResponder<GlyphsTool> *lastSelectTool;

/** The index of the currently active tool
 @see lastSelectTool
 */
@property (nonatomic, weak) NSResponder<GlyphsTool> *selectedTool;

@property (unsafe_unretained, nonatomic, nullable) IBOutlet NSArrayController *glyphListController;

/// Convenience getter for self.glyphsDocument.font.
@property (nonatomic, readonly, nullable) GSFont *documentFont;


/** The active editView controller or nil if the font view is active

 @return the active edit view controller
 */

@property (unsafe_unretained, readonly, nonatomic, nullable) GSEditViewController *activeEditViewController;

@property (unsafe_unretained, readonly, nonatomic, nullable) GSGlyphViewController *activeViewController;


/** Show the font info window

 @param tabIdx the index of the tab that should be activated
 */
- (nullable NSViewController *)showFontInfoWindowWithTabSelected:(GSFontInfoTabIndex)tabIdx;

#ifndef GLYPHS_LITE
- (nullable GSEditViewController *)addTabWithDisplayString:(nonnull NSString *)displayString;

- (nullable GSEditViewController *)addTabWithDisplayString:(NSString *)displayString validate:(BOOL)validate;

- (nullable GSEditViewController *)addTabWithString:(NSString *)string;
#endif

- (nullable GSEditViewController *)addTabWithLayers:(nonnull NSArray<GSLayer *> *)layers;

- (nullable GSEditViewController *)addTabWithGlyphs:(nonnull NSArray<GSGlyph *> *)glyphs;

- (nullable GSEditViewController *)addTabWithGlyphNames:(nonnull NSArray<NSString *> *)glyphNames;

@end

NS_ASSUME_NONNULL_END
