//
//  KWMCartResult.m
//  iCemarose
//
//  Created by lee on 2017/5/18.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMCartResult.h"
#import "KWMImageUtil.h"

@implementation KWMCartResult

- (instancetype)initWithDictionary:(NSDictionary *)dict error:(NSError **)err {
    self = [super initWithDictionary:dict error:err];
    if (self) {
        NSArray *dataArray = dict[@"items"];
        if(dataArray){
            self.items = [self buildListData:[KWMShopCartModel class] jsonDictionary:dataArray];
        }
    }
    
    return self;
}

- (instancetype)initWithDictionary:(NSDictionary *)dict modelClass:(Class)modelClass error:(NSError *__autoreleasing *)err {
    if (self = [super initWithDictionary:dict error:err]) {
        NSArray *dataArray = dict[@"items"];
        if(dataArray){
            self.items = [self buildListData:[KWMShopCartModel class] jsonDictionary:dataArray];
        }
    }
    return self;
}

- (NSArray *)buildListData:(Class)modelClass jsonDictionary:(NSArray *)dataArray {
    NSMutableArray *datas = [NSMutableArray arrayWithCapacity:dataArray.count];
    for (id item in dataArray) {
        KWMShopCartModel *shopCartModel = [[KWMShopCartModel alloc] init];
        shopCartModel.name = item[@"title"];
        shopCartModel.brand = item[@"vendor"];
        shopCartModel.identifier = [item[@"id"] stringValue];
        shopCartModel.size = item[@"variant_title"];
        shopCartModel.product_id = item[@"product_id"];
        shopCartModel.quantity = [item[@"quantity"] integerValue];
//        shopCartModel.price = [item[@"discounted_price"] floatValue] / 100;
        shopCartModel.price = [[NSDecimalNumber decimalNumberWithString:[item[@"discounted_price"] stringValue]] decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithDecimal:@(100).decimalValue]];
        shopCartModel.line_price = [[NSDecimalNumber decimalNumberWithString:[item[@"line_price"] stringValue]] decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithDecimal:@(100).decimalValue]];
        shopCartModel.imageStr = [KWMImageUtil getProductImageUrlByOriginalUrl:item[@"image"] ImageSize:SmallImage];
        shopCartModel.shopCartDict = item;
        [datas addObject:shopCartModel];
    }
    return datas;
}

@end