/// <summary>
        /// next -> initiate client play of next song in queue; update database if client is leader
        /// </summary>
        /// <param name="id">id -> current song id (for db)</param>
        public void next(int id)
        {
            OneMusicDBEntities db = new OneMusicDBEntities();

            Song current = db.Songs.FirstOrDefault(s => s.status == 1);
            Song next = new Song();

            if (current.id == id)
            {
                next = db.Songs.FirstOrDefault(s => s.status == 0); // && s.channel == current.channel);

                if (next == null)
                    next = db.Songs.FirstOrDefault(s => s.id > id);

                if (next == null)
                    next = db.Songs.FirstOrDefault();

                current.status = 2;
                current.position = null;
                next.status = 1;

                db.SaveChanges();
            }
            else
            {
                next = current;
            }

            Media output = new Media(next);
            output.Position = 0;

            Clients.Caller.play(output);
        }
        public ActionResult GetNext(int id, string channel = "one")
        {
            OneMusicDBEntities db = new OneMusicDBEntities();
            Song song = db.Songs.FirstOrDefault(s => s.status == 0);

            if (song == null)
                song = db.Songs.FirstOrDefault(s => s.id > id);

            if (song == null)
                song = db.Songs.FirstOrDefault();

            return Json(new Media(song), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Add(Media model)
        {
            if(User.Identity.IsAuthenticated) {
                OneMusicDBEntities db = new OneMusicDBEntities();
                Song n = new Song();
                n.track_id = model.Track_Id;
                n.track_title = model.Track_Title;
                n.status = 0;
                n.url = model.Url;
                n.date_added = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
                n.submitted_by = db.Users.FirstOrDefault(u => u.user_name == User.Identity.Name).user_id;

                db.Songs.Add(n);
                db.SaveChanges();
            }

            return RedirectToAction("Play");
        }
        /// <summary>
        /// broadcast -> sync media (or get current if only connected user) and initiate play on given client
        /// </summary>
        /// <param name="media">media -> updated Media object</param>
        /// <param name="id">id -> clientId of user who sent request</param>
        public void broadcast(Media media, string id)
        {
            if (media.Track_Id != null) {
                media = Sync(media);
            } else {
                OneMusicDBEntities db = new OneMusicDBEntities();
                Song current = db.Songs.FirstOrDefault(s => s.status == 1);

                if (current == null)
                    current = db.Songs.FirstOrDefault(s => s.status == 0);

                if (current == null)
                    current = db.Songs.FirstOrDefault();

                current.status = 1;
                db.SaveChanges();

                media = new Media(current);

                Thread.Sleep(2000); //wait 2s to allow for other users to contact first
            }

            Clients.Client(id).play(media);
        }
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (OneMusicDBEntities db = new OneMusicDBEntities())
                {
                    User user = db.Users.FirstOrDefault(u => u.user_name.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.Users.Add(new User { user_name = model.UserName, user_email = model.Email });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        public Media Sync(Media m = null)
        {
            OneMusicDBEntities db = new OneMusicDBEntities();
            Song current = db.Songs.FirstOrDefault(s => s.status == 1);

            if (m != null)
            {
                //if (m.Id > current.id)
                //{
                //    current.current = false;
                //    db.Songs.FirstOrDefault(s => s.id == m.Id).current = true;
                //}
                //else
                //{
                if (m.Id == current.id && (!current.position.HasValue || m.Position > current.position))
                    {
                        current.position = m.Position;
                        db.SaveChanges();
                    }
                //}
            }

            Media media = new Media(current);
            media.ReturnTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")).Second;

            return media;
        }