//
//  MIVideoController.m
//  Montage
//
//  Created by Marc Palluat de Besset on Fri Mar 29 2002.
//  Copyright (c) 2002 Arboretum Systems, Inc.. All rights reserved.
//

#ifndef ZzTypes_h
#include "ZzTypes.h"
#endif //ZzTypes_h

#import "MIVideoController.h"
#import "MIDocumentController.h"
#import "MIDocumentModel.h"
#import "MIPickScreenController.h"
#import "MIPaletteController.h"
#import "MIApplication.h"
#import "MenuTags.h"

#ifndef TArmeMasterMovie_h
#include "TArmeMasterMovie.h"
#endif //TArmeMasterMovie_h

#ifndef TArmeDoc_h
#include "TArmeDoc.h"
#endif //TArmeDoc_h

NSString *MTGVideoVisibleAtLaunch = @"VideoVisible";

@implementation MIVideoController

+ (void)addDefaultValuesToDictionary:(NSMutableDictionary*)defaultValues {
    [defaultValues setObject:[NSNumber numberWithBool:YES] forKey:MTGVideoVisibleAtLaunch];
}

+ (MIVideoController*)sharedVideoController {
    static MIVideoController *sharedVideoController = nil;
    if ( sharedVideoController == nil ) {
        sharedVideoController = [[self alloc] init];
    }
    return sharedVideoController;
}

- (NSString*)nibName {
    return @"VideoWindow";
}

- (MIVideoController*)init {
    if (( self = [super init] )) {
		fHalfSizeMenuItem = [NSApp searchForMenuItemWithTag:MTGHalfSizeVideoMenuTag];
		fNormalSizeMenuItem = [NSApp searchForMenuItemWithTag:MTGNormalSizeVideoMenuTag];
		fDoubleSizeMenuItem = [NSApp searchForMenuItemWithTag:MTGDoubleSizeVideoMenuTag];
		fFullScreenMenuItem = [NSApp searchForMenuItemWithTag:MTGFullScreenVideoMenuTag];
		[fHalfSizeMenuItem setTarget:self];
		[fHalfSizeMenuItem setAction:@selector(viewBackstageHalfx:)];
		[fNormalSizeMenuItem setTarget:self];
		[fNormalSizeMenuItem setAction:@selector(viewBackstage1x:)];
		[fDoubleSizeMenuItem setTarget:self];
		[fDoubleSizeMenuItem setAction:@selector(viewBackstage2x:)];
		[fFullScreenMenuItem setTarget:self];
		[fFullScreenMenuItem setAction:@selector(viewTheater:)];
		
//        NSRect videoWindowRect = NSMakeRect(984, 69, 290, 242);
        NSRect videoWindowRect = NSMakeRect(954, 69, 320, 262);
        fFullScreenVideoWindow = [[MIFullScreenVideoWindow alloc] initWithFrame:videoWindowRect];
        fBackstageVideoWindow = [[MIVideoWindow alloc] initWithFrame:videoWindowRect];
        [fBackstageVideoWindow setTitle:@"Viewer"];
        NSDocumentController* documentController = [NSDocumentController sharedDocumentController];
        MIMontageDocument* activeDoc = [documentController currentDocument];
        [self setActiveDocument:activeDoc];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentDocumentChanged:) name:MTGCurrentDocumentChangedNotification object:documentController];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(documentAdded:) name:MTGDocumentAddedNotification object:documentController];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil];
        [fBackstageVideoWindow setFrameUsingName:@"VideoWindow"];
        BOOL visible = [[NSUserDefaults standardUserDefaults] boolForKey:MTGVideoVisibleAtLaunch];
        [[MIPaletteController sharedPaletteController] registerPaletteController:self visibleAfterLaunch:visible];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MTGCurrentDocumentChangedNotification object:[NSDocumentController sharedDocumentController]];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:MTGDocumentAddedNotification object:[NSDocumentController sharedDocumentController]];
    [fFullScreenVideoWindow release];
    [fBackstageVideoWindow release];

    [super dealloc];
}

- (void)saveUserDefaults:(BOOL)shouldBeVisible
{
        [[NSUserDefaults standardUserDefaults] setBool:shouldBeVisible forKey:MTGVideoVisibleAtLaunch];
    [fBackstageVideoWindow saveFrameUsingName:@"VideoWindow"];
}

- (void)applicationWillTerminate:(NSNotification*)theNotification
{
    [self release];
}

- (void)currentDocumentChanged:(NSNotification*)notification {
    NSDictionary* notificationDictionary = [notification userInfo];
    MIMontageDocument* activeDoc = nil;
    if ( notificationDictionary ) {
        activeDoc = [notificationDictionary objectForKey:MTGCurrentDocumentKey];
    }
    [self setActiveDocument:activeDoc];
}

- (void)documentAdded:(NSNotification*)notification {
    NSDictionary* notificationDictionary = [notification userInfo];
    
    if ( notificationDictionary ) {
		MIMontageDocument* addedDoc = nil;
		NSSize newSize;
		
        addedDoc = [notificationDictionary objectForKey:MTGAddedDocumentKey];

		float magnification = addedDoc->fModel->fViewerMagnification;

		newSize.width = [addedDoc armeDocument]->fMasterMovie->GetWidth() * magnification;
		newSize.height = [addedDoc armeDocument]->fMasterMovie->GetHeight() * magnification;
		[fBackstageVideoWindow setContentSize:newSize];
		[fBackstageVideoWindow setViewsNeedDisplay:YES];

		[fHalfSizeMenuItem setState:NSOffState];
		[fNormalSizeMenuItem setState:NSOffState];
		[fDoubleSizeMenuItem setState:NSOffState];
		[fFullScreenMenuItem setState:NSOffState];

		if ( magnification == 0.5 ) [fHalfSizeMenuItem setState:NSOnState];
		if ( magnification == 1 ) [fNormalSizeMenuItem setState:NSOnState];
		if ( magnification == 2 ) [fDoubleSizeMenuItem setState:NSOnState];
    }
}

- (void)setActiveDocument:(MIMontageDocument*)activeDocument {
    if (activeDocument) {
        TArmeDoc* armeDoc = [activeDocument armeDocument];
        ZzAssert(armeDoc);
        TArmeMasterMovie* masterMovie = armeDoc->fMasterMovie;
        ZzAssert(masterMovie);
        [fBackstageVideoWindow attachMovie:masterMovie->GetMovie()];
    } else [fBackstageVideoWindow attachMovie:nil];
}


- (IBAction)showBackstage:(id)sender {
    if ([fFullScreenVideoWindow isVisible]) {
        [fBackstageVideoWindow takeMovieView:[fFullScreenVideoWindow giveMovieView]];
    }
    [fBackstageVideoWindow orderFront:self];
}

- (IBAction)show:(id)sender {
    [self showBackstage:sender];
}

- (IBAction)hide:(id)sender {
    [fBackstageVideoWindow orderOut:sender];
}

- (IBAction)toggleShowHide:(id)sender {
    if ( [self isVisible] ) [self hide:sender];
    else [self show:sender];
}


- (BOOL)isVisible {
    return [fBackstageVideoWindow isVisible];
}

- (void)viewBackstage:(float)magnification {
    // note that the full screen window will call this with sender self
    // on a mouse or keydown while in full screen mode
    [fBackstageVideoWindow setMagnification:magnification];
    if ([fFullScreenVideoWindow isVisible]) {
        [fBackstageVideoWindow takeMovieView:[fFullScreenVideoWindow giveMovieView]];
    }
    //[fBackstageVideoWindow makeKeyAndOrderFront:self];
    [fBackstageVideoWindow orderFront:self];
}

- (IBAction)viewBackstageHalfx:(id)sender {
	[fHalfSizeMenuItem setState:NSOnState];
    [fNormalSizeMenuItem setState:NSOffState];
    [fDoubleSizeMenuItem setState:NSOffState];
	[fFullScreenMenuItem setState:NSOffState];
	[self showBackstage:sender];
    [self viewBackstage:0.5];
}

- (IBAction)viewBackstage1x:(id)sender {
	[fHalfSizeMenuItem setState:NSOffState];
    [fNormalSizeMenuItem setState:NSOnState];
    [fDoubleSizeMenuItem setState:NSOffState];
	[fFullScreenMenuItem setState:NSOffState];
	[self showBackstage:sender];
    [self viewBackstage:1];
}

- (IBAction)viewBackstage2x:(id)sender {
	[fHalfSizeMenuItem setState:NSOffState];
    [fNormalSizeMenuItem setState:NSOffState];
    [fDoubleSizeMenuItem setState:NSOnState];
	[fFullScreenMenuItem setState:NSOffState];
	[self showBackstage:sender];
    [self viewBackstage:2];
}

- (float)magnification {
	return fBackstageVideoWindow->fMagnification;
}

- (IBAction)restoreBackstageView:(id)sender {
	[NSApp sendAction:[fActiveMenuItem action] to:[fActiveMenuItem target] from:fActiveMenuItem];
	fActiveMenuItem = nil;
}

- (IBAction)viewTheater:(id)sender
{
    if (![fFullScreenVideoWindow isVisible]) {
        NSScreen* screen = [[MIPickScreenController sharedPickScreenController] pickScreen];

        if (screen) {
            [fFullScreenVideoWindow takeMovieViewAndFill:[fBackstageVideoWindow giveMovieView] onScreen:screen];
			if ( [fHalfSizeMenuItem state] == NSOnState ) fActiveMenuItem = fHalfSizeMenuItem;
			if ( [fNormalSizeMenuItem state] == NSOnState ) fActiveMenuItem = fNormalSizeMenuItem;
			if ( [fDoubleSizeMenuItem state] == NSOnState ) fActiveMenuItem = fDoubleSizeMenuItem;
			[fHalfSizeMenuItem setState:NSOffState];
			[fNormalSizeMenuItem setState:NSOffState];
			[fDoubleSizeMenuItem setState:NSOffState];
			[fFullScreenMenuItem setState:NSOnState];
        }
    }
}


- (IBAction)viewBackstage4x:(id)sender {
    [self viewBackstage:4];
}

- (NSWindow*)window {
    return fBackstageVideoWindow;
}

- (BOOL)validateMenuItem:(NSMenuItem *)anItem {
	if ( [[NSDocumentController sharedDocumentController] currentDocument] != NULL ) return YES;
	else return NO;
}


// this function is currently obsolete; it was hooked to the "Switch Full Screen" menu item.
// see -viewTheater and -viewBackstage for current equivalents.
/*- (IBAction)switchFullScreen:(id)sender
{
    if ([fFullScreenVideoWindow isVisible])
    {
        [sender setState:NSOffState];
        [fBackstageVideoWindow takeMovieView:[fFullScreenVideoWindow giveMovieView]];
    }
    else
    {
        NSScreen* screen = [self pickScreen];
        if (screen)
        {
            [sender setState:NSOnState];
            [fFullScreenVideoWindow takeMovieViewAndFill:[fBackstageVideoWindow giveMovieView] onScreen:screen];
        }
        else
            [sender setState:NSOffState];
    }
    //
    // if([fVideoWindow isFullScreen])
   //  {
    //     [sender setState:NSOffState];
   //      [fVideoWindow setFullScreen:FALSE];
   //  }
  //   else
  //   {
    //     if ([fVideoWindow setFullScreen:YES])
      //       [sender setState:NSOnState];
     //}
    verntl(videoAlex,5,"switchFullScreen called");
}*/


@end
