//
// CartEntry.m
// GoToBus
//
// Created by iphone development on 7/19/14.
//
//

#import "CartEntry.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import <BlocksKit/BlocksKit.h>
#import "SalesEntryRoom.h"
#import "SoldStock.h"
#import "Vars.h"

@implementation CartEntry

- (instancetype)init {
	if (self = [super init]) {
		self.options = [NSMutableDictionary dictionary];
		self.stocks = [NSMutableArray array];
		self.rooms = [NSMutableArray array];
		self.product = [[Product alloc] init];
	}
	return self;
}

#pragma mark - Property

- (NSArray *)roomBasedCountDatas {
	NSMutableArray *array = [NSMutableArray array];
	for (SalesEntryRoom *room in self.rooms) {
		RoomBasedCountData *countData = [[RoomBasedCountData alloc] init];
		countData.roomNo = room.room_no;
		for (NSString *type in self.product.roomBasedPrice.allowedTypes) {
			[countData setValue:@([SoldStock filteredStocks:room.stocks byBasedType:type].count) forKeyPath:[[StringUtils uncapitalize:type] im_stringByAppendingString:@"Count"]];
		}
		[array addObject:countData];
	}
	return [NSArray arrayWithArray:array];
}

- (void)setRoomBasedCountDatas:(NSArray *)countDatas {
	[self.rooms removeAllObjects];
	for (RoomBasedCountData *countData in countDatas) {
		SalesEntryRoom *room = [[SalesEntryRoom alloc] init];
		room.room_no = countData.roomNo;
		for (NSString *type in self.product.roomBasedPrice.allowedTypes) {
			for (int i = 0; i < [[countData valueForKeyPath:[[StringUtils uncapitalize:type] im_stringByAppendingString:@"Count"]] intValue]; i++) {
				SoldStock *stock = [[SoldStock alloc] init];
				stock.people_type = type;
				if (self.product.roomBasedPrice.enable_child_age && [type isEqualToString:@"Child"]) {
					if (countData.childAges.count > 0) {
						stock.people_age = [countData.childAges[i] intValue];
					}
					if (stock.people_age < 0) {
						stock.people_age = self.product.roomBasedPrice.child_age_from;
					}
				}
				[room.stocks addObject:stock];
			}
		}
		[self.rooms addObject:room];
	}
}

- (PPLBasedCountData *)PPLBasedCountData {
	PPLBasedCountData *countData = [[PPLBasedCountData alloc] init];
	for (NSString *type in self.product.PPLBasedPrice.allowedTypes) {
		[countData setValue:@([SoldStock filteredStocks:self.stocks byBasedType:type].count) forKeyPath:[[StringUtils uncapitalize:type] im_stringByAppendingString:@"Count"]];
	}
	return countData;
}

- (void)setPPLBasedCountData:(PPLBasedCountData *)countData {
	[self.stocks removeAllObjects];
	for (NSString *type in self.product.PPLBasedPrice.allowedTypes) {
		for (int i = 0; i < [[countData valueForKeyPath:[[StringUtils uncapitalize:type] im_stringByAppendingString:@"Count"]] intValue]; i++) {
			SoldStock *stock = [[SoldStock alloc] init];
			stock.people_type = type;
			if (self.product.PPLBasedPrice.enable_child_age && [type isEqualToString:@"Child"]) {
				stock.people_age = self.product.PPLBasedPrice.child_age_from;
			}
			[self.stocks addObject:stock];
		}
	}
}

- (NSArray *)serialStocks {
	NSArray *stocks = nil;
	if (self.rooms.count == 0) {
		stocks = self.stocks;
	} else {
		if (self.product.is_hotel) {
			stocks = [self.rooms valueForKeyPath:@"checkin_guest"];
		} else {
			stocks = [self.rooms valueForKeyPath:@"@unionOfArrays.stocks"];
		}
	}
	return stocks;
}

- (NSInteger)peopleCount {
	if (self.roomCount > 0) {
		return [[self.roomBasedCountDatas valueForKeyPath:@"@sum.peopleCount"] intValue];
	} else {
		return self.PPLBasedCountData.peopleCount;
	}
}

- (NSInteger)roomCount {
	return self.roomBasedCountDatas.count;
}

- (NSInteger)nightCount {
	NSString *checkIn = [self.options[@"date"][@"checkIn"] substringToIndex:10];
	NSString *checkOut = [self.options[@"date"][@"checkOut"] substringToIndex:10];
	return [CocoaUtils dateIntervalForComponent:NSCalendarUnitDay fromDate:[[DateFormatUtils ISO_DATE_FORMAT] parse:checkIn] toDate:[[DateFormatUtils ISO_DATE_FORMAT] parse:checkOut]];
}

- (NSString *)shortDescription {
	NSInteger roomNum = self.roomCount;
	if (roomNum > 0) {
		NSMutableString *string = [NSMutableString string];
		NSString *roomString = [NSString stringWithFormat:roomNum > 1 ? IMLocalizedString(@"%d rooms") : IMLocalizedString(@"%d room"), roomNum];
		[string im_appendString:roomString];
		[string im_appendString:IMLocalizedString(PUNCTUATION_COMMA)];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		if (self.product.is_hotel) {
			NSInteger nightNum = self.nightCount;
			NSString *nightString = [NSString stringWithFormat:nightNum > 1 ? IMLocalizedString(@"%d nights") : IMLocalizedString(@"%d night"), nightNum];
			[string im_appendString:nightString];
		} else {
			NSInteger stockNum = self.peopleCount;
			NSString *stockString = [NSString stringWithFormat:stockNum > 1 ? IMLocalizedString(@"%d travelers") : IMLocalizedString(@"%d traveler"), stockNum];
			[string im_appendString:stockString];
		}
		return [NSString stringWithString:string];
	} else {
		NSInteger stockNum = self.peopleCount;
		NSString *stockString = [NSString stringWithFormat:stockNum > 1 ? IMLocalizedString(@"%d travelers") : IMLocalizedString(@"%d traveler"), stockNum];
		return stockString;
	}
}

@end
