
#import "PrefViewController.h"


@implementation PrefViewController

- (id) init {
    self = [ super initWithStyle: UITableViewStyleGrouped ];
	
    if (self != nil) {
        self.title = @"Preferences";
    }
    return self;
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
/*
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
	LogMethod();
	switch (interfaceOrientation) {
		 case UIInterfaceOrientationPortrait: 
		 {
		 }
		 break;
		 
		 case UIInterfaceOrientationLandscapeRight: 
		 {
		 }
		 break;
		 
		 case UIInterfaceOrientationLandscapeRight: 
		 {
		 }
		 break;
	}
}
 
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	LogMethod();
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	activeTextField = nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    /* Number of logical groups, including labels ?!?*/
    return PREF_NUM_GROUPS;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case(0):
            return 1;
            break;
        case(1):
            return 2;
            break;
    }
	
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case(0):
            return @"Presentation";
            break;
        case(1):
            return @"Search";
            break;
    }
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [ NSString stringWithFormat: @"%d:%d", [ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition:1 ]];
	
    UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier];
	
    if (cell == nil) {
        cell = [ [ [ UITableViewCell alloc ] initWithFrame: CGRectZero reuseIdentifier: CellIdentifier] autorelease ];
		
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
		
        switch ([ indexPath indexAtPosition: 0]) {
            case(0):
                switch([ indexPath indexAtPosition: 1]) {
                    case(0):
		      {
			UISegmentedControl *langControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects: @"English", @"French", @"German", nil]];
			langControl.contentMode = UIViewContentModeScaleToFill;
			langControl.frame =  CGRectMake(145, 5, 150, 35);
			langControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
			langControl.selectedSegmentIndex = 0;
			langControl.tag = 3;
			[langControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
			langControl.segmentedControlStyle = UISegmentedControlStyleBar;
			
			[cell addSubview:langControl];
			cell.textLabel.text = @"Language";
			[langControl release];
		      }
		      break;
                }
                break;
            case(1):
                switch ([ indexPath indexAtPosition: 1 ]) {
                    case(0):
		      {
			UISwitch *stemmingControl = [ [ UISwitch alloc ] initWithFrame: CGRectMake(200, 10, 60, 30) ];
			stemmingControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
			stemmingControl.on = YES;
			stemmingControl.tag = 1;
			[stemmingControl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
			[ cell addSubview: stemmingControl ];
			cell.textLabel.text = @"Stemming";
			[stemmingControl release];
		      }
		      break;
		    case(1):
		      {
			UISwitch *casematchControl = [ [ UISwitch alloc ] initWithFrame: CGRectMake(200, 10, 50, 30) ];
			casematchControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
			casematchControl.on = NO;
			casematchControl.tag = 2;
			[casematchControl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
			[ cell addSubview: casematchControl ];
			cell.textLabel.text = @"Case matching";
			[casematchControl release];
		      }
		      break;
		}
                break;
        }
    }
	
    return cell;
}

#pragma mark ControlEventTarget Actions


- (void)segmentAction:(UISegmentedControl*)sender
{
	if ([activeTextField canResignFirstResponder])
		[activeTextField resignFirstResponder];
	
	DEBUGLOG(NSLog(@"segmentAction: sender = %d, segment = %d",  [sender tag], [sender selectedSegmentIndex]));
}

- (void)switchAction:(UISwitch*)sender
{
	if ([activeTextField canResignFirstResponder])
		[activeTextField resignFirstResponder];
	DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d",  [sender tag], [sender isOn]));
}


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

@end
