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

#import "Period.h"

@interface Period (Private)

- (void)setNameForPeriod:(PeriodType)period;

@end

@implementation Period

@synthesize beginningDate = beginningDate_;
@synthesize endDate = endDate_;
@synthesize periodName = periodName_;
@synthesize periodType = periodType_;

- (id)initWithPeriod:(PeriodType)periodType {
  self = [super init];
  if (self) {
    [self setPeriodType:periodType];
    [self setNameForPeriod:periodType];
    [self setPeriodDates];
  }
  return self;
} 

- (void)setNameForPeriod:(PeriodType)period
{
  switch (periodType_) {
    case currentWeek: {
      [self setPeriodName:NSLocalizedString(@"history.period.name.current.week", nil)];
      break;
    }
    case currentMonth: {
      [self setPeriodName:NSLocalizedString(@"history.period.name.current.month", nil)];
      break;
    }
    case previousWeek: {
      [self setPeriodName:NSLocalizedString(@"history.period.name.previous.week", nil)];
      break;
    }
    case previousMonth: {
      [self setPeriodName:NSLocalizedString(@"history.period.name.previous.month", nil)];
      break;
    }    
    case all: {
      [self setPeriodName:NSLocalizedString(@"history.period.name.all", nil)];
      break;
    }
    default:
      break;
  }
}

- (void)setPeriodDates
{
  NSCalendar *currentCalendar = [NSCalendar currentCalendar];
  [currentCalendar setFirstWeekday:2];
  NSDateComponents *comp;
  NSDate *beginningD;
  NSDate *endD;
  
  switch (self.periodType) {
    case (currentWeek): {
      comp = [currentCalendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:[NSDate date]];
      [comp setWeekday:2];
      beginningD = [currentCalendar dateFromComponents:comp];
      endD = [NSDate date];
      break;
    }
    case (currentMonth): {
      comp = [currentCalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
      [comp setDay:1];
      beginningD = [currentCalendar dateFromComponents:comp];
      endD = [NSDate date];
      break;
    }
    case (previousWeek): {
      comp = [currentCalendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:[NSDate date]];
      [comp setWeek:([comp week] - 1)];
      [comp setWeekday:1];
      endD = [currentCalendar dateFromComponents:comp];
      
      [comp setWeekday:2];
      
      beginningD = [currentCalendar dateFromComponents:comp];
      
      break;
    }
    case (previousMonth): {
      comp = [currentCalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
      [comp setDay:-0];
      endD = [currentCalendar dateFromComponents:comp];
      
      [comp setMonth:([comp month] - 1)];
      [comp setDay:1];      
      beginningD = [currentCalendar dateFromComponents:comp];      
      break;
    } 
    case (all): {
      comp = [currentCalendar components:(NSYearCalendarUnit) fromDate:[NSDate date]];
      [comp setYear:2010];
      beginningD = [currentCalendar dateFromComponents:comp];
      endD = [NSDate date];
    }
    default:
      break;
  }
  
  [self setBeginningDate:beginningD];
  [self setEndDate:endD];  
  
}

@end
