//
// $Id: EmInspectorManager.m,v 1.7 1997/10/31 04:51:44 nygard Exp $
//

//
//  This file is a part of Empire, a game of exploration and conquest.
//  Copyright (C) 1996  Steve Nygard
//
//  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 the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//  You may contact the author by:
//     e-mail:  nygard@telusplanet.net
//

#import "Empire.h"

RCSID ("$Id: EmInspectorManager.m,v 1.7 1997/10/31 04:51:44 nygard Exp $");

#import "EmInspectorManager.h"

#import "EmInspector.h"
#import "ObjectExtensions.h"
#import "City.h"

static EmInspectorManager *_instance = nil;

//======================================================================
// The Inspector Manager provides an interface for inspecting game
// objects and keeping the inspectors up to date.
//======================================================================

@implementation EmInspectorManager

+ instance
{
    if (_instance == nil)
    {
        _instance = [[EmInspectorManager alloc] init];
    }
  
    return _instance;
}

//----------------------------------------------------------------------

- init
{
    NSString *nibFile;
    BOOL loaded;

    [super init];

    subject = nil;
    currentInspector = nil;

    nibFile = @"InspectorPanel.nib";
    loaded = [NSBundle loadNibNamed:nibFile owner:self];
    if (loaded == NO)
    {
        NSLog (@"Could not load %@.", nibFile);
        [super dealloc];
        return nil;
    }

    return self;
}

//----------------------------------------------------------------------

- (void) dealloc
{
    SNRelease (revertButton);
    SNRelease (okButton);

    [super dealloc];
}

//----------------------------------------------------------------------

- (void) awakeFromNib
{
    [revertButton retain];
    [okButton retain];

    subject = nil;
    player = p_neutral;

    [revertButton removeFromSuperview];
    [okButton removeFromSuperview];

    currentInspectorView = defaultView;
}

//----------------------------------------------------------------------

- (void) showPanel
{
    [inspectorPanel orderFront:self];
}

//----------------------------------------------------------------------

- (void) modeDidChange:sender
{
    [inspectorPanel orderFront:self];
    
    [self player:player selectThing:subject]; 
}

//----------------------------------------------------------------------

- (void) player:(Player)p selectThing:anObject
{
    int mode;
    NSString *inspectorClassName = nil;
    EmInspector *inspector;
    NSView *inspectorView;
    NSString *titleString;
    Class inspectorClass;

    if (anObject != subject && currentInspector != nil)
    {
        [currentInspector willStopInspecting];
    }

    subject = anObject;
    player = p;

    if (subject != nil)
        titleString = [NSString stringWithFormat:@"%@ Inspector", NSStringFromClass ([subject class])];
    else
        titleString = @"No Selection";

    [inspectorPanel setTitle:titleString];

    mode = [modePopup indexOfSelectedItem];

    //NSLog (@"mode: %d, subject: %@, anObject: %@", mode, subject, anObject);

    NSAssert (mode >= 0 && mode <= 1, @"Invalid inspector mode.");

    if (anObject == nil)
    {
        switch (mode)
        {
          case 0:
              inspectorClassName = @"NoAttributesInspector";
              break;

          case 1:
              inspectorClassName = @"NoUnitsInspector";
              break;
        }
    }
    else
    {
        switch (mode)
        {
          case 0:
              inspectorClassName = [anObject attributesInspectorClassName];
              break;

          case 1:
              inspectorClassName = [anObject unitsInspectorClassName];
              break;
        }
    }

    inspectorClass = NSClassFromString (inspectorClassName);
    inspector = [inspectorClass instance];
    [inspector setSubject:subject];
    inspectorView = [inspector inspectorView];

    NSAssert (inspectorView != nil, @"Inspector view was nil!");

    //NSLog (@"Replacing %@ with %@", currentInspectorView, inspectorView);
    [[inspectorPanel contentView] replaceSubview:currentInspectorView with:inspectorView];
    currentInspector = inspector;
    currentInspectorView = inspectorView;

    if ([inspector wantsButtons])
    {
        [[inspectorPanel contentView] addSubview:revertButton];
        [[inspectorPanel contentView] addSubview:okButton];
    }
    else
    {
        [revertButton removeFromSuperview];
        [okButton removeFromSuperview];
    }

    [inspector revert];

    //[inspectorPanel display];
    //[inspectorPanel setNeedsDisplay:YES];
}

//----------------------------------------------------------------------

- (Player) player
{
    return player;
}

//----------------------------------------------------------------------

- (void) setOkButtonEnabled:(BOOL)flag
{
    [okButton setEnabled:flag];
}

//----------------------------------------------------------------------

- (void) setRevertButtonEnabled:(BOOL)flag
{
    [revertButton setEnabled:flag];
}

//----------------------------------------------------------------------

- (void) revert:sender
{
    [inspectorPanel setDocumentEdited:NO];
    [currentInspector revert];
}

//----------------------------------------------------------------------

- (void) ok:sender
{
    [inspectorPanel setDocumentEdited:NO];
    [currentInspector ok];
}

@end
