/*******************************************************************************
 Copyright (c) 2004-2006 Jordy Rose
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 
 Except as contained in this notice, the name(s) of the above copyright holders
 shall not be used in advertising or otherwise to promote the sale, use or other
 dealings in this Software without prior authorization.
*******************************************************************************/

#import "DeleteDockController.h"

static const int kHeightDifference = 182 - 125;

/*!
 * Controls the panel used to delete docks. Part of Dockyard's Controller layer in the
 * MVC paradigm, as all decisions are relayed from the controls on the panel to the
 * <code>DockyardPreferences</code> instance.
 */
@implementation DeleteDockController
- (void)dealloc
{
	[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
	[super dealloc];
}

- (void)windowDidLoad
{
//	preferences = [menuExtra preferences];
	isShowingSwitchView = NO;
	
	NSWindow *window = [self window];
	[window setLevel:NSStatusWindowLevel];
	[window center];
	
	[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(dockyardDockChanged:) name:DockyardDockChangedNotification object:nil];
}

- (void)dockyardDockChanged:(NSNotification *)note
{
	[switchListPopUp selectItemWithTitle:[[note userInfo] objectForKey:@"newDock"]];
	if ([switchListPopUp indexOfSelectedItem] == 0)
		[dockListPopUp selectItemAtIndex:1];
	else
		[dockListPopUp selectItemAtIndex:0];
}

- (IBAction)showWindow:(id)sender
{
	[switchListPopUp selectItemWithTitle:[preferences currentDock]];
	if ([switchListPopUp indexOfSelectedItem] == 0)
		[dockListPopUp selectItemAtIndex:1];
	else
		[dockListPopUp selectItemAtIndex:0];
	[self selectDockToDelete:dockListPopUp];
	[super showWindow:sender];
}

/*!
 * Selects the dock to delete. If it is the current dock, shows another pop up button
 * asking which dock to switch to before the deletion. If it is not and the other button
 * is showing, this method hides it. The show/hide process is actually animated, with the
 * window sliding smoothly down or up when the button is added or removed.
 */
- (IBAction)selectDockToDelete:(NSPopUpButton *)sender
{
	NSString *selected = [sender titleOfSelectedItem];
	NSString *currentDock = [preferences currentDock];
	NSWindow *window = [self window];
	
	if ([currentDock isEqualToString:selected] && !isShowingSwitchView) {
		NSRect frame = [window frame];
		frame.size.height += kHeightDifference;
		frame.origin.y -= kHeightDifference;
		[window setFrame:frame display:YES animate:YES];
		[[window contentView] addSubview:switchView positioned:NSWindowBelow relativeTo:dockListPopUp];
		isShowingSwitchView = YES;
		
		int index = [dockListPopUp indexOfSelectedItem];
		if ([switchListPopUp indexOfSelectedItem] == index)
			[switchListPopUp selectItemAtIndex:(index == 0) ? 1 : 0];
		
	} else if (isShowingSwitchView) {
		[switchView removeFromSuperview];
		NSRect frame = [window frame];
		frame.size.height -= kHeightDifference;
		frame.origin.y += kHeightDifference;
		[window setFrame:frame display:YES animate:YES];
		isShowingSwitchView = NO;
	}
}

/*!
 * Selects the dock to switch to after deleting the current dock. Makes sure that the dock
 * being switched to and the dock being deleted are not the same.
 */
- (IBAction)selectDockToSwitch:(NSPopUpButton *)sender
{
	NSString *selected = [sender titleOfSelectedItem];
	NSString *currentDock = [preferences currentDock];
	
	if ([currentDock isEqualToString:selected]) {
		NSBeep();
		if ([sender indexOfSelectedItem] == 0)
			[sender selectItemAtIndex:1];
		else
			[sender selectItemAtIndex:0];
	}
}

/*!
 * Calls <code>deleteDockWithName:</code> on <code>DockyardPreferences</code>, as well as closing
 * the delete window. The dock is chosen using an NSPopUpButton. If the dock being deleted is the
 * current dock, this method will also call <code>changeDock:</code> to switch to another dock.
 *
 * @see DockyardPreferences#deleteDockWithName:
 */
- (IBAction)confirm:(id)sender
{
	NSString *selected = [[dockListPopUp titleOfSelectedItem] retain];
	NSString *switchToDock = [[switchListPopUp titleOfSelectedItem] retain];

//	[menuExtra deleteDockWithName:selected];
	NSString *currentDock = [preferences currentDock];
	if ([currentDock isEqualToString:selected]) {
		[preferences changeDock:switchToDock];
	}
	[preferences deleteDockWithName:selected];
	
//	[deleteDockWindow performClose:sender];
	[self close];
	
	[switchListPopUp selectItemWithTitle:[preferences currentDock]];
	if ([switchListPopUp indexOfSelectedItem] == 0)
		[dockListPopUp selectItemAtIndex:1];
	else
		[dockListPopUp selectItemAtIndex:0];
	[self selectDockToDelete:dockListPopUp];
	
	[switchToDock release];
	[selected release];
}
@end
