//
//  CTDiskCacheCenter.m
//  Open
//
//  Created by 雷俊博 on 2019/8/26.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "CTDiskCacheCenter.h"

NSString * const kCTDiskCacheCenterCachedObjectKeyPrefix = @"kCTDiskCacheCenterCachedObjectKeyPrefix";

@implementation CTDiskCacheCenter

+ (id)fetchCachedRecordWithKey:(NSString *)key
{
     NSString *actualKey = [NSString stringWithFormat:@"%@%@",kCTDiskCacheCenterCachedObjectKeyPrefix, key];
    id response = nil;
    NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:actualKey];
    if (data) {
        NSDictionary *fetchedContent = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        NSNumber *lastUpdateTimeNumber = fetchedContent[@"lastUpdateTime"];
        NSDate *lastUpdateTime = [NSDate dateWithTimeIntervalSince1970:lastUpdateTimeNumber.doubleValue];
        NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:lastUpdateTime];
        if (timeInterval < [fetchedContent[@"cacheTime"] doubleValue]) {
            response = fetchedContent[@"content"];
        } else {
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:actualKey];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
    return response;
}

+ (void)saveCacheWithResponse:(id)response key:(NSString *)key cacheTime:(NSTimeInterval)cacheTime
{
    if (response) {
        NSData *data = [NSJSONSerialization dataWithJSONObject:@{
                                                                 @"content":response,
                                                                 @"lastUpdateTime":@([NSDate date].timeIntervalSince1970),
                                                                 @"cacheTime":@(cacheTime)
                                                                 }
                                                       options:0
                                                         error:NULL];
        if (data) {
            NSString *actualKey = [NSString stringWithFormat:@"%@%@",kCTDiskCacheCenterCachedObjectKeyPrefix, key];
            [[NSUserDefaults standardUserDefaults] setObject:data forKey:actualKey];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
}

+ (void)cleanAll
{
    NSDictionary *defaultsDict = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
    NSArray *keys = [[defaultsDict allKeys] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", kCTDiskCacheCenterCachedObjectKeyPrefix]];
    for(NSString *key in keys) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
