//
//  ManagerUserDefault.swift
//  SocketChat
//
//  Created by Justine on 16/5/24.
//  Copyright © 2016年 AppCoda. All rights reserved.
//

import UIKit
class ManagerUserDefault: BaseOBJ {
    //获取唯一标识  appToken
//    NSString *uuid = [[NSUUID UUID] UUIDString];
//    [userDefaults setObject:uuid forKey:USERDEFAULT_APP_TOKEN];
//    [_requestBody setObject:uuid forKey:@"deviceToken"];
    var uuid = NSUUID().UUIDString
    
    func SetServers(dic:NSDictionary){
        InsertUserDefault(dic["serverID"] as! String,key: SERVER_ID)
        InsertUserDefault(dic["appServer"] as! String,key: APP_SERVER)
        InsertUserDefault(dic["fileServer"] as! String,key: FILE_UP_SERVER)
        InsertUserDefault(dic["downServer"] as! String,key: FILE_DOWN_SERVER)
        InsertUserDefault(dic["chatServer"] as! String,key: CHAT_SERVER)
        InsertUserDefault(dic["updateUrl"] as! String,key: UPDATE_SERVER)
        
    }
    func DelServers(){
        defer{
            userDefaults.synchronize()
        }
        DeleteUserDefault(SERVER_ID)
        DeleteUserDefault(APP_SERVER)
        DeleteUserDefault(FILE_UP_SERVER)
        DeleteUserDefault(FILE_DOWN_SERVER)
        DeleteUserDefault(CHAT_SERVER)
        DeleteUserDefault(UPDATE_SERVER)
        
        
    }
    //增
    func InsertUserDefault(obj:AnyObject,key:String){
        defer{
            userDefaults.synchronize()
        }
        userDefaults.setObject(obj, forKey: key)
    }
    func InsertUserDefault(dic:NSDictionary){
        
        for index in dic{
            print("##########################")
            print(index)
                //userDefaults.setObject(dic[0], forKey: APP_SERVER)
        }
        
    }
    //删
    private func DeleteUserDefault(obj:String){
        //手动更新数据
        defer{
            userDefaults.synchronize()
        }
        userDefaults.removeObjectForKey(obj)
    }
    //查
    func SelectUserDefault(obj:String)->AnyObject?{
        let result = userDefaults.objectForKey(obj)
        if result != nil {
            print("### find  \(obj)有值值为\(result)  ####")
            return result!
        }
        else{
            print("### find   \(obj)==nil,return 0   ####")
            return 0
        }
    }
    
}
