//
//  APPIdentificationManage.m
//  ufind1
//
//  Created by GUANG YU ZHU on 3/26/16.
//
//

#import "APPIdentificationManage.h"
#import <AdSupport/AdSupport.h>
#import <Security/Security.h>

#define KEY_UDID            @"com.shangyao.MobilePortalPrd_UDID"
#define KEY_IN_KEYCHAIN     @"com.shangyao.MobilePortalPrd_IN_KEYCHAIN"

//#define KEY_HTTP_UDID            @"KEY_HTTP_UDID"
//#define KEY_IN_HTTP_KEYCHAIN     @"KEY_IN_HTTP_KEYCHAIN"

@implementation APPIdentificationManage
{
    NSString *_uuid;
    NSString *_httpUUID;
}

#pragma mark 读取UUID
//- (NSString *)readHttpUUID:(NSString *)httpUUID
//{
//    if (_httpUUID == nil || _httpUUID.length == 0) {
//        NSMutableDictionary *udidKVPairs = (NSMutableDictionary *)[APPIdentificationManage load:KEY_IN_HTTP_KEYCHAIN];
//        NSString *uuid = [udidKVPairs objectForKey:KEY_HTTP_UDID];
//        if (uuid == nil || uuid.length == 0) {
//            [self saveHTTPUDID:httpUUID];
//        }
//        _uuid = uuid;
//    }
//    return _uuid;
//}
#pragma mark 保存UUID到钥匙串
//- (void)saveHTTPUDID:(NSString *)udid
//{
//    NSMutableDictionary *udidKVPairs = [NSMutableDictionary dictionary];
//    [udidKVPairs setObject:udid forKey:KEY_HTTP_UDID];
//    [APPIdentificationManage save:KEY_IN_HTTP_KEYCHAIN data:udidKVPairs];
//}
/**
 *先从内存中获取uuid，如果没有再从钥匙串中获取，如果还没有就生成一个新的uuid，并保存到钥匙串中供以后使用
 **/

- (NSString *)readUDID
{
    if (_uuid == nil || _uuid.length == 0) {
        NSMutableDictionary *udidKVPairs = (NSMutableDictionary *)[APPIdentificationManage load:KEY_IN_KEYCHAIN];
        NSString *uuid = [udidKVPairs objectForKey:KEY_UDID];
        if (uuid == nil || uuid.length == 0) {
            uuid = [self openUDID];
            [self saveUDID:uuid];
        }
//        NSLog(@"%@",[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]);

        _uuid = uuid;
    }
    return _uuid;
}
#pragma mark 获取UUID
- (NSString *)openUDID
{
    NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString];
    return identifierForVendor;
}
#pragma mark 保存UUID到钥匙串
- (void)saveUDID:(NSString *)udid
{
    NSMutableDictionary *udidKVPairs = [NSMutableDictionary dictionary];
    [udidKVPairs setObject:udid forKey:KEY_UDID];
    [APPIdentificationManage save:KEY_IN_KEYCHAIN data:udidKVPairs];
}

#pragma mark 查询钥匙串
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:
            (__bridge_transfer id)kSecClassGenericPassword,(__bridge_transfer id)kSecClass,
            service, (__bridge_transfer id)kSecAttrService,
            service, (__bridge_transfer id)kSecAttrAccount,
            (__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,(__bridge_transfer id)kSecAttrAccessible,
            nil];
}

#pragma mark 将数据保存到钥匙串
+ (void)save:(NSString *)service data:(id)data {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge_transfer id)kSecValueData];
    SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL);
}

#pragma mark 加载钥匙串中数据
+ (id)load:(NSString *)service {
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    
    [keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData];
    [keychainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit];
    CFDataRef keyData = NULL;
    if (SecItemCopyMatching((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
        @try {
            ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge_transfer NSData *)keyData];
        } @catch (NSException *e) {
            NSLog(@"Unarchive of %@ failed: %@", service, e);
        } @finally {
        }
    }
    return ret;
}

//#pragma mark 删除钥匙串中数据
//- (void)delete:(NSString *)service {
//    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//    SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
//}

@end
