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

#import "canalController.h"

@interface canalController () <UITableViewDelegate, UITableViewDataSource>{
	NSArray *dataSource;
	NSInteger pastRowsNb;
}
@property (weak, nonatomic) IBOutlet UISwitch *emergencySwitch;

@end

@implementation canalController

- (void)viewDidLoad {
	[super viewDidLoad];
	pastRowsNb = 0;
	self.tableView.delegate = self;
	self.tableView.dataSource= self;
	CGRect screen = [[UIScreen mainScreen] bounds];
	self.view.frame = screen;
	self.view.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:.6];
	NSString *xPathQuery = [NSString stringWithFormat:@"//canals"];
	NSError *myError;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"sessionInfos.xml"]];
	NSData *data = [NSData dataWithContentsOfFile:fullPath options:NSDataReadingMappedIfSafe error:&myError];
	contentList = PerformXMLXPathQuery(data, xPathQuery);
	NSArray *matches = [contentList valueForKey:@"nodeChildArray"];
	NSArray *matches2 = [matches valueForKey:@"nodeAttributeArray"];
	NSArray *matches3 = [matches2 valueForKey:@"nodeContent"];
	//NSLog(@"content list = %@", matches3);
	dataSource = matches3[0];
}

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

- (IBAction)emergencyChanged:(id)sender {
	NSDictionary* userInfo;
	if (self.emergencySwitch.isOn) {
		userInfo = @{@"isEmergency": @"true"};
	} else {
		userInfo = @{@"isEmergency": @"false"};
	}
	[[NSNotificationCenter defaultCenter] postNotificationName:@"setArticleEmergency" object:self userInfo:userInfo];
	
}
- (IBAction)urgenceWeekEndButtonTapped:(id)sender {
	if (self.emergencySwitch.isOn) {
		[self.emergencySwitch setOn:false animated:true];
		[self emergencyChanged:sender];
	} else {
		[self.emergencySwitch setOn:true animated:true];
		[self emergencyChanged:sender];
	}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return dataSource.count;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSInteger rowIndex = indexPath.row + pastRowsNb;
	canalViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"canalViewCell"];
	if (!cell)
	{
		[tableView registerNib:[UINib nibWithNibName:@"canalViewCell" bundle:nil] forCellReuseIdentifier:@"canalViewCell"];
		cell = [tableView dequeueReusableCellWithIdentifier:@"canalViewCell"];
	}
	cell.canalName.text = dataSource[rowIndex][1];
	NSURL *url = [NSURL URLWithString:dataSource[rowIndex][4]];
	NSData *data = [[NSData alloc] initWithContentsOfURL:url];
	UIImage *tmpImage = [[UIImage alloc] initWithData:data];
	cell.canalImage.image = tmpImage;
	pastRowsNb++;
	return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 18;
}

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

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	self.view.hidden = true;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"canalChoosen" object:self];
	
	NSInteger rowIndex = indexPath.row + indexPath.section;
	NSString *test = dataSource[rowIndex][0];
	int value = [test intValue];
	//NSLog(@"%@", dataSource[indexPath.row][0]);
	//NSLog(@"%@", test);
	//NSLog(@"%d", value);
	NSUserDefaults *pref2;
	pref2 = [NSUserDefaults standardUserDefaults];
	[pref2 setInteger:value forKey:@"canalId"];
	[pref2 synchronize];
	[self removeAnimate];
	[self.view removeFromSuperview];
	
	[[NSNotificationCenter defaultCenter] postNotificationName:@"pushArticle" object:self];
}

- (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];
		}
	}];
}

@end
