//
//  popupViewViewController.m
//  melody app
//
//  Created by Odin on 25/10/2016.
//  Copyright © 2016 demain un autre jour. All rights reserved.
//

#import "DBmanager.h"
#import "viewInfo.h"
#import "editingView.h"
#import "popupViewViewController.h"

@interface popupViewViewController () {
		NSArray *dataCat;
}

@end

@implementation popupViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	self.view.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:.6];
	self.popUpView.layer.cornerRadius = 5;
	self.popUpView.layer.shadowOpacity = 0.8;
	self.popUpView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
	// Do any additional setup after loading the view, typically from a nib.
	self.tablePeople.delegate = self;
	self.tablePeople.dataSource = self;
	// Initialize the dbManager property.
	self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"baseData.sql"];
	[self loadData];
}

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


-(void)loadData{
	// Form the query.
	NSLog(@"recordid == %d", self.recordIDToEdit);
	NSString *query = [NSString stringWithFormat:@"select * from catGeo where fileInfoID = %d", self.recordIDToEdit];
	self.arrPeopleInfo = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
	
	// Reload the table view.
	[self.tablePeople reloadData];
}

- (IBAction)addNewRecord:(id)sender {
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	// Dequeue the cell.
	static NSString *CellIdentifier = @"Cell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
	}
	
	NSInteger indexOfTitle = [self.dbManager.arrColumnNames indexOfObject:@"nomLC"];
	
	// Set the loaded data to the appropriate cell labels.
	cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfTitle], [NSString stringWithFormat:@""]];
	return cell;
}

//*****************************************//
//********* tables rows counting **********//
//*****************************************//

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

//*****************************************//
//********* numbers of subrows ************//
//*****************************************//

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return self.arrPeopleInfo.count;
}

//*****************************************//
//************** size of celss ************//
//*****************************************//

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 60.0;
}

//*****************************************//
//****** accesory button parameters *******//
//*****************************************//

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
	// Get the record ID of the selected name and set it to the recordIDToEdit property.
	self.recordIDToEdit = [[[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:0] intValue];
	
	// Perform the segue.
	[self performSegueWithIdentifier:@"idSegueEditInfo" sender:self];
}

//*****************************************//
//************* editing infos *************//
//*****************************************//

-(void)editingInfoWasFinished{
	// Reload the data.
	[self loadData];
}

//*****************************************//
//******** mooving to edit panel **********//
//*****************************************//

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
	editingView *editInfoViewController = [segue destinationViewController];
	editInfoViewController.delegate = self;
	editInfoViewController.recordIDToEdit = self.recordIDToEdit;
}


//*****************************************//
//********** new article edition **********//
//*****************************************//

- (IBAction)writeArticle:(id)sender {
	// Before performing the segue, set the -1 value to the recordIDToEdit. That way we'll indicate that we want to add a new record and not to edit an existing one.
	self.recordIDToEdit = -1;
	
	// Perform the segue.
	[self performSegueWithIdentifier:@"idSegueEditInfo" sender:self];
}

//*****************************************//
//************ delete an article **********//
//*****************************************//

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
	
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		// Delete the selected record.
		// Find the record ID.
		int recordIDToDelete = [[[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:0] intValue];
		// Prepare the query.
		NSString *query = [NSString stringWithFormat:@"select * from catGeo where fileInfoID = %d", self.recordIDToEdit];
		dataCat = nil;
		dataCat = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
		NSString *query2 = [NSString stringWithFormat:@"delete from catGeo where geoID = %@", dataCat[indexPath.row][1]];
		// Execute the query.
		[self.dbManager executeQuery:query2];
		// Reload the table view.
		[self loadData]; 
	}
}


- (void)showAnimate
{
	self.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
	self.view.alpha = 0;
	[UIView animateWithDuration:.25 animations:^{
		self.view.alpha = 1;
		self.view.transform = CGAffineTransformMakeScale(1, 1);
	}];
}

- (void)removeAnimate
{
	[UIView animateWithDuration:.25 animations:^{
		self.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
		self.view.alpha = 0.0;
	} completion:^(BOOL finished) {
		if (finished) {
			[self.view removeFromSuperview];
		}
	}];
}

- (void)showInView:(UIView *)aView animated:(BOOL)animated valueToEdit:(int) valueEdit
{
	self.recordIDToEdit = valueEdit;
	[aView addSubview:self.view];
	if (animated) {
		[self showAnimate];
	}
}

- (IBAction)return:(id)sender {
	[self removeAnimate];
}
@end
