#import <Foundation/Foundation.h>

#import <UIKit/CDStructures.h>
#import <UIKit/UINavigationBar.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UITable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UITableColumn.h>

#import "GreenstoneApplication.h"
#import "GSResultItem.h"

#import "MenuCascadeView.h"


// ***** think about coding for orientation of iPod!!!!

@implementation MenuCascadeView

-(id) init 
{
  CGRect rect = [GreenstoneApplication workAreaRect];  
  return [self initWithFrame:rect];	
}

-(id) initWithFrame:(CGRect)rect 
{
  return [self initWithFrame:rect withParent:nil withCollection:nil withTitle:nil withGID:nil];
}

-(id) initWithFrame:(CGRect)rect withTitle:(NSString*)title
{
  return [self initWithFrame:rect withParent:nil withCollection:nil withTitle:title withGID:nil];
}

-(id) initWithFrame:(CGRect)rect withParent:(UIView*)parent 
        withCollection:(NSString*)collection withTitle:(NSString*)title withGID:(NSString*)gid
{
  self = [super initWithFrame:rect];
  
  _parent = parent;
  _collection = collection;
  _gid = gid;
  _childMenu = nil;
 
  _tabrow    = [[NSMutableArray alloc] initWithCapacity:MCInitCapacity];
  _oidItem   = [[NSMutableArray alloc] initWithCapacity:MCInitCapacity];

  int frame_x_dim = CGRectGetMaxX(rect)+1;
  int frame_y_dim = CGRectGetMaxY(rect)+1;
    
  int tab_x_org = 0.0f;
  int tab_y_org = TOP_NAVBAR_HEIGHT;
  int tab_x_dim = frame_x_dim;
  int tab_y_dim = frame_y_dim -TOP_NAVBAR_HEIGHT;

  CGRect tablerect = CGRectMake(tab_x_org,tab_y_org,tab_x_dim,tab_y_dim);

  _table = [[UITable alloc] initWithFrame:tablerect];
    
  UITableColumn *col = [[UITableColumn alloc] initWithTitle: @"MenuCascadeApp"
		       identifier: @"menucascade" width: frame_x_dim];
      
  [_table addTableColumn: col]; 
  [_table setDataSource: self];
  [_table setDelegate: self];
  [_table reloadData];
  
  int nav_x_org = 0.0f;
  int nav_y_org = 0.0f;
  int nav_x_dim = frame_x_dim;
  int nav_y_dim = TOP_NAVBAR_HEIGHT;

  CGRect navrect = CGRectMake(nav_x_org,nav_y_org,nav_x_dim,nav_y_dim);
  _navbar = [[UINavigationBar alloc] initWithFrame: navrect];
  
  NSString* back = (parent) ? @"Back" : nil;
  [_navbar showButtonsWithLeftTitle:back rightTitle:nil leftBack:YES];
  [_navbar enableAnimation];
  [_navbar setBarStyle: UIBarStyleDefault];
  [_navbar setDelegate: self];

  if (title != nil) {
    [self setTitle:title];
  }

  [self addSubview: _navbar]; 
  [self addSubview: _table]; 	

  return self;
}


-(void) setTitle: (NSString*)title
{
  UINavigationItem* navtitle = [UINavigationItem alloc];
  [_navbar pushNavigationItem: [navtitle initWithTitle: title]];
}


-(void) appendMenuItem: (NSObject*) app withItem:(NSString*)itemLabel withGID:(NSString*)childGID
{
  UIImageAndTextTableCell* itcell = [[UIImageAndTextTableCell alloc] init];
  [itcell setTitle: itemLabel]; 
    
  if ([childGID hasPrefix:@"CL"]==YES) {
    [itcell setShowDisclosure: YES];
    [itcell setDisclosureStyle: 2];
  }
  else if (_parent == nil) {
    // Root menu => Make disclosures look a bit different
    [itcell setShowDisclosure: YES];
    [itcell setDisclosureStyle: 1];
  }

  [_tabrow addObject:itcell]; 
  [itcell retain];

  if (itemLabel == nil) {
    itemLabel = [[NSString alloc] initWithString:@"Unknown Title"];
  }

  GSResultItem* oidItem = [[GSResultItem alloc] initWithOID:childGID withTitle:itemLabel]; // *****
  [_oidItem addObject:oidItem];  

  // needed to force table view to update
  [_table reloadData];

}

// ************
// * Delegates
// ************

- (int) numberOfRowsInTable: (UITable *)table
{
  return [_tabrow count];
}

- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col
{


  return [_tabrow objectAtIndex:row];
}

- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col
				reusing: (BOOL) reusing
{
  return [_tabrow objectAtIndex:row];
}



-(void) navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button 
{
  switch (button) 
  {
    case 1:  // left
      // cerr << "***** Need to set collection to nil if going to top page" << endl; // ****
      // [GreenstoneApplication setInCollection:_parent._collectcion];
      [GreenstoneApplication transitionView:UITransitionBothShiftRight 
         fromView:self toView:_parent];

      if (_childMenu != nil) {
	[_childMenu release];
	_childMenu = nil;
      }
      break;

    default:
      NSLog(@"Unrecognised navbar button = %d",button);
      break;
  }
}


-(void) dealloc
{
  [_childMenu release];

  [_gid release]; // **** is this ever used???
  [_collection release];

  [_tabrow release];
  [_oidItem release];

  [ super dealloc ];
}

@end
