//
// BusProductApi.m
// Project
//
// Created by Ziv on 2017/5/17.
//
//

#import "BusStatusApi.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "Utility.h"
#import "Vars.h"
#import "DrawUtils.h"

@implementation BusStatusApi
+ (NSAttributedString *)attributedStringForBusStatusLocationWithPlacemark:(CLPlacemark *)placemark timestamp:(NSDate *)timestamp product:(Product *)product schedule:(Schedule *)schedule {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
    if (product) {
        [attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:product.name attributes:@{NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]}]];
        [attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\nBus ID " attributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]}]];
        [attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:product.schedule_id attributes:@{NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]}]];
    } else if (schedule) {
        [attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:schedule.route.shortName attributes:@{NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]}]];
    }
    [attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n\n" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:5]}]];
    [attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:[NSString stringWithFormat:@"Currently moving along %@, %@", placemark.thoroughfare, placemark.locality] attributes:@{NSForegroundColorAttributeName : [UIColor darkGrayColor], NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]}]];
    [attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n\n" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:5]}]];
    [attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:[NSString stringWithFormat:@"Last updated: %@", [Utility localizedStringWithDatetime:timestamp]] attributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]}]];
    return [[NSAttributedString alloc] initWithAttributedString:attributedString];
}
@end



@implementation BusStatusDetail

- (instancetype)init {
	if (self = [super init]) {
		self.apiVersion = 1;
	}
	return self;
}

- (BOOL)anonymous {
	return YES;
}

-(NSString *)namespace{
    if (self.isDBDSearch) {
        return @"api-bus-status";
    }
    return [super namespace];
}


@end

@implementation BusStatusTimeBean
@end

@implementation BusStatusDetailBean

#pragma mark - Property

- (BOOL)isCancelled {
	return self.departureStopStatus.status == 3 || self.arrivalStopStatus.status == 3;
}

- (NSAttributedString *)attributedScheduledTimeText {
	NSMutableAttributedString *sText = [[NSMutableAttributedString alloc] init];
    if (![StringUtils isEmpty:self.scheduleStop.arrivalTime]) {
       if (!self.combinationDepCross) {
            [sText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"A" attributes:@{NSForegroundColorAttributeName : [Vars defaultTintColor]}]];
            [sText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@" "]];
            [sText appendAttributedString:[[NSAttributedString alloc] im_initWithString:self.scheduleStop.arrivalTime]];
        }
    }

	if (![StringUtils isEmpty:self.scheduleStop.arrivalTime] && ![StringUtils isEmpty:self.scheduleStop.leavingTime] && !self.combinationArrCross && !self.combinationDepCross) {
		[sText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
	}
    
	if (![StringUtils isEmpty:self.scheduleStop.leavingTime]) {
        if (!self.combinationArrCross) {
            [sText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"D" attributes:@{NSForegroundColorAttributeName : [Vars defaultTintColor]}]];
            [sText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@" "]];
            [sText appendAttributedString:[[NSAttributedString alloc] im_initWithString:self.scheduleStop.leavingTime]];
        }
	}
    
	return sText;
}

- (NSAttributedString *)attributedStatusTimeText {
	NSMutableAttributedString *aText = [[NSMutableAttributedString alloc] init];
	if (self.arrivalStopStatus) {
		if (self.arrivalStopStatus.status == 3) {
			[aText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"N/A"]];
		} else {
            if (!self.combinationDepCross) {
                [aText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"A" attributes:@{NSForegroundColorAttributeName : [Vars defaultTintColor]}]];
                [aText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@" "]];
                [aText appendAttributedString:[[NSAttributedString alloc] im_initWithString:self.arrivalStopStatus.isPassed ? self.arrivalStopStatus.time : [self.arrivalStopStatus.time im_stringByAppendingString:@"*"] attributes:@{ NSForegroundColorAttributeName : self.arrivalStopStatus.status == 2 ? [UIColor redColor] : [UIColor blackColor] }]];
            }
		}
	}
	if (self.arrivalStopStatus && self.departureStopStatus && !self.combinationDepCross && !self.combinationArrCross) {
		[aText appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
	}
	if (self.departureStopStatus) {
		if (self.departureStopStatus.status == 3) {
			[aText appendAttributedString:[[NSAttributedString alloc] initWithString:@"N/A"]];
		} else {
            if (!self.combinationArrCross) {
			[aText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"D" attributes:@{NSForegroundColorAttributeName : [Vars defaultTintColor]}]];
			[aText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@" "]];
            [aText appendAttributedString:[[NSAttributedString alloc] im_initWithString:self.departureStopStatus.isPassed ? self.departureStopStatus.time : [self.departureStopStatus.time im_stringByAppendingString:@"*"] attributes:@{ NSForegroundColorAttributeName : self.departureStopStatus.status == 2 ? [UIColor redColor] : [UIColor blackColor] }]];            }
            }
	}
	return aText;
}

@end

@implementation BusStatusLocationBean
@end

@implementation BusStatusDetailResponse

- (NSInteger)stopIndex {
	__block NSInteger stopIndex = -1;
	[self.resultList enumerateObjectsUsingBlock:^(BusStatusDetailBean *obj, NSUInteger idx, BOOL *stop) {
		if (obj.departureStopStatus.isPassed || obj.arrivalStopStatus.isPassed) {
			stopIndex = idx;
		}
	}];
	return stopIndex;
}

- (CGFloat)stopProgress {
	return self.locationPercent;
}

@end

@implementation ScheduleDriverAssignInfo
-(NSString *)namespace{
    if (self.isDBDSearch) {
        return @"api-bus-status";
    }
    return [super namespace];
}
@end

@implementation ScheduleDriverAssignInfoResponse
@end

@implementation UpdateBusStatusTime
@end
