//
//  AppController.m
//  Pense-Bête
//
//  Created by Bertrand on 11/12/18.
//  Copyright 2018 __MyCompanyName__. All rights reserved.
//

#import "AppController.h"


@implementation AppController

- (id) init {
	if(![super init]) {
		NSLog(@"AppController init failed");
		return nil;
	}
	taskList = [[NSMutableArray alloc] initWithCapacity: 0];
	return self;
}

-(int) numberOfRowsInTableView: (NSTableView *) tv{
		return [taskList count];
	}
	
- (id) tableView: (NSTableView *) tv
		objectValueForTableColumn: (NSTableColumn *) tableColumn
							  row: (int) row {
		NSString *v =[taskList objectAtIndex: row];
		
		return v;
}

- (void) tableView: (NSTableView *) tv
		setObjectValue: (id) anObject
		forTableColumn:(NSTableColumn *) aTableColumn
				   row: (int) rowIndex {
				   
	[taskList replaceObjectAtIndex: rowIndex
						withObject: anObject];

}


- (IBAction) createNewTask:(id) sender {
	NSLog(@"sender: %@", sender);
	NSString *string = [[NSString alloc] init];
	string = [textField stringValue];
	NSLog(@"%@", string);
	if (string != nil) {
		[taskList insertObject: string
					   atIndex: 0];
	}
	[toDoTableView reloadData];
	NSLog(@"%@", taskList);
	NSLog(@"dataSource : %@", [toDoTableView dataSource]);
	[string release];// With this, I've got an "exec_BAD_ACCESS" error when calling three times in a row createNewTask with the same string in textField
	
}

- (IBAction) removeTask:(id)sender{
	int i = [toDoTableView selectedRow];
	NSLog(@"ligne %i", i);
	if (i > -1) {
		[taskList removeObjectAtIndex: i];
		[toDoTableView reloadData];
	}
}
		
@end
