//
//  DVDCaseController.m
//  DVDCase
//
//  Created by Bill Dudney on 1/3/09.
//  Copyright 2009 Gala Factory Software LLC. All rights reserved.
//
//
//  Licensed with the Apache 2.0 License
//  http://apache.org/licenses/LICENSE-2.0
//

#import "DVDCabinetController.h"


@implementation DVDCabinetController

@synthesize key;

//START:code.DVDCabinetController.viewDidLoad
- (void)viewDidLoad {
  [super viewDidLoad];
  NSArray *keys = [NSArray arrayWithObjects:@"home", @"work", nil];
  NSArray *homeDVDs = [NSArray arrayWithObjects:@"Thomas the Builder", nil];
  NSArray *workDVDs = [NSArray arrayWithObjects:@"Intro to Blender", nil];
  NSArray *values = [NSArray arrayWithObjects:homeDVDs, workDVDs, nil];
  data = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
}
//END:code.DVDCabinetController.viewDidLoad

//START:code.DVDCabinetController.viewWillAppear
- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [self.tableView reloadData];
}
//END:code.DVDCabinetController.viewWillAppear

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//START:code.DVDCabinetController.numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    return [[data valueForKey:self.key] count];
}
//END:code.DVDCabinetController.numberOfRowsInSection

//START:code.DVDCabinetController.cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  static NSString *CellIdentifier = @"Cell";
  
  UITableViewCell *cell = [tableView
                           dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier]
            autorelease];
  }
  
  cell.textLabel.text = [[data valueForKey:self.key] objectAtIndex:indexPath.row];
  
  return cell;
}
//END:code.DVDCabinetController.cellForRowAtIndexPath


//START:code.DVDCabinetController.didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [self.navigationController popViewControllerAnimated:YES];
}
//END:code.DVDCabinetController.didSelectRowAtIndexPath

- (void)dealloc {
  [data release];
  data = nil;
  self.key = nil;
  [super dealloc];
}


@end

