//
//  NSDate+Common.m
//  Coding_iOS
//
//  Created by 王 原闯 on 14-8-16.
//  Copyright (c) 2014年 Coding. All rights reserved.
//

#import "NSDate+Common.h"

@implementation NSDate (Common)

- (BOOL)isSameDay:(NSDate*)anotherDate{
	NSCalendar* calendar = [NSCalendar currentCalendar];
	NSDateComponents* components1 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self];
	NSDateComponents* components2 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:anotherDate];
	return ([components1 year] == [components2 year] && [components1 month] == [components2 month] && [components1 day] == [components2 day]);
}

- (NSInteger)secondsAgo{
    NSCalendar *calendar = [[self class] sharedCalendar];
    NSDateComponents *components = [calendar components:(NSSecondCalendarUnit)
                                               fromDate:self
                                                 toDate:[NSDate date]
                                                options:0];
    return [components second];
}
- (NSInteger)minutesAgo{
    NSCalendar *calendar = [[self class] sharedCalendar];
    NSDateComponents *components = [calendar components:(NSMinuteCalendarUnit)
                                               fromDate:self
                                                 toDate:[NSDate date]
                                                options:0];
    return [components minute];
}
- (NSInteger)hoursAgo{
    NSCalendar *calendar = [[self class] sharedCalendar];
    NSDateComponents *components = [calendar components:(NSHourCalendarUnit)
                                               fromDate:self
                                                 toDate:[NSDate date]
                                                options:0];
    return [components hour];
}

- (NSInteger)weeksAgo{
    NSCalendar *calendar = [[self class] sharedCalendar];
    NSDateComponents* components1 = [calendar components:(NSWeekOfYearCalendarUnit) fromDate:self];
    NSDateComponents* components2 = [calendar components:(NSWeekOfYearCalendarUnit) fromDate:[NSDate date]];
    
    return components2.weekOfYear - components1.weekOfYear;
}

- (NSInteger)monthsAgo{
    NSCalendar *calendar = [[self class] sharedCalendar];
    NSDateComponents *components = [calendar components:(NSMonthCalendarUnit)
                                               fromDate:self
                                                 toDate:[NSDate date]
                                                options:0];
    return [components month];
}

- (NSInteger)yearsAgo{
    NSCalendar *calendar = [[self class] sharedCalendar];
    NSDateComponents *components = [calendar components:(NSYearCalendarUnit)
                                               fromDate:self
                                                 toDate:[NSDate date]
                                                options:0];
    return [components year];
}

//返回星期几
-(NSString*) stringOfWeekDay
{
    //通过第三方 获取是某星期的第几天 然后通过第三方返回的天数作为数组的下坐标取得某天(比如: 星期2)
    NSArray* weekdays = @[@"temp",@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
    return [weekdays objectAtIndex:[self weekday]];
}

- (NSString *)stringTimesAgo{
    if ([self compare:[NSDate date]] == NSOrderedDescending) {
        return @"刚刚";
    }
    
    NSString *text = nil;

    NSInteger agoCount = [self weeksAgo];
    if (agoCount > 0) {
        text = [self stringWithFormat:@"yy/MM/dd"];
    }else{
        agoCount = [self daysAgoAgainstMidnight];
        if (agoCount > 0) {
            if (agoCount == 1) {
                text = @"昨天";
            }else{
                text = [self stringOfWeekDay];
            }
        }else{
            agoCount = [self hoursAgo];
            if (agoCount > 0) {
                text = [NSString stringWithFormat:@"%ld小时前", (long)agoCount];
            }else{
                agoCount = [self minutesAgo];
                if (agoCount > 0) {
                    text = [NSString stringWithFormat:@"%ld分钟前", (long)agoCount];
                }else{
                    text = @"刚刚";
                }
            }
        }
    }
    return text;
}

- (NSString *)string_yyyy_MM_dd_EEE{
    NSString *text = [self stringWithFormat:@"yyyy-MM-dd EEE"];
    NSInteger daysAgo = [self daysAgoAgainstMidnight];
    switch (daysAgo) {
        case 0:
            text = [text stringByAppendingString:@"（今天）"];
            break;
        case 1:
            text = [text stringByAppendingString:@"（昨天）"];
            break;
        default:
            break;
    }
    return text;
}

//对于消息  显示一个发送的时间点 (比如: 昨天,某年某月) 然后将这个时间点以文本的方式返回
- (NSString *)stringTimeDisplay{
    NSString *text = nil;
    //daysAgoAgainstMidnight :  午夜时间距今几天  返回一个天数
    NSInteger daysAgo = [self daysAgoAgainstMidnight];
    NSString *dateStr;
    
    // 用返回的天数判断
    switch (daysAgo) {
            //天数小于 1
        case 0:
            dateStr = @"";
            break;
            //天数等于 1
        case 1:
            dateStr = @"昨天";
            break;
            //天数大于 1
        default:
            if ([self weeksAgo] == 0) {
                //stringOfWeekDay: 返回星期几
                dateStr = [self stringOfWeekDay];
            }else{
                //string_yyyy_MM_dd: 返回年月日
                dateStr = [self string_yyyy_MM_dd];
            }
            
            break;
    }
    // String_a_HH_mm:   会获取一天当中的一个时间段 比如: 中午,下午  --
    if ([dateStr isEqualToString:@""]) {
            text = [NSString stringWithFormat:@"%@", [self string_a_HH_mm]];
    }
    else{
        text = [NSString stringWithFormat:@"%@ %@", dateStr, [self string_a_HH_mm]];
    }
    
    return text;
}

//返回年月日
- (NSString *)string_yyyy_MM_dd{
    return [self stringWithFormat:@"yyyy-MM-dd"];
}

//将一天当中的 凌晨,上午,中午,下午,晚上划分出来,以文本形式返回
- (NSString *)string_a_HH_mm{
    NSString *text = nil;
    NSString *aStr, *timeStr;
    timeStr = [self stringWithFormat:@"hh:mm"];
    NSUInteger hour = [self hour];
    /*原始
    if (hour < 3) {
        aStr = @"凌晨";
    }else if (hour >= 3 && hour < 12){
        aStr = @"上午";
    }else if (hour >= 12 && hour < 13){
        aStr = @"中午";
    }else if (hour >= 13 && hour < 18){
        aStr = @"下午";
    }else{
        aStr = @"晚上";
    }
    */
    if(hour<12){
        aStr = @"上午";
    }
    else aStr = @"下午";
    //判断系统当前是12小时制还是24小时制。
    NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
    NSRange containsA = [formatStringForHours rangeOfString:@"a"];
    BOOL hasAMPM = containsA.location != NSNotFound;
    //hasAMPM==TURE为12小时制，否则为24小时制
    if(hasAMPM){
        text = [NSString stringWithFormat:@"%@ %@", aStr, timeStr];
    }
    else{
        text = [NSString stringWithFormat:@"%@", timeStr];
    }
    
    //    text = [NSString stringWithFormat:@"%@ %@", aStr, timeStr];
    
    return text;
}
+ (NSString *)convertStr_yyyy_MM_ddToDisplay:(NSString *)str_yyyy_MM_dd{
    NSString *displayStr = @"";
    if (str_yyyy_MM_dd && str_yyyy_MM_dd.length > 0) {
        NSDate *date = [NSDate dateFromString:str_yyyy_MM_dd withFormat:@"yyyy-MM-dd"];
        if (date) {
            NSDate *today = [NSDate dateFromString:[[NSDate date] stringWithFormat:@"yyyy-MM-dd"] withFormat:@"yyyy-MM-dd"];
            if ([date year] != [today year]) {
                displayStr = [date stringWithFormat:@"yyyy年MM月dd日"];
            }else{
                NSCalendar *calendar = [[self class] sharedCalendar];
                NSDateComponents *components = [calendar components:(NSDayCalendarUnit)
                                                           fromDate:today
                                                             toDate:date
                                                            options:0];
                NSInteger leftDayCount = [components day];
                switch (leftDayCount) {
                    case 2:
                        displayStr = @"后天";
                        break;
                    case 1:
                        displayStr = @"明天";
                        break;
                    case 0:
                        displayStr = @"今天";
                        break;
                    case -1:
                        displayStr = @"昨天";
                        break;
                    case -2:
                        displayStr = @"前天";
                        break;
                    default:
                        displayStr = [date stringWithFormat:@"MM月dd日"];
                        break;
                }
            }
        }
        
    }
    return displayStr;
}

- (NSInteger)leftDayCount{
    NSCalendar *calendar = [[self class] sharedCalendar];
    NSDate *today = [NSDate dateFromString:[[NSDate date] stringWithFormat:@"yyyy-MM-dd"] withFormat:@"yyyy-MM-dd"];
    NSDateComponents *components = [calendar components:(NSDayCalendarUnit)
                                               fromDate:today
                                                 toDate:self
                                                options:0];
    return [components day];
}

#pragma mark --自定义外部（Recent）时间规则
- (NSString *)stringTimesCustomOutSideRule{
    if ([self compare:[NSDate date]] == NSOrderedDescending) {
        return [self stringWithFormat:@"HH:mm"];
    }
    
    NSString *text = nil;
    
    NSInteger agoCount = [self yearsAgo];
    if (agoCount > 0) {//年比较
        text = [self stringWithFormat:@"YYYY-MM-dd"];
        
    }else{
        agoCount = [self monthsAgo];
        if (agoCount > 0) {//月比较
            text = [self stringWithFormat:@"MM-dd"];
        }
        else//agoCount == 0
        {
            agoCount = [self daysAgoAgainstMidnight];//日比较
            if (agoCount >1) {
                text = [self stringWithFormat:@"MM-dd"];
            }
            else if (agoCount==1){
                text =@"昨天";
            }
            else if (agoCount==0){
                //agoCount  = [self minutesAgo];
                //if (agoCount>=5) {
                text = [self stringWithFormat:@"HH:mm"];
                //}
                
            }
        }
    }
    return text;
    
}
#pragma mark --自定义内部（ChatMain）时间规则
-(NSString *)stringTimesCustomInSideRule{
    NSString *text = nil;
    //daysAgoAgainstMidnight :  午夜时间距今几天  返回一个天数
    NSInteger Ago = [self yearsAgo];
    NSString *dateStr;
    if (Ago>0) {
        text = [self stringWithFormat:@"YYYY-MM-dd"];
    }
    else if (Ago ==0){
        Ago = [self monthsAgo];
        if (Ago>0) {
            text = [self stringWithFormat:@"MM-dd"];
        }
        else// (Ago ==0)
        {
            Ago = [self daysAgoAgainstMidnight];
            if (Ago>1) {
                text = [self stringWithFormat:@"MM-dd"];
            }
            else if (Ago ==1){
                text = @"昨天";
            }
            else if (Ago==0){
                //text = @"今天";
            }
        }
    }
    if (!text) {
        return [NSString stringWithFormat:@"%@",[self string_a_HH_mm]];
    }
    else{
        return [NSString stringWithFormat:@"%@ %@", text, [self string_a_HH_mm]];
        
    }
    
    
}




@end
