         task.resume()
         
     }
+    
 }
/*
以降すべてAPI関連はこのAPIを通して行うようにする
*/
import Foundation
import SwiftyJSON
import KeychainAccess

class CaveAPI {
    private let ud = NSUserDefaults.standardUserDefaults()
    private let keychain = Keychain(service: "net.cavelis.gae")
    
    var devKey: String {
        get {
            return "116E0C9916D143B487B220DE68743EE4"
        }
    }
    
    private(set) var apiKey: String {
        get {
            return self.ud.stringForKey("auth_api")!
        }
        set(apiKey) {
            self.ud.setObject(apiKey, forKey: "auth_api")
            self.ud.synchronize()
        }
    }
    
    private(set) var auth_user: String {
        get {
            return self.ud.stringForKey("auth_user")!
        }
        set(auth_user) {
            self.ud.setObject(auth_user, forKey: "auth_user")
            self.ud.synchronize()
        }
    }
    
    private(set) var auth_pass: String {
        get {
            return try! keychain.getString("auth_pass")!
        }
        set(auth_pass) {
            try! keychain.set(auth_pass, key: "auth_pass")
        }
    }
    
    private(set) var cookieKey: [NSHTTPCookie]? {
        get {
            if let data = self.ud.objectForKey("HTTPCookieKey"){
                let cookie = NSKeyedUnarchiver.unarchiveObjectWithData(data as! NSData) as! [NSHTTPCookie]
                return cookie
            }
            return nil
        }
        set(cookieKey) {
            var data:NSData! = nil
            if cookieKey != nil{
                data = NSKeyedArchiver.archivedDataWithRootObject(cookieKey!)
            }
            self.ud.setObject(data, forKey: "HTTPCookieKey")
            self.ud.synchronize()
        }
    }
    
    private(set) var accessKey: String {
        get {
            return self.ud.stringForKey("access_key")!
        }
        set(accessKey) {
            self.ud.setObject(accessKey, forKey: "access_key")
            self.ud.synchronize()
        }
    }
    
    private(set) var favUser:[String]{
        get{
            return self.ud.arrayForKey("favUser") as! [String]
        }
        set(favUser){
            self.ud.setObject(favUser, forKey: "favUser")
            self.ud.synchronize()
        }
    }
    
    internal func searchUser(name name:String) -> Bool{
        let users = self.favUser
        
        if users.indexOf(name) != nil {
            return true
        }else{
                return false
        }
    }
    
    internal func addUser(name name:String) -> Void{
        var users = self.favUser
        
        if users.indexOf(name) == nil {
            users.append(name)
            self.favUser = users
        }

        return
    }
    
    internal func deleteUser(name name:String) -> Bool{
        var users = self.favUser
        let index = users.indexOf(name)
        
        if index != nil {
            users.removeAtIndex(index!)
            self.favUser = users
            
            return true
        }
        
        return false
    }
    
    func initData(){
        self.accessKey = ""

        self.auth_pass = ""
        self.cookieKey = nil
        self.favUser = []
        self.presets = []
    }
    
    var devKey: String {
        get {
            return "116E0C9916D143B487B220DE68743EE4"
        }
    }
    
    private(set) var apiKey: String {
        get {
            return self.ud.stringForKey("auth_api")!
        }
        set(apiKey) {
            self.ud.setObject(apiKey, forKey: "auth_api")
            self.ud.synchronize()
        }
    }
    
    private(set) var auth_user: String {
        get {
            return self.ud.stringForKey("auth_user")!
        }
        set(auth_user) {
            self.ud.setObject(auth_user, forKey: "auth_user")
            self.ud.synchronize()
        }
    }
    
    private(set) var auth_pass: String {
        get {
            return try! keychain.getString("auth_pass")!
        }
        set(auth_pass) {
            try! keychain.set(auth_pass, key: "auth_pass")
        }
    }
    
    private(set) var cookieKey: [NSHTTPCookie]? {
        get {
            if let data = self.ud.objectForKey("HTTPCookieKey"){
                let cookie = NSKeyedUnarchiver.unarchiveObjectWithData(data as! NSData) as! [NSHTTPCookie]
                return cookie
            }
            return nil
        }
        set(cookieKey) {
            var data:NSData! = nil
            if cookieKey != nil{
                data = NSKeyedArchiver.archivedDataWithRootObject(cookieKey!)
            }
            self.ud.setObject(data, forKey: "HTTPCookieKey")
            self.ud.synchronize()
        }
    }
    
    private(set) var accessKey: String {
        get {
            return self.ud.stringForKey("access_key")!
        }
        set(accessKey) {
            self.ud.setObject(accessKey, forKey: "access_key")
            self.ud.synchronize()
        }
    }
    
    
    //FavUser
    private(set) var favUser:[String]{
        get{
            return self.ud.arrayForKey("favUser") as! [String]
        }
        set(favUser){
            self.ud.setObject(favUser, forKey: "favUser")
            self.ud.synchronize()
        }
    }
    
    internal func searchUser(name name:String) -> Bool{
        let users = self.favUser
        
        if users.indexOf(name) != nil {
            return true
        }else{
            return false
        }
    }
    
    internal func addUser(name name:String) -> Void{
        var users = self.favUser
        
        if users.indexOf(name) == nil {
            users.append(name)
            self.favUser = users
            return
        }
        
        return
    }
    
    internal func deleteUser(name name:String) -> Void{
        var users = self.favUser
        let index = users.indexOf(name)
        
        if index != nil {
            users.removeAtIndex(index!)
            self.favUser = users
            
            return
        }
        
        return
    }
    
    
    //Presets
    private(set) var presets:[Preset]{
        get{
            let data = self.ud.objectForKey("Presets") as! NSData
            return  NSKeyedUnarchiver.unarchiveObjectWithData(data) as! [Preset]
        }
        set(presets){
            let data = NSKeyedArchiver.archivedDataWithRootObject(presets)
            self.ud.setObject(data, forKey: "Presets")
            self.ud.synchronize()
        }
    }
    
    internal func presetNames() -> [String]{
        var strArray:[String] = []
        
        for preset in self.presets{
           strArray.append(preset.PresetName)
        }
        
        return strArray
    }
    
    internal func loadPreset(presetName presetName:String) -> Preset{
        for preset in self.presets{
            print(preset.PresetName)
        }
        return self.presets[0]
    }
    
    internal func savePreset(preset preset:Preset) -> Bool{
        var data = self.ud.objectForKey("Presets") as! NSData
        var pre = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! [Preset]
        
        pre.append(preset)
        print(pre)
        
        data = NSKeyedArchiver.archivedDataWithRootObject(pre)
        
        self.ud.setObject(data, forKey: "Presets")
        self.ud.synchronize()
        return false
    }
    
    
    internal func Login(user user:String,pass:String,regist:(Bool)->Void){
        let str = "user_name=\(user)&password=\(pass)"
