//
//  NSData+Helper.m
//  ChinaDailyForiPad
//
//  Created by 看看智能 on 2018/11/1.
//  Copyright © 2018 王双龙. All rights reserved.
//

#import "NSDate+Helper.h"
#import "ZYSuspensionView.h"

static const unsigned componentFlags = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitQuarter);

@implementation NSDate (Helper)
#pragma mark - Public Method
+ (NSCalendar *) currentCalendar
{
    static NSCalendar *sharedCalendar = nil;
    if (!sharedCalendar)
        sharedCalendar = [NSCalendar autoupdatingCurrentCalendar];
    return sharedCalendar;
}

- (BOOL) isSameWeekAsDate:(NSDate *)otherDate
{
    BOOL result = NO;
    
    NSTimeInterval startInterval = [self timeIntervalAtStartOfDaySince1970];
    NSTimeInterval endInterval = [self timeIntervalAtEndOfDaySince1970];
    NSTimeInterval otherInterval = [otherDate timeIntervalSince1970];
    NSUInteger weekday = [self customWeekday];
    startInterval = startInterval - (weekday-1) * D_DAY;
    endInterval = endInterval + (7-weekday) * D_DAY;
    if (otherInterval >= startInterval && otherInterval <= endInterval) {
        result = YES;
    }
    
    return result;
}

#pragma mark - Private Method
- (NSDate *) dateAtStartOfDay
{
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    components.hour = 0;
    components.minute = 0;
    components.second = 0;
    return [[NSDate currentCalendar] dateFromComponents:components];
}

- (NSDate *) dateAtEndOfDay
{
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    components.hour = 23;
    components.minute = 59;
    components.second = 59;
    return [[NSDate currentCalendar] dateFromComponents:components];
}

- (NSTimeInterval)timeIntervalAtStartOfDaySince1970
{
    return [[self dateAtStartOfDay] timeIntervalSince1970];
}

- (NSTimeInterval)timeIntervalAtEndOfDaySince1970
{
    return [[self dateAtEndOfDay] timeIntervalSince1970];
}

#pragma mark - getter or setter
- (NSInteger) weekday
{
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    return components.weekday;
}

- (NSInteger)customWeekday
{
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    return components.weekday==1 ? 7 : --components.weekday;
}

- (instancetype)getDateFromDateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day hours:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second{
   
    NSDateComponents* dateComponents;
    
    NSCalendar *dateCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
   
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

    dateComponents = [dateCalendar components:unitFlags fromDate:self];

    NSDateComponents *myCompoentes = [[NSDateComponents alloc]init];
    [myCompoentes setYear:dateComponents.year + year];
    [myCompoentes setMonth:dateComponents.month + month];
    [myCompoentes setDay:dateComponents.day + day];
    [myCompoentes setHour:dateComponents.hour + hour];
    [myCompoentes setMinute:dateComponents.minute + minute];
    [myCompoentes setSecond:dateComponents.second + second];

    NSDate *myDate = [dateCalendar dateFromComponents:myCompoentes];
    
    return myDate;
}


-(NSString*)transferStrngDate{
    

    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *newdate = [dateFormatter stringFromDate:date];

    NSString *oldDate =[[NSUserDefaults standardUserDefaults ]objectForKey:@"date_kankan"];
    
    
    if ( oldDate== NULL  ) {

        
        [[NSUserDefaults standardUserDefaults ] setObject:newdate forKey:@"date_kankan"];
        [[NSUserDefaults standardUserDefaults ]synchronize];
        return @"首次显示";
        
        
        
    }else{
        if ([oldDate isEqualToString:newdate]) {
            return  newdate;
        }else{
        
            
            if ([self isSameWeekAsDate:[self zipFornowDate:oldDate]] == NO) {
                [[NSUserDefaults standardUserDefaults ] setObject:newdate forKey:@"date_kankan"];
                [[NSUserDefaults standardUserDefaults ]synchronize];
                return  oldDate;
            }
            

            return oldDate;
        }
    }
    
    
}
-(NSDate*)zipFornowDate:(NSString*)birthdayStr{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
    if (birthdayStr || ![birthdayStr isEqualToString:@""]) {
        NSDate *birthdayDate = [dateFormatter dateFromString:birthdayStr];
        return birthdayDate;
    }
    return    [dateFormatter dateFromString:@"2016-10-03 13:12:12"];
    
}

@end
