//
// TrackBusStatusInfo.m
// LiveProject
//
// Created by Ziv on 15/8/28.
//
//

#import "BusStatusInfo.h"
#import "Lang.h"
#import "SDK.h"

@implementation BusStatusInfo

#pragma mark - Property

- (NSString *)statusName {
	return [BusStatusInfo nameForStatus:self.status];
}

- (NSString *)otherDescription {
	if (self.status == 1) {
		if (![StringUtils isEmpty:self.note]) {
			return [NSString stringWithFormat:@"Note: %@", self.note];
		}
	} else if (self.status == 2 || self.status == 4) {
		NSMutableString *string = [NSMutableString string];
		[string im_appendString:self.statusName];
		if (self.hour > 0 || self.min > 0) {
			[string im_appendString:@" "];
		}
		[string im_appendString:[BusStatusInfo timeStringOfHour:self.hour andMinute:self.min]];
		if (self.hour > 0 || self.min > 0) {
			[string im_appendString:@"."];
		}
		if (![StringUtils isEmpty:self.note]) {
			[string im_appendString:@" "];
			[string im_appendString:self.note];
		}
		return [NSString stringWithFormat:@"Note: %@", string];
	} else if (self.status == 3) {
		NSString *reason = [BusStatusInfo reasonForCancelledType:self.cancelled_type];
		if (self.cancelled_type == 4) {
			reason = self.note;
		}
		if (![StringUtils isEmpty:reason]) {
			return [NSString stringWithFormat:@"Reason: Due to %@", reason];
		}
	}
	return nil;
}

- (UIColor *)statusColor {
	UIColor *statuColor = nil;
	if (self.status == 1) {
		statuColor = [UIColor cloverColor];
	} else {
		statuColor = [UIColor redColor];
	}
	return statuColor;
}

#pragma mark - Public

- (void)trim {
	if (self.status != 2 && self.status != 4) {
		self.hour = 0;
		self.min = 0;
	}
	if (self.status == 3 && self.cancelled_type != 4) {
		self.note = nil;
	}
}

+ (NSString *)nameForStatus:(NSInteger)status {
	if (status == 1) {
		return @"Scheduled";
	} else if (status == 2) {
		return @"Delayed";
	} else if (status == 3) {
		return @"Cancelled";
	} else if (status == 4) {
		return @"Departed early";
	}
	return nil;
}

+ (NSString *)reasonForCancelledType:(NSInteger)cancelled_type {
	NSString *reason = nil;
	switch (cancelled_type) {
		case 1:
			reason = @"Mechanical Problem";
			break;

		case 2:
			reason = @"Insufficient Travellers";
			break;

		case 3:
			reason = @"Weather";
			break;

		case 4:
			reason = @"Other";
			break;

		default:
			break;
	}
	return reason;
}

+ (NSString *)timeStringOfHour:(NSInteger)hour andMinute:(NSInteger)minute {
	if (hour == 0 && minute == 0) {
		return nil;
	}
	NSMutableString *string = [NSMutableString string];
	if (hour > 0) {
		[string appendFormat:@"%d %@", hour, hour > 1 ? @"hours" : @"hour"];
	}
	if (hour > 0 && minute > 0) {
		[string im_appendString:@" "];
	}
	if (minute > 0) {
		[string appendFormat:@"%d %@", minute, minute > 1 ? @"minutes" : @"minute"];
	}
	return [NSString stringWithString:string];
}

@end
