/* DefaultView.m

   Copyright (C) 2003 

   Author:  Yen-Ju Chen <yjchenx@hotmail.com>

   Rewritten from PlistEditor by Fabien VALLON <fabien@tuxfamily.org>
   (http://gnustep-apps.org/fabien/PlistEditor/)

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

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.
   If not, write to the Free Software Foundation,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "DefaultView.h"
#include "Node.h"
#include <AppKit/AppKit.h>

@implementation DefaultView

/*
 OutlineView DataSource
 */
- (int) outlineView: (NSOutlineView *) outlineView 
        numberOfChildrenOfItem: (id) item
{
  if (item == nil) 
    {
      return [_rootArray count];
    }
  else 
    return [[item children] count];
}


- (BOOL) outlineView: (NSOutlineView *) outlineView 
         isItemExpandable: (id) item
{
  if (item == nil)  /* Root */
    {
      return YES;
    }
  else 
    {
      if ( [[item children] count] > 0 )
        return YES;
      else
        return NO;
    }
}


- (id) outlineView: (NSOutlineView *) outlineView 
       child: (int) index ofItem: (id) item
{
  if (item == nil) /* Root */
    {
      return [_rootArray objectAtIndex:index];
    }
  else 
    {
      return [[item children] objectAtIndex:index];
    }
}

- (id) outlineView: (NSOutlineView *) outlineView 
       objectValueForTableColumn: (NSTableColumn *) tableColumn 
       byItem: (id) item
{
  if ( [[tableColumn identifier] isEqualToString:@"KEYSTAG"] )
    {
      return [item key];
    } 
  else //  Must be "VALUESTAG"
    {
      return [item value];
    }
}

- (BOOL) outlineView:(NSOutlineView *)outlineView 
         shouldEditTableColumn:(NSTableColumn *)tableColumn 
         item:(id) item
{
  if ( [[tableColumn identifier] isEqualToString:@"VALUESTAG"]  )
    {
//      NSLog(@"here");
//      [tableColumn setEditable:YES];
//      return YES;
    }
  return NO;
}

-(void) buildTree
{
  int i;
  Node *node;
  NSArray *keysArray = [_dict allKeys];

  for (i = 0; i < [keysArray count]; i++)
    {
      node = [[Node alloc] init];
      [node setKey:[keysArray objectAtIndex:i]];
      [_rootArray addObject: [self buildNode: node 
                                   withObject: [_dict objectForKey: [keysArray objectAtIndex:i]]
                                   withParent:nil]];
      RELEASE(node);
    }
}

-(Node *) buildNode: (Node *) node 
          withObject: (id) object 
          withParent: (Node *) parent 
{
  int i;
  Node *newNode;

  [node setParent:parent];

  if (([object isKindOfClass:[NSString class]]) || 
       ([object isKindOfClass:[NSNumber class]]))
    {      
      [node setValue:object];
    }
  else if ([object isKindOfClass:[NSDictionary class]])
    {
      NSDictionary *dict = (NSDictionary *)object;
      NSArray *keysArray = [dict allKeys];
      for (i = 0; i < [keysArray count]; i++)
        {         
          newNode = [[Node alloc] init];
          [newNode setKey: [keysArray objectAtIndex: i]];
          [node addChildren: [self buildNode: newNode 
                                   withObject: [dict objectForKey: [keysArray objectAtIndex:i]] 
                                   withParent: node]];
          RELEASE(newNode);
        }
    }
  else if ([object isKindOfClass:[NSArray class]])
    {
      NSArray *array = (NSArray *) object;
      [node setValue: @""];
      for (i = 0; i < [array count]; i++)
        {         
          newNode = [[Node alloc] init];
          [newNode setKey: [NSString stringWithFormat: @"%i",i]];
          [node addChildren: [self buildNode: newNode 
                                   withObject: [array objectAtIndex:i] 
                                   withParent: node]];
          RELEASE(newNode);
        }
    }
  else
    NSLog(@"====> Clas %@" ,NSStringFromClass([object class]));

  return node;
}

- (id) initWithFrame: (NSRect) frame
{
  NSScrollView *scrollView;
  NSTableColumn *keyColumn, *valueColumn;

  self = [super initWithFrame: frame];
  _rootArray = [NSMutableArray new];

  location = [@"~/GNUstep/Defaults/.GNUstepDefaults" stringByStandardizingPath];

  _dict = [[[NSString alloc] initWithContentsOfFile: location] propertyList];
  [self buildTree];

  keyColumn = [[NSTableColumn alloc] initWithIdentifier: @"KEYSTAG"];
  [[keyColumn headerCell] setStringValue: _(@"Key")];

  valueColumn = [[NSTableColumn alloc] initWithIdentifier: @"VALUESTAG"];
  [[valueColumn headerCell] setStringValue: _(@"Value")];

  outlineView = [[NSOutlineView alloc] initWithFrame: [self bounds]];
  [outlineView setDrawsGrid: NO];
  [outlineView addTableColumn: keyColumn];
  [outlineView addTableColumn: valueColumn];
  [outlineView setAutoresizesAllColumnsToFit: YES];
  [outlineView setAllowsMultipleSelection: NO];
  [outlineView setAllowsEmptySelection: YES];
  [outlineView setTarget: self];
  [outlineView setDataSource: self];
  [outlineView setDelegate: self];
  [outlineView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
  [outlineView setOutlineTableColumn: keyColumn];
  [outlineView setIndentationPerLevel: 10];

  RELEASE(keyColumn);
  RELEASE(valueColumn);

  scrollView = [[NSScrollView alloc] initWithFrame: [self bounds]];
  [scrollView setHasHorizontalScroller: YES];
  [scrollView setHasVerticalScroller: YES];
  [scrollView setBorderType: NSBezelBorder];
  [scrollView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
  [scrollView setDocumentView: outlineView];
  RELEASE(outlineView);

  [self addSubview: scrollView];
  RELEASE(scrollView);

  return self;
}

- (void) dealloc
{
  RELEASE(_rootArray);
  [super dealloc];
}

@end
