//
//  RootViewController.m
//  BonjourWebBrowser
//
//  Created by Chris Adamson on 7/21/08.
//  Copyright Subsequently and Furthermore, Inc. 2008. All rights reserved.
//
//
//  Licensed with the Apache 2.0 License
//  http://apache.org/licenses/LICENSE-2.0
//


#import "RootViewController.h"
#import "BonjourWebBrowserAppDelegate.h"
#import "WebPageViewController.h"


@implementation RootViewController


//START:code.BonjourWebBrowser.startSearchingForWebServers
-(void) startSearchingForWebServers {
	bonjourBrowser = [[NSNetServiceBrowser alloc] init];
	[bonjourBrowser setDelegate: self];
	[bonjourBrowser searchForServicesOfType:@"_http._tcp" inDomain:@""]; //<label id="code.BonjourWebBrowser.startSearchingForWebServers.searchforservicesoftype"/>
}
//END:code.BonjourWebBrowser.startSearchingForWebServers

// bonjour delegate methods
- (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)netServiceBrowser {
	[activityIndicator startAnimating];
}

- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)netServiceBrowser {
	[activityIndicator stopAnimating];
}

- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didNotSearch:(NSDictionary *)errorInfo {
	[activityIndicator stopAnimating];
}


- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didFindService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing {
	[discoveredWebServices addObject: netService];
	[tableView reloadData];
	if (! moreServicesComing)
		[activityIndicator stopAnimating];
}

- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didRemoveService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing {
	[discoveredWebServices removeObject: netService];
	[tableView reloadData];
}


// resolution delegate methods
- (void)netServiceDidResolveAddress:(NSNetService *)sender {
	
}


- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict {
	
}




- (void)viewDidLoad {
	// Add the following line if you want the list to be editable
	// self.navigationItem.leftBarButtonItem = self.editButtonItem;
	self.title = @"Local Web Servers";
	discoveredWebServices = [[NSMutableArray alloc] initWithCapacity: 10]; // arbitrary initial capacity
	[self startSearchingForWebServers];
}


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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [discoveredWebServices count];
}


- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"DiscoveredServerId";
	
	UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	
	// Set up the cell
	NSNetService *aService = (NSNetService*) [discoveredWebServices objectAtIndex: [indexPath row]];
	if (aService != NULL)
		cell.text = [aService name];
	return cell;
}


 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	 // navigate to a web view for that service
	 NSNetService *aService = (NSNetService	*) [discoveredWebServices objectAtIndex: [indexPath row]];
	 if (aService != NULL) {
		 WebPageViewController *webPageViewController =
			[[WebPageViewController alloc]
			 initWithNibName:@"WebPageView" bundle:nil netService:aService];
		 [self.navigationController pushViewController:webPageViewController animated:YES];
		 [webPageViewController release];
	 }
 }


/*
 Override if you support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
		
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		// Delete the row from the data source
		[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
	}	
	if (editingStyle == UITableViewCellEditingStyleInsert) {
		// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
	}	
}
*/


/*
 Override if you support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	// Return NO if you do not want the specified item to be editable.
	return YES;
}
*/


/*
 Override if you support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
 Override if you support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
	// Return NO if you do not want the item to be re-orderable.
	return YES;
}
 */ 

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


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


@end

