
#import "LRFilterBar.h"
#import "RBSplitView.h"
#import "LRTableView.h"
#import "AquaTabsControl.h"
#import "WFFilePicker.h"
#import "WBCalendarControl.h"

#import "CalendarController.h"

#import <Cocoa/Cocoa.h>

@class AboutWindow;
@class MultipleSelection;
@class Preferences;
@class ReadMe;
@class FinalsCalculator;
@class GPACalculator;
@class CoursesAndTextbooks;

@interface Controller : NSObject
{	
	// Main Window Items
	IBOutlet NSWindow *mainWindow;
	bool programDidSuccessfullyStartUp;
	
	IBOutlet RBSplitView *mainWindowSplitView;
	IBOutlet NSView *drawerSplitSubview;
	IBOutlet RBSplitSubview *mainSplitSubview;
	IBOutlet NSView* dragView;
	
	IBOutlet NSPanel* detachedSidebarWindow;
	
	IBOutlet NSView *assignmentView;
	IBOutlet NSView *coursesView;
	
	IBOutlet NSTabView *assignmentDetailsTabView;
	IBOutlet AquaTabsControl *assignmentDetailsTabBar;
	
	IBOutlet NSTabView *formattingTabView;
	IBOutlet NSView *formatChangeView;
    IBOutlet NSTableView *assignmentTable;
	NSMutableDictionary *tableColumns;
	IBOutlet NSMenu *assignmentTableContextualMenuNoSelection;
	IBOutlet NSMenu *assignmentTableContextualMenuSelection;
	
	IBOutlet FilterBar *filterBar;
	
	// Main Window - Toolbar
	NSToolbar *mainToolbar;
	IBOutlet NSSearchField *toolbarSearchField;
	NSToolbarItem *searchBoxToolbarItem;
	IBOutlet NSView *searchField;
	IBOutlet NSSegmentedControl *formatToolbarItem;
	
	// Assignment Drawer - No Selection
	IBOutlet NSTextField *personalNameField;
	IBOutlet NSTextField *personalSchoolField;
	IBOutlet NSTextField *personalStudentIDField;
	
	IBOutlet NSTableView *coursesAndTextbooksTable;
	IBOutlet NSBox *labelsCoursesAndTextbooksBox;
	
	// Assignment Drawer - Assignment View
	IBOutlet NSTextField *nameField;
	IBOutlet NSTabView *dateEntryTabView;
    IBOutlet NSTextField *dateField;
	IBOutlet NSPopUpButton *datePopupMonth;
	IBOutlet NSPopUpButton *datePopupDay;
	
	IBOutlet NSPopUpButton *datePopupYear;
	IBOutlet NSButton *todayButton;
	IBOutlet NSButton *courseButton;
    IBOutlet NSComboBox *courseField;
	NSString *originalCourseString;
	IBOutlet NSButton *textbookButton;
    IBOutlet NSComboBox *textbookField;
	IBOutlet NSButton *completedButton;
    IBOutlet NSComboBox *typeField;
    IBOutlet NSTextField *gradeField;
	IBOutlet NSSlider *priorityField;
	IBOutlet WFFilePicker *fileField;
	IBOutlet NSButton *snapshotField;
    IBOutlet LRTableView *notesField;
    IBOutlet NSTableView *subprojectsTableView;
	
	IBOutlet NSTokenField *addressBookField;
	
	IBOutlet NSButton *labelsAssignmentDrawerCourseButton;
	IBOutlet NSButton *labelsAssignmentDrawerTextbookButton;
	IBOutlet NSTextField *labelsAssignmentDrawerTypeField;
	IBOutlet NSTextField *labelsAssignmentDrawerGradeField;
	
	IBOutlet NSButton *addSubprojectButton;
	IBOutlet NSButton *removeSubprojectButton;
	
	// Registration Window
	IBOutlet NSMenu *appMenu;
	
	// Menu Items
	IBOutlet NSMenuItem *toggleAssignmentDrawerMenuItem;
	IBOutlet NSMenuItem *detachSidebarMenuItem;
	IBOutlet NSMenuItem *addMenuItem;
	IBOutlet NSMenuItem *addMultipleMenuItem;
	IBOutlet NSMenuItem *addProjectMenuItem;
	IBOutlet NSMenuItem *deleteMenuItem;
	IBOutlet NSMenuItem *emailMenuItem;
	IBOutlet NSMenuItem *duplicateMenuItem;
	IBOutlet NSMenuItem *exportToiCal;
	IBOutlet NSMenuItem *exportToText;
	IBOutlet NSMenuItem *assignmentWindowMenuItem;
	IBOutlet NSMenuItem *coursesAndTextbooksMenuItem;
	IBOutlet NSMenuItem *formatListMenuItem;
	IBOutlet NSMenuItem *formatWeeklyCalendarMenuItem;
	IBOutlet NSMenuItem *formatMonthlyCalendarMenuItem;
	IBOutlet NSMenuItem *goToTodayMenuItem;
	IBOutlet NSMenuItem *nextMonthMenuItem;
	IBOutlet NSMenuItem *previousMonthMenuItem;
	
	IBOutlet NSMenuItem *detailsSidebarMenuItem;
	IBOutlet NSMenuItem *notesSidebarMenuItem;
	IBOutlet NSMenuItem *subprojectsSidebarMenuItem;
	
	// Menu Items for Checkmarks
	IBOutlet NSMenuItem *menuitemMainWindow;
	IBOutlet NSMenuItem *menuitemCourseWindow;
	IBOutlet NSMenuItem *menuitemGPACalcWindow;
	IBOutlet NSMenuItem *menuitemFinalsCalcWindow;
	
	// Mini Calendar
	IBOutlet NSWindow *miniCalendarWindow;
    IBOutlet WBCalendarControl *miniCalendar;
    IBOutlet NSTextField *miniCalendarMonthYear;
    NSCalendarDate *miniCalendarDate;
	
	// Sorting Stuff
    BOOL didJustSort;
	
	// Data Sources
	NSMutableArray *subset, *assignmentTableSortingSelectors;
	NSMutableArray *coursesAndTextbooksArray;
	
	// Export to Text
	IBOutlet NSPanel *exportWindow;
	IBOutlet NSProgressIndicator *exportProgressBar;
	
	// Filter and Search
	IBOutlet NSMenuItem *searchAll;
	IBOutlet NSMenuItem *searchName;
	IBOutlet NSMenuItem *searchCourse;
	IBOutlet NSMenuItem *searchTextbook;
	IBOutlet NSMenuItem *searchType;
	IBOutlet NSMenuItem *searchGrade;
	IBOutlet NSMenuItem *searchNotes;
	NSMutableArray *allAssignments, *filteredAssignments, *displayAssignments;
}

- (id)init;
+ (void)initialize;
+ (Controller *)sharedController;
- (void)awakeFromNib;
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification;
- (void)applicationWillResignActive:(NSNotification *)aNotification;
- (void)applicationWillTerminate:(NSNotification *)aNotification;
- (void)saveData;
- (void)windowWillClose:(NSNotification *)aNotification;
- (void)windowDidResize:(NSNotification *)aNotification;

#pragma mark -
#pragma mark Sidebar
- (IBAction)toggleAssignmentDrawer:(id)sender;
- (void)detachSidebarAtStartup;
- (IBAction)detachSidebar:(id)sender;
- (void)detachSidebarWithAnimation:(BOOL)animation;
- (void)attachSidebarWithAnimation:(BOOL)animation;
- (void)setSidebarView:(NSView *)subView;

#pragma mark -
#pragma mark Export to Text
- (IBAction)exportAssignmentsToiCal:(id)sender;
- (IBAction)exportAssignmentsToText:(id)sender;

#pragma mark -
#pragma mark Window Opening
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
					hasVisibleWindows:(BOOL)flag;
- (void)windowDidBecomeKey:(NSNotification *)aNotification;
- (void)windowDidResignKey:(NSNotification *)aNotification;
- (void)updateWindowInformation;
- (void)updateWindowCheckmarks;
- (IBAction)openForum:(id)sender;
- (IBAction)openWebsite:(id)sender;
- (IBAction)installWidget:(id)sender;

#pragma mark -
#pragma mark Assignment Management
- (IBAction)addAssignment:(id)sender;
- (int)addAssignmentWithDate:(NSDate *)date;
- (IBAction)editAssignment:(id)sender;
- (IBAction)duplicateAssignment:(id)sender;
- (void)controlTextDidBeginEditing:(NSNotification *)aNotification;
- (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
- (IBAction)updateTextbooksForCourse:(id)sender;
- (IBAction)deleteAssignment:(id)sender;
- (IBAction)markAssignmentAsCompleted:(id)sender;

#pragma mark -
#pragma mark Data Source Delegate
- (int)numberOfRowsInTableView:(NSTableView *)aTableView;
- (id)tableView:(NSTableView *)aTableView
	objectValueForTableColumn:(NSTableColumn *)aTableColumn 
	row:(int)rowIndex;

#pragma mark -
#pragma mark Assignment Table
- (void)clearAssignmentDrawer;
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification;
- (BOOL)validateMenuItem:(NSMenuItem*)anItem;
- (void)tableView:(NSTableView *)aTableView
  willDisplayCell:(id)aCell
   forTableColumn:(NSTableColumn *)aTableColumn
			  row:(int)rowIndex;
- (void)colorTableView:(NSTableView *)aTableView
				  cell:(id)aCell
		forTableColumn:(NSTableColumn *)aTableColumn
				   row:(int)rowIndex
				object:(id)object;
- (NSColor *)createColor:(NSString *)string;
- (void)tableViewColumnDidResize:(NSNotification *)aNotification;
- (void)tableView:(NSTableView *)tableView
didDragTableColumn:(NSTableColumn *)tableColumn;
- (IBAction)doubleClickedAssignmentTable:(id)sender;

#pragma mark -
#pragma mark Sorting Stuff
- (void)tableView:(NSTableView *)tableView
didClickTableColumn:(NSTableColumn *)tableColumn;
- (void)sortColumn:(NSTableColumn *)tableColumn;

#pragma mark -
#pragma mark Assignment Drawer
- (void)tabView:(NSTabView *)tabView
didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (void)setAssignmentDrawerNextKeyView:(NSTabView *)tabView;
- (IBAction)dateToToday:(id)sender;
- (IBAction)increaseDate:(id)sender;
- (IBAction)decreaseDate:(id)sender;
- (void)addDay:(int)inDay;
- (BOOL)textShouldEndEditing:(NSText *)aTextObject;
- (IBAction)changeDateWithPullDownBoxes:(id)sender;
- (void)updateDatePullDown;
- (IBAction)openCourseOrTextbookDetails:(id)sender;
- (IBAction)selectDetailsTabWithIdentifier:(id)sender;
- (IBAction)openVideoWindow:(id)sender;
- (void)snapshotImageWasTaken:(NSImage *)image;

#pragma mark -
#pragma mark Toolbar Delegate
- (void)setupToolbar;
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
	 itemForItemIdentifier:(NSString *)itemIdentifier
 willBeInsertedIntoToolbar:(BOOL)flag;
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem;
- (IBAction)setFormatView:(id)sender;

#pragma mark -
#pragma mark Filter and Search
- (void)controlTextDidChange:(NSNotification *)aNotification;
- (void)filterWithViews:(NSString *)viewName
			 andCourses:(NSString *)courseName;
- (IBAction)searchAssignments:(id)sender;
- (IBAction)setSearchSpecifications:(id)sender;

#pragma mark -
#pragma mark Email Assignment
- (IBAction)emailAssignment:(id)sender;
- (NSString *)stringByReplacingAmpersands:(NSString *)originalString;

#pragma mark -
#pragma mark Multiple
- (IBAction)addMultiple:(id)sender;

#pragma mark -
#pragma mark Registration
- (IBAction)showRegistration:(id)sender;

#pragma mark -
#pragma mark About Window
- (IBAction)showAboutWindow:(id)sender;

#pragma mark -
#pragma mark Assignment Drawer - Multiple Selection
- (NSView*)showMultipleSelectionView;

#pragma mark -
#pragma mark Preferences Window
- (IBAction)showPreferencesWindow:(id)sender;

#pragma mark -
#pragma mark Backup
- (IBAction)exportToBackup:(id)sender;
- (IBAction)importFromBackup:(id)sender;

#pragma mark -
#pragma mark Read Me
- (IBAction)showReadMeWindow:(id)sender;

#pragma mark -
#pragma mark Calculators
- (IBAction)showFinalsCalculator:(id)sender;
- (IBAction)showGPACalculator:(id)sender;

#pragma mark -
#pragma mark Courses And Textbooks
- (IBAction)showCoursesAndTextbooksWindow:(id)sender;

#pragma mark -
#pragma mark Accessors
- (NSTableView *)assignmentTable;
- (NSMutableArray *)displayAssignments;

#pragma mark -
#pragma mark Split View
- (void)didAdjustSubviews:(RBSplitView*)sender;
- (unsigned int)splitView:(RBSplitView*)sender
dividerForPoint:(NSPoint)point
inSubview:(RBSplitSubview*)subview;
- (void)splitView:(RBSplitView*)sender didCollapse:(RBSplitSubview*)subview;
- (void)selectDetailsTab:(int)tabIndex;
- (void)resizeSubViewToView:(NSView *)subview;

#pragma mark -
#pragma mark Projects
- (NSArray *)subprojectsForProject:(NSString *)project;
- (IBAction)addProject:(id)sender;
- (IBAction)addProjectWithDate:(NSDate *)date;
- (IBAction)addSubproject:(id)sender;
- (IBAction)deleteSubproject:(id)sender;
- (void)tableView:(NSTableView *)aTableView
   setObjectValue:(id)anObject
   forTableColumn:(NSTableColumn *)aTableColumn
			  row:(int)rowIndex;
- (NSArray *)subprojectsForProject:(NSString *)project;
- (int)findIndexOfProjectForSubproject:(NSString *)project;
- (id)actualSelectedObject;
- (NSString *)getSubprojectDisplayName:(id)subproject;

#pragma mark -
#pragma mark Mini Calendar
- (IBAction)showMiniCalendar:(id)sender;
- (void)addMonths:(int)inMonths andYears:(int)inYears;
- (IBAction)decreaseMonth:(id)sender;
- (IBAction)increaseMonth:(id)sender;
- (IBAction)setDay:(id)sender;

#pragma mark -
#pragma mark Priorities
- (void)addPrioritiesToAssignmentsWithoutThem;

#pragma mark -
#pragma mark Syncing
- (IBAction)syncData:(id)sender;

#pragma mark -
#pragma mark Calendar
- (IBAction)goToNextOrPreviousMonth:(id)sender;
- (void)changeSelectedAssignmentDateTo:(NSCalendarDate *)newDate;

#pragma mark -
#pragma mark Filter Bar
- (void)updateFilterBar;
- (IBAction)selectNewItem:(id)sender;

@end
