/*
        PreferencesController.m

	PreferencesController : This file is part of SlideShow

	Copyright (C) 2004 Fabien VALLON
	Additional copyrights here

	Authors: Fabien VALLON <fabien@sonappart.net>
	Date:	17 August 2004

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License as
	published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public
	License along with this program; if not, write to:

		Free Software Foundation, Inc.
		59 Temple Place - Suite 330
		Boston, MA  02111-1307, USA
*/

#include "PreferencesController.h"

#include "InputView.h"

#include <Foundation/NSNotification.h>

#include <AppKit/NSBrowser.h>
#include <AppKit/NSBrowserCell.h>
#include <AppKit/NSButton.h>
#include <AppKit/NSColorWell.h>
#include <AppKit/NSNibLoading.h>
#include <AppKit/NSPanel.h>
#include <AppKit/NSPopUpButton.h>
#include <AppKit/NSTabView.h>
#include <AppKit/NSTextField.h>



//static NSString *COLORCHANGE = @"COLORCHANGE";
static NSString *PREFERENCEPOPUPTAG = @"PREFERENCEPOPUPTAG";
static NSString *PREFERENCESFRAME = @"PREFERENCESFRAME";

//tags views
#define GENERAL  0
#define SHORTCUT 1

//PreferenceController is implement as singleton
static PreferencesController *sharedPreferences = nil;
@implementation PreferencesController

-(id) init
{
  if ( sharedPreferences )
    [self dealloc];
  else
    {
      if ( ! ( self = [super init] ) ) 
	return nil;
      //Load Preferences.gorm
      {
	if ( ! [NSBundle loadNibNamed:@"Preferences" owner:self] )
	  {
	    NSLog(@"Cannot load bundle Preferences");
	    return nil; // Not sure
	  }

	sharedPreferences = self;
      }
    }

  return sharedPreferences;
}

+ (id) sharedPreferences
{
  if ( ! sharedPreferences )
    return [[self alloc] init];
  else
    return sharedPreferences;
}

-(void) dealloc
{
  if (window)
    [window release];
  [super dealloc];
}


-(void) awakeFromNib
{
  unsigned int popUptag;
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  _shortcuts = [Shortcuts sharedShortcuts];
  NSData *colorData=[defaults dataForKey:@"BGCOLOR"];
  NSColor *color;
  
  //Get Preferences defaults (popUp / tabView)
  if ( ( ! ( popUptag = [defaults integerForKey:PREFERENCEPOPUPTAG] ) ) || ( popUptag > 1 ) )
    popUptag = GENERAL;
  
  [popUpButton selectItemAtIndex:popUptag];
  [tabView selectTabViewItemAtIndex:popUptag];


  //Get General defaults
  if ( colorData ) 
    color = (NSColor *)[NSUnarchiver unarchiveObjectWithData:colorData];
  else
    color = [NSColor blackColor];

  [bgColorWell setColor: color]; 

  [startFullScreenSwitch setState:[defaults boolForKey:@"FULLSCREENFROMSERVICES"]];

  //Get Shortcuts Defaults
  [catpureButton setEnabled:NO];
  [deleteButton setEnabled:NO];
  
  //window frame
  [window setFrameAutosaveName:PREFERENCESFRAME];
}



//
//IBActions
//

-(IBAction) popUpAction:(id) sender;
{
  if ( sender != popUpButton )
    return;
  {
    int tag = [[sender selectedItem] tag];

    
    NSLog(@"popUpAction %@ %i popUpButton %@ ",sender, tag,popUpButton);
    if ( tag > 1 )
      {
	NSLog(@"popUpAction tag(%i) > 1",tag);
	return;
      }
    else
      [tabView selectTabViewItemAtIndex:tag];
  }
}

//General View  IBActions
-(IBAction) colorAction: (id) sender
{
  NSData *data=[NSArchiver archivedDataWithRootObject:[ bgColorWell color]];
  [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"BGCOLOR"];

  [[NSUserDefaults standardUserDefaults] synchronize];
}

-(IBAction) startFullScreenSwitchAction: (id) sender
{
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  
  [defaults setBool:[startFullScreenSwitch state]
	    forKey:@"FULLSCREENFROMSERVICES"];
  [defaults synchronize];

}



//Shortcurts View  IBAction

-(IBAction) keystrokeAction:(id) sender;
{
  if ( ! [[[inputView entryField] stringValue] isEqualToString:@"Capture"] ) 
    {
      NSString *entry = [[inputView entryField] stringValue];
      [_shortcuts setShortcut: entry
 		  forAction: [[browser selectedCellInColumn:0] stringValue]];

      [browser reloadColumn:1];
    }
    
  [_shortcuts synchronize];
  [inputPanel orderOut:self];
}


-(IBAction) deleteAction:(id) sender
{
  NSString *shortcutString;
  if ( ( shortcutString =  [[browser selectedCellInColumn:1] stringValue] ) )
    {
      [_shortcuts removeShortcut: shortcutString];
      [_shortcuts synchronize];
      [browser reloadColumn:1];
    }
}

-(IBAction) browserAction:(id) sender
{
  if ( sender != browser ) 
    return; 
  {
    int row = [sender selectedRowInColumn:0];
    if  ( row < 0 ) 
      {
	[catpureButton setEnabled: NO];
	[deleteButton setEnabled: NO];
      }
    else 
      {
	[catpureButton setEnabled: YES];
	[deleteButton setEnabled: YES];
      }
  }
}

//Shortcuts View NSBrowser Delegate method
- (NSString *)browser:(NSBrowser *)sender titleOfColumn:(int)column
{
  if ( sender == browser ) 
    {
      if ( column == 0 )
	return @"Actions";
      if ( column == 1 )
	return @"Short cuts";
    }
  
  return @"";
}

- (int)browser:(NSBrowser *)sender numberOfRowsInColumn:(int)column
{
  if ( sender == browser ) 
    {
      if  ( column == 0 ) 
	return [[_shortcuts actions] count];
      else if ( column == 1 )
	{
	  NSString *key = [[sender selectedCellInColumn:0] stringValue];
	  return [[_shortcuts shortcutsForAction: key] count];
	}
    }
  
  return 0;
}

- (void)browser:(NSBrowser *)sender willDisplayCell:(id)cell atRow:(int)row column:(int)column
{
  if ( sender == browser ) 
    {
      if  ( column == 0 ) 
	[cell setStringValue: [[_shortcuts actions] objectAtIndex:row]];
      
      else if ( column == 1 )
	{
	  NSString *key = [[sender selectedCellInColumn:0] stringValue];
	  [cell setStringValue:[[_shortcuts shortcutsForAction:key] objectAtIndex:row]];
	  [cell setLeaf: YES];
	}
      else 
	{
	  [cell setStringValue:@"b0rk"];
	  [cell setLeaf: YES];
	}
    }
}
 

//window delegate method
- (void)windowWillClose:(NSNotification *)aNotification
{
  id object = [aNotification object];
  if ( object == window ) 
    [[NSUserDefaults standardUserDefaults] setInteger:[[popUpButton selectedItem] tag]
					   forKey: PREFERENCEPOPUPTAG];
  else if ( object == inputPanel ) 
    NSLog(@" inputView entriy %@",[[inputView entryField] stringValue]);
    
}



- (void)windowDidBecomeKey: (NSNotification *)aNotification
{
  if ( [aNotification object] == inputPanel ) 
    {
      [inputView setEnabled: YES];
      [[inputView entryField] setStringValue:@"Capture"];
    }
}
- (void)windowDidResignKey: (NSNotification *)aNotification
{
  if ( [aNotification object] == inputPanel )
      [inputView setEnabled: NO];
}



//Accessor
-(NSPanel *) window
{
  return window;
}


@end
