 import AVFoundation
 import MediaPlayer
 import SocketIO
+
 class ViewController: UIViewController {
     
     @IBOutlet weak var toolbar: UIToolbar!

    func addHandlers() {
        // Our socket handlers go here
        
        self.socket.onAny {println("Got event: \($0.event), with items: \($0.items)")}
        self.socket.on("strims") {[weak self] data, ack in
            println("Got data: \(data), with ack: \(ack)")
            return
        }
        
        
    }
    
    

    
    let socket = SocketIOClient(socketURL: "http://api.overrustle.com", options: ["nsp": "/streams"])
    
    var api_data:NSDictionary = NSDictionary();
    
    @IBAction func strimsPressed(sender: AnyObject) {
        
        var title = "Select Strim"
        
        if let viewers = api_data["viewercount"] as? Int {
            println("Rustlers:", viewers)
            title = "\(viewers) Rustlers Watching"
        }
        
        var list = NSArray()
        
        if let stream_list = api_data["stream_list"] as? NSArray {
            list = stream_list
            println("Stream List", stream_list)
        }
        
        
        let rustleActionSheet = UIActionSheet(title: title, delegate: nil, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil)
        rustleActionSheet.actionSheetStyle = UIActionSheetStyle.BlackTranslucent
        
        var i : Int
        
        for i = 0; i<list.count; i++ {
            let stream = list[i] as! NSDictionary
            if let channel = stream["channel"] as? String, let platform = stream["platform"] as? String {
                var button_title = "\(channel) on \(platform)"
                if let name = stream["name"] as? String {
                    button_title = "\(name) via \(button_title)"
                }
                
                var button = UIButton()
                button.addTarget(self, action: "selected:", forControlEvents: UIControlEvents.TouchDown)
                var frame = CGRectMake(0, CGFloat(i*40 + 10), 160, 40)
                button.frame = frame
                button.tag = i
                button.setTitle("Button \(i)", forState: UIControlState.Normal)
                println("Making button \(i)")
                rustleActionSheet.addButtonWithTitle(button_title)
                rustleActionSheet.addSubview(button)
            }
        }

        rustleActionSheet.bounds = CGRectMake(0, 0, 320, 485)
        rustleActionSheet.showInView(self.view)
