//
//  KWMShopCartData.m
//  iCemarose
//
//  Created by 陈荣科 on 16/9/22.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMShopCartData.h"
#define KWM_MAX_PRODUCT_COUNT               9999

@implementation KWMShopCartData

-(void)syncCartProducts:(NSArray *)items {
    [self removeAllItems];
    for (KWMShopCartModel *item in items) {
        [self inocreaseItem:item];
    }
}
//添加购物商品
- (void) inocreaseItem:(KWMShopCartModel *)shopCartModel{
    KWMShopCartItem *shopCartItem = [self toShopCartProduct:shopCartModel];
//    NSInteger finalValue = shopCartItem.quantity+1;
    NSInteger finalValue = shopCartItem.quantity;
    if (finalValue > KWM_MAX_PRODUCT_COUNT) {
        finalValue = KWM_MAX_PRODUCT_COUNT;
    }
//    shopCartItem.quantity = finalValue;
    [self saveAll];
}

//删除购物商品
- (void) decreaseItem:(KWMShopCartModel *)shopCartModel{
    KWMShopCartItem *shopCartItem = [self toShopCartProduct:shopCartModel];
    NSInteger finalValue = shopCartItem.quantity - 1;
    if (finalValue < 0) {
        finalValue = 0;
        [self removeItem:shopCartModel];
        return;
    }
    shopCartItem.quantity = finalValue;
    [self saveAll];
}

- (void)saveAll {
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}

//移除商品
- (void) removeItem:(KWMShopCartItem *)shopCartItem{
    NSPredicate *filterById = [NSPredicate predicateWithFormat:@"identifier == %@", shopCartItem.identifier];
    NSArray *existsArray = [KWMShopCartItem MR_findAllWithPredicate:filterById];
    
    if(existsArray && existsArray.count > 0){
        KWMShopCartItem *exists = existsArray.firstObject;
        KWMShopCartModel *model = [self toProduct:exists];
        [exists MR_deleteEntity];
        
        [self saveAll];
    }
}

//移除所有商品
- (void) removeAllItems{
    NSArray *shopCartArr = [self getALLItems];
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"1 == 1"];
    for (KWMShopCartModel *object in shopCartArr) {
        KWMShopCartItem *item = [self toShopCartProduct:object];
        [item MR_deleteEntity];
    }
//    [KWMShopCartItem MR_deleteAllMatchingPredicate:predicate];
    
    [self saveAll];
}

// 获取所有商品
- (NSArray *) getALLItems{
    NSArray *shopCartArr = [KWMShopCartItem MR_findAll];
    if (shopCartArr) {
        NSMutableArray *modelArr = [[NSMutableArray alloc] initWithCapacity:100];
        for (NSInteger i = 0; i< shopCartArr.count; i++) {
            KWMShopCartItem *item = [shopCartArr objectAtIndex:i];
            KWMShopCartModel *model = [self toProduct:item];
            [modelArr addObject:model];
        }
        return  [[NSArray alloc] initWithArray:modelArr];
    }else{
        return [[NSArray alloc]init];
    }

    return shopCartArr;
}

//修改商品个数
- (void) changeNumberShopcart:(KWMShopCartModel *)shopCartModel{
    KWMShopCartItem *result;
    NSPredicate *filterById = [NSPredicate predicateWithFormat:@"identifier == %@", shopCartModel.identifier];
    NSArray *existsArray = [KWMShopCartItem MR_findAllWithPredicate:filterById];
    if(existsArray && existsArray.count > 0){
        result = existsArray.firstObject;
        result.quantity = shopCartModel.quantity;
    }
    [self saveAll];
}
//商品转购物车
- (KWMShopCartItem *)toShopCartProduct:(KWMShopCartModel *)shopCartModel {
    
    KWMShopCartItem *result;
    NSPredicate *filterById = [NSPredicate predicateWithFormat:@"identifier == %@", shopCartModel.identifier];
    NSArray *existsArray = [KWMShopCartItem MR_findAllWithPredicate:filterById];
    
    if(existsArray && existsArray.count > 0){
        result = existsArray.firstObject;
        result.quantity += shopCartModel.quantity;
    }else{
        result = [KWMShopCartItem MR_createEntity];
        result.identifier = shopCartModel.identifier;
        result.imageStr = shopCartModel.imageStr;
        result.name = shopCartModel.name;
        result.brand = shopCartModel.brand;
        result.size = shopCartModel.size;
        result.quantity = shopCartModel.quantity;
        result.price = shopCartModel.price;
        result.line_price = shopCartModel.line_price;
        result.product_id = shopCartModel.product_id.stringValue;
        result.shopCartDict = shopCartModel.shopCartDict;
    }
    return result;
}

//购物车转商品
- (KWMShopCartModel *)toProduct:(KWMShopCartItem *)shopCartItem {
    
    KWMShopCartModel *result = [[KWMShopCartModel alloc]init];
    result.identifier = shopCartItem.identifier;
    result.name = shopCartItem.name;
    result.imageStr = shopCartItem.imageStr;
    result.price = shopCartItem.price;
    result.brand = shopCartItem.brand;
    result.size = shopCartItem.size;
    result.quantity = shopCartItem.quantity;
    result.line_price = shopCartItem.line_price;
    result.shopCartDict = shopCartItem.shopCartDict;
    result.product_id = @(shopCartItem.product_id.integerValue);
    return result;
}

@end