//
//  SelectPeriodViewController.m
//  DrivoMeter
//
//  Created by Triin Uudam on 4/16/12.
//  Copyright (c) 2012 Mobi Solutions OÜ. All rights reserved.
//

#import "SelectPeriodViewController.h"
#import "HistoryViewController.h"
#import "Period.h"

@interface SelectPeriodViewController ()

@end

@implementation SelectPeriodViewController

@synthesize historyPeriodArray = historyPeriodArray_;
@synthesize selectedPeriod = selectedPeriod_;
@synthesize delegate = delegate_;

- (id)init {
  self = [super initWithNibName:@"SelectPeriodViewController" bundle:nil];
  if (self) {
    
  }
  return self;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  
  Period *currWeek = [[Period alloc] initWithPeriod:currentWeek];
  Period *currMonth = [[Period alloc] initWithPeriod:currentMonth];
  Period *prevWeek = [[Period alloc] initWithPeriod:previousWeek];
  Period *prevMont = [[Period alloc] initWithPeriod:previousMonth];
  Period *allPeriod = [[Period alloc] initWithPeriod:all];
  
  NSArray *periods = [NSArray arrayWithObjects:currWeek, currMonth, prevWeek, prevMont, allPeriod, nil];
  [self setHistoryPeriodArray:periods];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [self.navigationItem setTitle:NSLocalizedString(@"history.period.view.title", nil)];
}

- (void)viewDidUnload
{
  [super viewDidUnload];
  // Release any retained subviews of the main view.
  // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  return NSLocalizedString(@"history.period.section.title", nil);
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }
  Period *period = [historyPeriodArray_ objectAtIndex:indexPath.row];
  [cell.textLabel setText:period.periodName];
  if ([period.periodName isEqualToString:selectedPeriod_.periodName]) {
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
  }
  [cell.textLabel setTextColor:[UIColor darkGrayColor]];
  [cell.textLabel setFont:[UIFont systemFontOfSize:17]];
  [cell setBackgroundColor:[UIColor clearColor]];
  return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  [self setSelectedPeriod:(Period *)[historyPeriodArray_ objectAtIndex:indexPath.row]];
  [self.delegate updatePeriodTo:selectedPeriod_];
  [self.navigationController popViewControllerAnimated:YES];
}

@end
