//
//  themSearchDisplayDelegate.m
//  melody app
//
//  Created by Odin on 15/12/2016.
//  Copyright © 2016 demain un autre jour. All rights reserved.
//


/*
 
 +---------------------------------------------------------------+
 |  This class is for the delegate of theme cat collectionView   |
 +---------------------------------------------------------------+
 
 
 */


#import "themSearchDisplayDelegate.h"

@interface themSearchDisplayDelegate () {
	long				matchCont;
	UITableView			*ref;
	UICollectionView	*myTagView;
}
@end

@implementation themSearchDisplayDelegate

+ (void) initialize {
	
}

- (id) init {
	self = [super init];
	return self;
}

- (void) reload:( NSMutableArray *)myNewList myFilteredList:(NSMutableArray *)myFilteredList {
	contentList = myNewList;
	filteredContentList = myFilteredList;
	[ref reloadData];
}

- (id) initWithArticleId:(int)artId mainView:(UIView *)mainView tagView:(UICollectionView *)tagView
{
	self = [super init];
	self.articleId = artId;
	myTagView = tagView;
	self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"baseData.sql"];
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	// Return the number of sections.
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	//return 2;
	// Return the number of rows in the section.
	if (isSearching) {
		//NSLog(@"count = %ld", (unsigned long)[filteredContentList count]);
		if ( [filteredContentList count] > 1000)
		{
			return 0;
		}
		return [filteredContentList count];
	}
	else {
		if ( [filteredContentList count] > 1000)
		{
			return 0;
		}
		return [contentList count];
	}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSString *query;
	NSArray *matches = [contentList valueForKey:@"nodeAttributeArray"];
	NSArray *matches2 = [matches valueForKey:@"nodeContent"];
	if ([self catGeoNotAlreadyLinkedToArticle:[matches2[indexPath.row][0]intValue]]) {
		query = [NSString stringWithFormat:@"insert into catThem values(%d, %d, \"%@\")", self.articleId, [matches2[indexPath.row][0]intValue], matches2[indexPath.row][1]];
		//NSLog(@"query is %@", query);
		[self.dbManager executeQuery:query];
		if (self.dbManager.affectedRows != 0) {
			//NSLog(@"Query was executed successfully. Affected rows = %d", self.dbManager.affectedRows);
			//Inform the delegate that the editing was finished.
			UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@", matches2[indexPath.row][1]]
																  message:@"Catégorie géographique ajoutée"
																 delegate:nil
														cancelButtonTitle:@"OK"
														otherButtonTitles:nil];
			[myAlertView show];
			self.dbManager.lastInsertedRowID --;
		}
		[[NSNotificationCenter defaultCenter] postNotificationName:@"catThemAdded" object:self];
	} else {
		UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Erreur"
															  message:@"L'article fait déjà partie de cette catégorie"
															 delegate:nil
													cancelButtonTitle:@"OK"
													otherButtonTitles:nil];
		[myAlertView show];
	}
}


- (BOOL) catGeoNotAlreadyLinkedToArticle:(int) idGeo {
	NSString *queryCat = [NSString stringWithFormat:@"select * from catThem where fileInfoID = '%d' AND themID = '%d'", self.articleId, idGeo];
	NSArray *dataCat = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:queryCat]];
	if (dataCat.count > 0) {
		return false;
	} else {
		return true;
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	ref = tableView;
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
	}
	//NSLog(@"indexPath.row = %ld", indexPath.row);
	// Configure the cell...
	NSArray *matches = [contentList valueForKey:@"nodeAttributeArray"];
	NSArray *matches2 = [matches valueForKey:@"nodeContent"];
	matchCont = [matches2 count];
	//NSLog(@"count of matches = %ld", matchCont);
	if ([matches2 count] > indexPath.row) {
		cell.textLabel.text = matches2[indexPath.row][1];
	} else {
		cell.textLabel.text = @"";
	}
	return cell;
}

@end
