 import Foundation
 import AVFoundation
 
+struct k {
+	static let play = "play"
+	static let item = "item"
+	static let name = "name"
+	static let list = "list"
+	static let link = "link"
+	static let rank = "rank"
+}
+
 class PlayItem : NSObject {
-	var name : String = "item"
+	var name : String = k.item
 	var link : NSURL = NSURL.init(string: "http://")!
 	var rank = 0
 	
 	override init() {
-		name = "item"
+		name = k.item
 		link = NSURL.init(string: "http://")!
 		super.init()
 	}

						let track0 = AVURLAsset(URL:fileURL!, options:nil).tracks[0]
						if track0.mediaType != AVMediaTypeVideo
						{
							print("validate nonAV -> .None")
							return .None
						}
						
						let path = fileURL!.absoluteString.stringByRemovingPercentEncoding
						print("validate file \(path)")
					} else {
						print("validate item -> \(item)")
					}
				}
			}
			print("validate Above -> .Move")
			return .Move
		}
		print("validate other -> .None")
		return .None
	}
	

		}
	}
	
	@IBOutlet weak var restoreButton: NSButton!
	@IBAction func restorePlaylists(sender: NSButton) {
		if let playArray = defaults.arrayForKey(UserSetting.Playlists.userDefaultsKey) {
			playlistArrayController.removeObjects(playlistArrayController.arrangedObjects as! [AnyObject])
//			self.playlists = [PlayList]()

			for playlist in playArray {
				let play = playlist as! Dictionary<String,AnyObject>
				let items = play[k.list] as! [Dictionary <String,AnyObject>]
				var list : [PlayItem] = [PlayItem]()
				for playitem in items {
					let item = playitem as Dictionary <String,AnyObject>
					let name = item[k.name] as! String
					let path = item[k.link] as! String
					let link = NSURL.init(string: path)
					let rank = item[k.rank] as! Int
					let temp = PlayItem(name:name, link:link!, rank:rank)
					list.append(temp)
				}
				let name = play[k.name] as! String
				let temp = PlayList(name: name, list: list)
				playlistArrayController.addObject(temp)
			}
			dispatch_async(dispatch_get_main_queue()) {
				self.playlistTableView.reloadData()
			}
		}
	}

	@IBOutlet weak var saveButton: NSButton!
	@IBAction func savePlaylists(sender: AnyObject) {
		let playArray = playlistArrayController.arrangedObjects as! [PlayList]
		var temp = Array<AnyObject>()
		for playlist in playArray {
			var list = Array<AnyObject>()
			for playitem in playlist.list {
				let item : [String:AnyObject] = [k.name:playitem.name, k.link:playitem.link.absoluteString, k.rank:playitem.rank]
				list.append(item)
			}
			temp.append([k.name:playlist.name, k.list:list])
		}
		defaults.setObject(temp, forKey: UserSetting.Playlists.userDefaultsKey)
		defaults.synchronize()
	}
	
