//
//  GMCache.m
//  Gengmei
//
//  Created by Thierry on 1/5/15.
//  Copyright (c) 2015 Wanmeichuangyi. All rights reserved.
//

#import "GMCache.h"
@interface WMDocumentCache : TMCache

@end
@implementation GMCache
+ (void)storeObjectAtDiskWithkey:(NSString *)key object:(id <NSCoding>)object block:(TMDiskCacheObjectBlock)block{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[TMCache sharedCache].diskCache setObject:object forKey:key block:block];
}

+ (void)fetchObjectAtDiskWithkey:(NSString *)key block:(TMDiskCacheObjectBlock)block{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[TMCache sharedCache].diskCache objectForKey:key block:block];
}

+ (void)removeObjectAtDiskWithkey:(NSString *)key block:(TMDiskCacheObjectBlock)block{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[TMCache sharedCache].diskCache removeObjectForKey:key block:block];
}

+ (void)removeAllObjectsAtDiskWithBlock:(TMDiskCacheBlock)block{
    [[TMCache sharedCache].diskCache removeAllObjects:block];
}

+ (void)storeObjectAtDiskWithkey:(NSString *)key object:(id <NSCoding>)object{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[TMCache sharedCache].diskCache setObject:object forKey:key];
}

+ (id)fetchObjectAtDiskWithkey:(NSString *)key{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return nil;
    }
    return  [[TMCache sharedCache].diskCache objectForKey:key];
}

+ (void)removeObjectAtDiskWithkey:(NSString *)key{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[TMCache sharedCache].diskCache removeObjectForKey:key];
}

+ (void)removeAllObjectsAtDisk{
    [[TMCache sharedCache].diskCache removeAllObjects];
}

+ (void)storeObjectAtMemoryWithkey:(NSString *)key object:(id <NSCoding>)object block:(TMMemoryCacheObjectBlock)block{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[TMCache sharedCache].memoryCache setObject:object forKey:key block:block];
}

+ (void)fetchObjectAtMemoryWithkey:(NSString *)key block:(TMMemoryCacheObjectBlock)block{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[TMCache sharedCache].memoryCache objectForKey:key block:block];
}

+ (void)removeObjectAtMemoryWithkey:(NSString *)key block:(TMMemoryCacheObjectBlock)block{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[TMCache sharedCache].memoryCache removeObjectForKey:key block:block];
}

+ (void)removeAllObjectsAtMemoryWithBlock:(TMMemoryCacheBlock)block{
    [[TMCache sharedCache].memoryCache removeAllObjects:block];
}

+ (void)storeObjectAtMemoryWithkey:(NSString *)key object:(id)object{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[TMCache sharedCache].memoryCache setObject:object forKey:key];
}

+ (id)fetchObjectAtMemoryWithkey:(NSString *)key{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return nil;
    }
    return  [[TMCache sharedCache].memoryCache objectForKey:key];
}

+ (void)removeObjectAtMemoryWithkey:(NSString *)key{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[TMCache sharedCache].memoryCache removeObjectForKey:key];
}

+ (void)removeAllObjectsAtMemory{
    [[TMCache sharedCache].memoryCache removeAllObjects];
}

+ (void)storeObjectAtDocumentPathWithkey:(NSString *)key
                                  object:(id <NSCoding>)object
                                   block:(TMDiskCacheObjectBlock)block{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[WMDocumentCache sharedCache].diskCache setObject:object forKey:key block:block];
}

+ (void)fetchObjectAtDocumentPathWithkey:(NSString *)key
                                   block:(TMDiskCacheObjectBlock)block{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[WMDocumentCache sharedCache].diskCache objectForKey:key block:block];
}

+ (void)removeObjectAtDocumentPathWithkey:(NSString *)key
                                    block:(TMDiskCacheObjectBlock)block{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[WMDocumentCache sharedCache].diskCache removeObjectForKey:key block:block];
}

+ (void)removeAllObjectsAtDocumentPathWithBlock:(TMDiskCacheBlock)block{
    [[WMDocumentCache sharedCache].diskCache removeAllObjects:block];
}

+ (void)storeObjectAtDocumentPathWithkey:(NSString *)key
                                  object:(id <NSCoding>)object{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[WMDocumentCache sharedCache].diskCache setObject:object forKey:key];
}

+ (id)fetchObjectAtDocumentPathWithkey:(NSString *)key{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return nil;
    }
    return [[WMDocumentCache sharedCache].diskCache objectForKey:key];
}

+ (void)removeObjectAtDocumentPathWithkey:(NSString *)key{
    NSAssert([GMCache gmCache_isNonEmpty:key], @"key不能为空");
    if (![GMCache gmCache_isNonEmpty:key]) {
        return;
    }
    [[WMDocumentCache sharedCache].diskCache removeObjectForKey:key];
}

+ (void)removeAllObjectsAtDocumentPath{
    [[WMDocumentCache sharedCache].diskCache removeAllObjects];
}

+ (BOOL)gmCache_isNonEmpty:(NSString *)key {
    
    NSMutableCharacterSet *emptyStringSet = [[NSMutableCharacterSet alloc] init];
    [emptyStringSet formUnionWithCharacterSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    [emptyStringSet formUnionWithCharacterSet: [NSCharacterSet characterSetWithCharactersInString: @"　"]];
    if ([key length] == 0) {
        return NO;
    }
    NSString* str = [key stringByTrimmingCharactersInSet:emptyStringSet];
    return [str length] > 0;
}

@end

NSString * const WMCacheSharedName = @"WMCacheShared";
@implementation WMDocumentCache
+ (instancetype)sharedCache{
    static id cache;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        cache = [[self alloc] initWithName:WMCacheSharedName rootPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
    });
    return cache;
}
@end
