//
//  GSEditViewController.h
//  Glyphs
//
//  Created by Georg Seifert on 21.4.08.
//  Copyright 2008 schriftgestaltung.de. All rights reserved.
//

#import <GlyphsCore/GlyphsCore.h>
#import <GlyphsApp/GSGlyphViewController.h>

NS_ASSUME_NONNULL_BEGIN
@class GSWindowController;
@class GSGlyphEditView;
@class GSFrameView;
@class GSFontMaster;
@class GSLayer;
@class GSGlyphsPreview;
#if !TARGET_OS_IOS // TODO
@class MDPSplitView;
#endif
@class GSColorBackgroundView;
@class MABGTimer;
@class GSGlyphDetailDialog;
@class GSInspectorViewController;

@interface GSEditViewController : GSGlyphViewController <NSSplitViewDelegate> {
	GSGlyphEditView *__unsafe_unretained _graphicView;
	GSFrameView *__unsafe_unretained _frameView;
	NSPopUpButton *__unsafe_unretained _singleLayerSelect;
	NSArrayController *__unsafe_unretained _fontMastersController;
	id layerInstance;
	NSUInteger _masterIndex;
	NSArray *selection;
	IBOutlet NSButton *__unsafe_unretained _showBackgroundButton;
	GSLayer *_shadowLayer;
	BOOL work;
	NSString *_selectedScript;

#ifndef GLYPHS_LITE
	NSPopUpButton *__unsafe_unretained _featurePopupButton;
	NSView *__unsafe_unretained _featurePopupButtonContainer;
	NSPopUpButton *__unsafe_unretained _instancePopupButton;
	NSView *__unsafe_unretained _instancePopupButtonContainer;
	NSInteger _selectedInstance;
	NSButton *__unsafe_unretained _previewButton;
#endif
	GSGlyphsPreview *__unsafe_unretained _previewView;
#if !TARGET_OS_IOS
	MDPSplitView *__unsafe_unretained _previewSplitView;
#else
	UISplitViewController *__unsafe_unretained _previewSplitView;
#endif
	NSView *__unsafe_unretained _previewBackgroundView;

	NSButton *__unsafe_unretained _zoomInButton;
	NSButton *__unsafe_unretained _zoomOutButton;
	NSButton *__unsafe_unretained _negativeButton;
	NSView *__unsafe_unretained _negativeButtonContainer;
	NSButton *__unsafe_unretained _mirrorButton;
	NSView *__unsafe_unretained _mirrorButtonContainer;
	NSSlider *__unsafe_unretained _blurSlider;
	NSView *__unsafe_unretained _blurSliderContainer;

	NSTextField *__unsafe_unretained _sizeTextField;
	NSArray *_inspectorViewControllers;
	NSArray *_extraInspectorViewControllers;
	NSStackView * __weak _infoView;
	CGFloat _infoViewExtraOffset;
	NSLayoutConstraint *_infoViewHorizontalConstraint;
	NSLayoutConstraint *_infoViewVerticalConstraint;
	BOOL _showInfoBox;
	BOOL _centerInfoBox;
#ifndef GLYPHS_LITE
	NSMutableArray *_panelInspectors;
#endif
}

@property (weak) GSWindowController *windowController;
#if !TARGET_OS_IOS // TODO
@property (strong, nonatomic) NSSplitViewController *mainSplitViewController;
#endif
@property (unsafe_unretained, nonatomic) IBOutlet GSGlyphEditView *graphicView;
@property (unsafe_unretained, nonatomic) IBOutlet GSFrameView *frameView;
@property (unsafe_unretained, nonatomic) IBOutlet NSView *contentView;
@property (strong, nonatomic) NSViewController *contentViewController;
@property (unsafe_unretained, nonatomic) IBOutlet NSPopUpButton *singleLayerSelect;
@property (unsafe_unretained, readonly, nonatomic) NSArray *allLayers;
@property (unsafe_unretained, readonly, nonatomic) NSArray *selectedLayers;
@property (unsafe_unretained, readonly, nonatomic, nullable) GSLayer *activeLayer;
#ifndef GLYPHS_LITE
@property (unsafe_unretained, nonatomic) IBOutlet NSArrayController *fontMastersController;
#endif
@property (unsafe_unretained, nonatomic) IBOutlet NSButton *showBackgroundButton;
#ifndef GLYPHS_LITE
@property (unsafe_unretained, nonatomic) IBOutlet NSPopUpButton *writingDirectionControl;
@property (unsafe_unretained, nonatomic) IBOutlet NSPopUpButton *featurePopupButton;
#endif
@property (unsafe_unretained, nonatomic) IBOutlet GSColorBackgroundView *bottomView;
@property (unsafe_unretained, nonatomic) IBOutlet NSStackView *bottomToolbar;
#ifndef GLYPHS_LITE
@property (unsafe_unretained, nonatomic) IBOutlet NSView *leftSidebarView;
@property (strong, nonatomic) NSViewController *leftSidebarViewController;
@property (unsafe_unretained, nonatomic) IBOutlet NSTabView *leftSidebarTabView;
#endif
#if !TARGET_OS_IOS // TODO
@property (unsafe_unretained, nonatomic) IBOutlet NSSegmentedControl *leftSidebarSegmentedControl;
#endif

#ifndef GLYPHS_LITE
@property (assign, nonatomic) NSUInteger selectedSidebarTabBarIndex;

@property (nonatomic, weak) IBOutlet NSButton *featureButton;
@property (weak) IBOutlet NSButton *checkerboardToggleButton;
@property (weak) NSView *checkerboardToggleButtonContainer;
@property (nonatomic, retain) NSMutableArray *featureArray;

@property (unsafe_unretained, nonatomic) IBOutlet NSPopUpButton *instancePopupButton;
#endif
@property (unsafe_unretained, nonatomic) IBOutlet GSGlyphsPreview *previewView;
#if !TARGET_OS_IOS // TODO
@property (unsafe_unretained, nonatomic) IBOutlet MDPSplitView *previewSplitView;
#endif
@property (unsafe_unretained, nonatomic) IBOutlet NSView *previewBackgroundView;
@property (unsafe_unretained, nonatomic) IBOutlet NSBox *bottomToolbarContainer;

@property (unsafe_unretained, nonatomic) IBOutlet NSButton *zoomInButton;
@property (unsafe_unretained, nonatomic) IBOutlet NSButton *zoomOutButton;
@property (nonatomic, unsafe_unretained) IBOutlet NSTextField *sizeTextField;

@property (nonatomic, unsafe_unretained) IBOutlet NSButton *negativeButton;
#ifndef GLYPHS_LITE
@property (nonatomic, unsafe_unretained) IBOutlet NSButton *mirrorButton;
#endif
@property (nonatomic, unsafe_unretained) IBOutlet NSSlider *blurSlider;

@property (nonatomic, strong) GSGlyphDetailDialog *glyphDetailWindow;

@property (assign) BOOL showInfoBox;
@property (assign) BOOL centerInfoBox;

@property (readonly) NSMutableDictionary <NSString *, GSInspectorViewController *> *infoBoxCache;

#ifdef GLYPHS_LITE
@property (nonatomic, assign) BOOL drawBlack;
#endif

@property (assign) NSUInteger masterIndex;
- (nullable GSFontMaster *)activeMaster;

@property (strong, nonatomic) NSArray *selection;
@property (readonly, nonatomic) NSMutableSet *selectedFeatures;
@property (readonly, nonatomic, nullable) NSArray *allFeatures;
@property (readonly, nonatomic) NSString *selectedScript;
@property (strong, nonatomic, nullable) GSLayer *shadowLayer;
@property (readonly, nonatomic) BOOL hasWeightAxis;
@property (readonly, nonatomic) CGFloat minWeightValue;
@property (readonly, nonatomic) CGFloat maxWeightValue;
@property (assign, nonatomic) CGFloat weightValue;
/// The effective writing direction used to layout the layers.
///
/// If `usesAutomaticWritingDirection` is enabled, setting this property will revert immediately to the inherent writing direction.
@property (assign, nonatomic) GSWritingDirection writingDirection;
/// If enabled, the effective writing direction is derived from the inherent direction of the glyphs of the layers.
@property (assign, nonatomic) BOOL usesAutomaticWritingDirection;
@property (assign, nonatomic) BOOL doShaping;

@property (assign, nonatomic) BOOL work;
#ifndef GLYPHS_LITE
@property (nonatomic, assign) NSInteger selectedInstance;
#endif

// TODO: actually implement this
@property (nonatomic, readonly, strong) NSDictionary *currentAxisValues;

@property (nonatomic, unsafe_unretained) IBOutlet NSButton *previewButton;
@property (nonatomic, unsafe_unretained) IBOutlet NSPopUpButton *kerningControl;

@property (nonatomic, strong, nullable) NSDictionary *tempData;

- (void)setFrameSize:(NSSize)frameSize previewHeight:(CGFloat)previewHeight previewVisible:(BOOL)previewVisible;

- (void)initialZoom;

- (NSUndoManager *)activeMasterUndoManager;

- (void)didGetSelected;

- (void)didUnselect;

- (IBAction)closeTab:(id)sender;

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

/// is called when the status bar needs updating
- (void)updateFormatBar:(NSString *)keyPath;

- (void)updatePreview;

- (IBAction)singleLayerSelect:(id)sender;

- (IBAction)showBackground:(id)sender;

- (void)scrollEditViewToGlyphsAtIndex:(NSUInteger)index;

#ifndef GLYPHS_LITE
- (IBAction)selectFeature:(id)sender;

- (IBAction)selectInstance:(id)sender;

- (void)setMasterIndex:(NSUInteger)masterIndex;

- (void)clearManuallyConfiguredGlyphs;

- (void)_updateFeaturePopup;

- (void)_updateInstancePopup;

- (IBAction)insertPlaceholder:(id)sender;

- (IBAction)insertAllMasters:(id)sender;

- (void)addViewToBottomToolbar:(NSView *)aView;

- (IBAction)correctPathDirection:(id)sender;

- (IBAction)correctPathDirectionForAllMasters:(id)sender;

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

@property (nonatomic, assign) CGFloat previewHeight;

@property (nonatomic, assign) GSWritingDirection previewOrientation;

- (void)reflow;

/// call to update the graphicView
- (void)redraw;

/// applies changes to the state of the edit view in changBlock but keeps the center of the active layer at the same place
- (void)reflowKeepActiveContentCenteredWithCallback:(void (^)(id self))changeBlock;

/**
 Applies changes to the state of the edit view in changBlock. Allow to customize the what position prior to the change change and after will be synced.
 e.g. when centering on the selection
 */
- (void)reflowKeepActiveContentCenteredWithCallback:(void (^)(id self))changeBlock origCenter:(NSPoint (^ _Nullable)(GSLayer *oldActiveLayer))origCenterBlock newCenter:(NSPoint (^ _Nullable)(GSLayer *newActiveLayer))newCenterBlock;

- (IBAction)showGlyphInFontViewAction:(id)sender;

- (void)toggleSidebar:(nullable id)sender;

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

- (void)roundCoordinates:(id)sender;

- (void)cleanUpPaths:(id)sender;

- (IBAction)updateWritingDirection:(id)sender;

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

@property (nonatomic, weak) IBOutlet NSStackView *infoView;

@end
NS_ASSUME_NONNULL_END
