public IHttpActionResult AddShareathon(ShareathonViewModel shareathon)
        {
            Shareathon _shreathon = new Shareathon();
            string id = "";
            for (int i = 0; i < shareathon.FacebookPageId.Length; i++)
            {
                string dataid = shareathon.FacebookPageId[i];
                id = dataid + "," + id;
            }
            _shreathon.Facebookpageid = id.TrimEnd(',');
            _shreathon.Facebookaccountid = shareathon.Facebookaccountid;
            _shreathon.Timeintervalminutes = shareathon.Timeintervalminutes;
            _shreathon.Id = Guid.NewGuid();
            _shreathon.Userid = shareathon.Userid;
            if (!sharepo.IsShareathonExist(shareathon.Userid, shareathon.Facebookaccountid, _shreathon.Facebookpageid))
            {
                if (sharepo.AddShareathon(_shreathon))
                {
                    return Ok();
                }
                else
                {
                    return BadRequest();
                }
            }
            else
            {
                return BadRequest("Shareathon exist");
            }


        }
        public IHttpActionResult EditShareathon(ShareathonViewModel sharethon)
        {
            Shareathon eidtShareathon = sharepo.getShareathon(sharethon.Id);
            FacebookAccount facebookAccount = sharepo.getFbAccount(sharethon.Facebookaccountid);
            eidtShareathon.Facebookaccountid = facebookAccount.Id;
            string id = "";
            for (int i = 0; i < sharethon.FacebookPageId.Length; i++)
            {
                string dataid = sharethon.FacebookPageId[i];
                id = dataid + "," + id;
            }
            eidtShareathon.Facebookpageid = id.TrimEnd(',');
            eidtShareathon.Timeintervalminutes = sharethon.Timeintervalminutes;

            if (sharepo.updateShareathon(eidtShareathon))
            {
                return Ok();
            }
            else
            {
                return BadRequest();
            }
        }
        public IHttpActionResult GetGroupShareathons(string UserId)
        {
            Guid userId = Guid.Empty;
            try
            {
                userId = Guid.Parse(UserId);
            }
            catch
            {
                return BadRequest();
            }
            if (userId == Guid.Empty)
            {
                return BadRequest();
            }
            else
            {
                List<ShareathonGroup> shareatons = sharegrprepo.getUserShareathon(userId);
                List<ShareathonViewModel> shareathonviewModels = new List<ShareathonViewModel>();
                foreach (var item in shareatons)
                {
                    ShareathonViewModel svmodel = new ShareathonViewModel();
                    svmodel.Id = item.Id;
                    svmodel.IsHidden = item.IsHidden;
                    svmodel.Lastpostid = item.Lastpostid;
                    svmodel.Lastsharetimestamp = item.Lastsharetimestamp;
                    svmodel.Timeintervalminutes = item.Timeintervalminutes;
                    svmodel.Userid = item.Userid;
                    svmodel.Facebookaccount = sharepo.getFbAccount(item.Facebookaccountid);
                    shareathonviewModels.Add(svmodel);
                }
                return Ok(shareathonviewModels);
            }

        }
        public IHttpActionResult GetShareathons(string UserId)
        {
            Guid userId = Guid.Empty;
            try
            {
                userId = Guid.Parse(UserId);
            }
            catch
            {
                return BadRequest();
            }
            if (userId == Guid.Empty)
            {
                return BadRequest();
            }
            else
            {
                List<Shareathon> shareatons = sharepo.getUserShareathon(userId);
                List<ShareathonViewModel> shareathonviewModels = new List<ShareathonViewModel>();
                foreach (var item in shareatons)
                {
                    ShareathonViewModel svmodel = new ShareathonViewModel();
                    svmodel.Id = item.Id;
                    svmodel.IsHidden = item.IsHidden;
                    svmodel.Lastpostid = item.Lastpostid;
                    svmodel.Lastsharetimestamp = item.Lastsharetimestamp;
                    svmodel.Timeintervalminutes = item.Timeintervalminutes;
                    svmodel.Userid = item.Userid;
                    svmodel.Facebookaccount = sharepo.getFbAccount(item.Facebookaccountid);
                    List<FacebookAccount> Facebookpages = new List<FacebookAccount>();
                    try {
                        string[] fbids = item.Facebookpageid.Split(',');
                        foreach (var id in fbids)
                        {
                            try 
                            {
                                Facebookpages.Add(sharepo.getFbAccount(Guid.Parse(id)));
                            }
                            catch { }
                        }
                    }
                    catch { }
                    svmodel.Facebookpages = Facebookpages;
                    svmodel.pageid = item.Facebookpageid;
                    shareathonviewModels.Add(svmodel);
                }
                return Ok(shareathonviewModels);
            }

        }
        public async Task<ActionResult> Edit(ShareathonViewModel shareathon)
        {

            string id = "";
            for (int i = 0; i < shareathon.FacebookPageId.Length; i++)
            {
                string dataid = shareathon.FacebookPageId[i];
                id = dataid + "," + id;
            }
            User objUser = (User)Session["User"];
            List<KeyValuePair<string, string>> Parameters = new List<KeyValuePair<string, string>>();
            Parameters.Add(new KeyValuePair<string, string>("Id", shareathon.Id.ToString()));
            Parameters.Add(new KeyValuePair<string, string>("Facebookaccountid", shareathon.Facebookaccountid.ToString()));
            Parameters.Add(new KeyValuePair<string, string>("Userid", objUser.Id.ToString()));
            Parameters.Add(new KeyValuePair<string, string>("Facebookpageid", id.TrimEnd(',')));
            Parameters.Add(new KeyValuePair<string, string>("Timeintervalminutes", shareathon.Timeintervalminutes.ToString()));
            string accesstoken = string.Empty;
            string UserId = objUser.Id.ToString();
            if (Session["access_token"] != null)
            {
                accesstoken = Session["access_token"].ToString();
            }

            HttpResponseMessage response = await WebApiReq.PostReq("api/ApiShareathon/EditShareathon", Parameters, "Bearer", accesstoken);
            if (response.IsSuccessStatusCode)
            {
                return RedirectToAction("Index");
            }
            List<FacebookAccount> lstFacebookAccount = new List<FacebookAccount>();
            Api.FacebookAccount.FacebookAccount ApiobjFacebookAccount = new Api.FacebookAccount.FacebookAccount();
            List<FacebookAccount> facebookaccounts = (List<FacebookAccount>)(new JavaScriptSerializer().Deserialize(ApiobjFacebookAccount.GetAllFacebookAccountsByUserIdAndGroupId(objUser.Id.ToString(), System.Web.HttpContext.Current.Session["group"].ToString()), typeof(List<FacebookAccount>)));
            List<FacebookAccount> facebookpages = (List<FacebookAccount>)(new JavaScriptSerializer().Deserialize(ApiobjFacebookAccount.GetAllFacebookPageByUserIdAndGroupId(objUser.Id.ToString(), System.Web.HttpContext.Current.Session["group"].ToString()), typeof(List<FacebookAccount>)));

            foreach (FacebookAccount item in facebookpages)
            {
                if (item != null)
                {
                    lstFacebookAccount.Add(item);
                }
            }
            if (lstFacebookAccount.Count > 0)
            {
                ViewBag.FbPages = lstFacebookAccount;
                Session["FbPages"] = ViewBag.FbPages;
            }
            else
            {
                ViewBag.FbPages = null;
            }
            ViewBag.FbAccounts = facebookaccounts.Where(t => t.FbUserId != "").ToList();
            ViewBag.UserId = objUser.Id;
            return View();
        }
        public IHttpActionResult GetShareathons(string UserId)
        {
            Guid userId = Guid.Empty;
            try
            {
                userId = Guid.Parse(UserId);
            }
            catch
            {
                return BadRequest();
            }
            if (userId == Guid.Empty)
            {
                return BadRequest();
            }
            else
            {
                List<Shareathon> shareatons = sharepo.getUserShareathon(userId);
                List<ShareathonViewModel> shareathonviewModels = new List<ShareathonViewModel>();
                foreach (var item in shareatons)
                {
                    ShareathonViewModel svmodel = new ShareathonViewModel();
                    svmodel.Id = item.Id;
                    svmodel.IsHidden = item.IsHidden;
                    svmodel.Lastpostid = item.Lastpostid;
                    svmodel.Lastsharetimestamp = item.Lastsharetimestamp;
                    svmodel.Timeintervalminutes = item.Timeintervalminutes;
                    svmodel.Userid = item.Userid;
                    svmodel.Facebookaccount = sharepo.getFacebookAccountDetailsByUserProfileId(item.Facebookaccountid, item.Userid);
                    List<Domain.Socioboard.Domain.FacebookAccount> Facebookpages = new List<Domain.Socioboard.Domain.FacebookAccount>();
                    try
                    {
                        string[] fbids = item.Facebookpageid.Split(',');
                        foreach (var id in fbids)
                        {
                            try
                            {
                                Domain.Socioboard.Domain.FacebookAccount fbaccount = sharepo.getFacebookAccountDetailsByUserProfileId(id, item.Userid);
                                if (fbaccount != null)
                                {
                                    Facebookpages.Add(fbaccount);
                                }
                               
                            }
                            catch { }
                        }
                    }
                    catch { }
                    svmodel.Facebookpages = Facebookpages.Where(t=>t.FbUserId!="").ToList();
                    svmodel.pageid = item.Facebookpageid;
                    shareathonviewModels.Add(svmodel);
                }
                return Ok(shareathonviewModels);
            }

        }