//
//  ChatController.m
//  Tuve
//
//  Created by Philippe Hausler on 9/8/08.
//  Copyright 2008 Philippe Hausler. All rights reserved.
//

#import "ChatController.h"


@implementation ChatController
@synthesize tableView;

- (id)initWithStyle:(UITableViewStyle)style {
	if (self = [super initWithStyle:style]) {
	}
	return self;
}


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


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSInteger index = [indexPath indexAtPosition:1];
	static NSString *cellTypeId = @"chatCell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellTypeId];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellTypeId] autorelease];
		cell.font = [UIFont systemFontOfSize:10];
		cell.lineBreakMode = UILineBreakModeWordWrap;
	}
	// Configure the cell
	cell.text = [messages objectAtIndex:index];
	return cell;
}

/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
*/
/*
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if (editingStyle == UITableViewCellEditingStyleDelete) {
	}
	if (editingStyle == UITableViewCellEditingStyleInsert) {
	}
}
*/
/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/
/*
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/
- (void)msgRecieved:(NSNotification *)aNotification
{
	NSDictionary *userInfo = [aNotification userInfo];
	[self addMessage:[userInfo objectForKey:@"message"] fromUser:[userInfo objectForKey:@"sender"]];
}
- (void)addMessage:(NSString *)message fromUser:(NSString *)user
{
	[messages addObject:[NSString stringWithFormat:@"%@: %@", user,message]];
	[self.tableView reloadData];
	[self.tableView scrollToRowAtIndexPath:[[NSIndexPath indexPathWithIndex:0] indexPathByAddingIndex:[messages count]-1] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

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


- (void)viewDidLoad {
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(msgRecieved:) name:@"TUMSG" object:NULL];
	messages = [[NSMutableArray alloc] init];
	[super viewDidLoad];
}


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

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

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

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

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
}


@end

