public string getFbToken()
        {
            Domain.Myfashion.Domain.FacebookAccount _FacebookAccount = objFacebookAccountRepository.getToken();
            string token = _FacebookAccount.AccessToken;

            return(token);
        }
        public string AddFacebookAccount(string FbUserId, string FbUserName, string AccessToken, string Friends, string EmailId, string Type, string ProfileUrl, string IsActive, string UserId, string GroupId)
        {
            try
            {
                objFacebook             = new Domain.Myfashion.Domain.FacebookAccount();
                objFacebook.Id          = Guid.NewGuid();
                objFacebook.FbUserId    = FbUserId;
                objFacebook.FbUserName  = FbUserName;
                objFacebook.AccessToken = AccessToken;
                objFacebook.Friends     = Convert.ToInt16(Friends);
                objFacebook.EmailId     = EmailId;
                objFacebook.Type        = Type;
                objFacebook.ProfileUrl  = ProfileUrl;
                objFacebook.IsActive    = Convert.ToInt16(IsActive);
                objFacebook.UserId      = Guid.Parse(UserId);
                objFacebookAccountRepository.addFacebookUser(objFacebook);



                return(new JavaScriptSerializer().Serialize("Added"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return("Something Went Wrong");
            }
        }
        public string AddFacebookAccount(string FbUserId, string FbUserName, string AccessToken, string Friends, string EmailId, string Type, string ProfileUrl, string IsActive, string UserId, string GroupId)
        {
            try
            {
                objFacebook = new Domain.Myfashion.Domain.FacebookAccount();
                objFacebook.Id = Guid.NewGuid();
                objFacebook.FbUserId = FbUserId;
                objFacebook.FbUserName = FbUserName;
                objFacebook.AccessToken = AccessToken;
                objFacebook.Friends = Convert.ToInt16(Friends);
                objFacebook.EmailId = EmailId;
                objFacebook.Type = Type;
                objFacebook.ProfileUrl = ProfileUrl;
                objFacebook.IsActive = Convert.ToInt16(IsActive);
                objFacebook.UserId = Guid.Parse(UserId);
                objFacebookAccountRepository.addFacebookUser(objFacebook);



                return new JavaScriptSerializer().Serialize("Added");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return "Something Went Wrong";
            }
        }
 public string getUserDetails(string FbUserId)
 {
     try
     {
         Domain.Myfashion.Domain.FacebookAccount objFacebookAccount = objFacebookAccountRepository.getUserDetails(FbUserId);
         return(new JavaScriptSerializer().Serialize(objFacebookAccount));
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.StackTrace);
         return("Something Went Wrong");
     }
 }
 public string getFacebookAccountDetailsById(string UserId, string ProfileId)
 {
     try
     {
         Domain.Myfashion.Domain.FacebookAccount objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
         if (objFacebookAccountRepository.checkFacebookUserExists(ProfileId, Guid.Parse(UserId)))
         {
             objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(ProfileId, Guid.Parse(UserId));
             return(new JavaScriptSerializer().Serialize(objFacebookAccount));
         }
         else
         {
             objFacebookAccount = objFacebookAccountRepository.getUserDetails(ProfileId);
             return(new JavaScriptSerializer().Serialize(objFacebookAccount));
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.StackTrace);
         return("Something Went Wrong");
     }
 }
 public string getFacebookAccountDetailsById(string UserId, string ProfileId)
 {
     try
     {
         Domain.Myfashion.Domain.FacebookAccount objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
         if (objFacebookAccountRepository.checkFacebookUserExists(ProfileId, Guid.Parse(UserId)))
         {
             objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(ProfileId, Guid.Parse(UserId));
             return new JavaScriptSerializer().Serialize(objFacebookAccount);
         }
         else
         {
             objFacebookAccount = objFacebookAccountRepository.getUserDetails(ProfileId);
             return new JavaScriptSerializer().Serialize(objFacebookAccount);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.StackTrace);
         return "Something Went Wrong";
     }
 }
        public ActionResult Facebook(string code)
        {
            string status = "";

            if (Session["fblogin"] != null)
            {
                if ((string)Session["fblogin"] == "fblogin")
                {
                    Session["fblogin"] = null;
                    if (String.IsNullOrEmpty(code))
                    {
                        return(RedirectToAction("Index", "Index"));
                    }
                    Domain.Myfashion.Domain.User objUser        = (Domain.Myfashion.Domain.User)Session["User"];
                    Domain.Myfashion.Domain.User checkuserexist = (Domain.Myfashion.Domain.User)Session["User"];
                    string facebookcode = code;
                    Api.Facebook.Facebook apiobjFacebook = new Api.Facebook.Facebook();
                    Api.User.User         ApiobjUser     = new Api.User.User();
                    string   fbloginreturn    = apiobjFacebook.FacebookLogin(code);
                    string[] arrfbloginreturn = Regex.Split(fbloginreturn, "_#_");

                    objUser = (Domain.Myfashion.Domain.User)(new JavaScriptSerializer().Deserialize(arrfbloginreturn[0], typeof(Domain.Myfashion.Domain.User)));
                    Session["AccesstokenFblogin"] = arrfbloginreturn[1];
                    Session["fblogin"]            = "******";

                    try
                    {
                        Response.Write("Facebook Returned email : " + objUser.EmailId);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        logger.Error(ex.StackTrace);
                        logger.Error(ex.Message);
                    }

                    try
                    {
                        checkuserexist = (Domain.Myfashion.Domain.User)(new JavaScriptSerializer().Deserialize(ApiobjUser.getUserInfoByEmail(objUser.EmailId.ToString()), typeof(Domain.Myfashion.Domain.User)));
                        string pid  = checkuserexist.PuId;
                        string puid = pid.Substring(36, pid.Length - 36);
                        SortedDictionary <string, string> strdic = new SortedDictionary <string, string>();
                        strdic.Add("username", checkuserexist.EmailId);
                        strdic.Add("password", puid);
                        string data = CustomHttpWebRequest.HttpWebRequest("GET", "myfashion/loginService.action", strdic);
                        data = data.Replace("func({", "{");
                        data = data.Replace("});", "}");
                        JObject jo = JObject.Parse(data);
                        status = Server.UrlDecode((string)jo["code"]);
                    }
                    catch (Exception e)
                    {
                        checkuserexist = null;
                    }
                    if (checkuserexist != null)
                    {
                        Session["User"] = checkuserexist;
                        int daysremaining = 0;

                        daysremaining = (checkuserexist.ExpiryDate.Date - DateTime.Now.Date).Days;
                        if (daysremaining > 0)
                        {
                            #region Count Used Accounts
                            try
                            {
                                Session["Paid_User"] = "******";
                                Api.SocialProfile.SocialProfile apiobjSocialProfile = new Api.SocialProfile.SocialProfile();
                                //Session["ProfileCount"] = Convert.ToInt32(apiobjSocialProfile.GetAllSocialProfilesOfUserCount(objUser.Id.ToString()).ToString());
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                            #endregion
                        }
                        else
                        {
                            Session["Paid_User"] = "******";
                        }

                        if (status == "101")
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Index"));
                        }
                    }
                    else
                    {
                        objUser.ActivationStatus = "1";
                        Session["User"]          = objUser;
                        return(RedirectToAction("SignUp", "Index"));
                    }
                }
                else if ((string)Session["fblogin"] == "page")
                {
                    Session["fblogin"] = null;
                    Api.Facebook.Facebook apiobjFacebook = new Api.Facebook.Facebook();
                    List <Domain.Myfashion.Domain.AddFacebookPage> lstAddFacebookPage = new List <Domain.Myfashion.Domain.AddFacebookPage>();
                    lstAddFacebookPage = (List <Domain.Myfashion.Domain.AddFacebookPage>)(new JavaScriptSerializer().Deserialize(apiobjFacebook.GetFacebookPages(code), typeof(List <Domain.Myfashion.Domain.AddFacebookPage>)));
                    Session["fbpage"]  = lstAddFacebookPage;
                    return(RedirectToAction("Index", "Home", new { hint = "fbpage" }));
                }
                else if ((string)Session["fblogin"] == "fbgroup")
                {
                    Session["fblogin"] = null;

                    Api.Facebook.Facebook apiobjFacebook = new Api.Facebook.Facebook();
                    List <Domain.Myfashion.Domain.AddFacebookGroup> lstAddFacebookGroup = new List <Domain.Myfashion.Domain.AddFacebookGroup>();
                    lstAddFacebookGroup = (List <Domain.Myfashion.Domain.AddFacebookGroup>)(new JavaScriptSerializer().Deserialize(apiobjFacebook.GetFacebookGroups(code), typeof(List <Domain.Myfashion.Domain.AddFacebookGroup>)));
                    Session["fbgrp"]    = lstAddFacebookGroup;
                    return(RedirectToAction("Index", "Home", new { hint = "fbgrp" }));
                }
            }
            else
            {
                Domain.Myfashion.Domain.User objUser = (Domain.Myfashion.Domain.User)Session["User"];
                string facebookcode = code;
                Api.Facebook.Facebook apiobjFacebook = new Api.Facebook.Facebook();

                apiobjFacebook.Timeout = 120 * 1000;

                //string AddfacebookAccount = apiobjFacebook.AddFacebookAccount(facebookcode, objUser.Id.ToString(), Session["group"].ToString());
                string AddfacebookAccount = "";
                Domain.Myfashion.Domain.FacebookAccount objfacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                try
                {
                    var res_addFacebook = apiobjFacebook.AddFacebookAccount(facebookcode, objUser.Id.ToString(), Session["group"].ToString());
                    AddfacebookAccount = res_addFacebook;
                    try
                    {
                        objfacebookAccount = (Domain.Myfashion.Domain.FacebookAccount) new JavaScriptSerializer().Deserialize(res_addFacebook, typeof(Domain.Myfashion.Domain.FacebookAccount));
                        AddfacebookAccount = objfacebookAccount.FbUserId;
                    }
                    catch (Exception)
                    {
                    }
                }
                catch (Exception)
                {
                    AddfacebookAccount = "issue_access_token";
                }

                if (AddfacebookAccount == "issue_access_token")
                {
                    Response.Redirect(Helper.SBUtils.GetFacebookRedirectLink());
                }
                else if (AddfacebookAccount == "Account already Exist !")
                {
                }
                else
                {
                    Session["SocialManagerInfo"] = AddfacebookAccount;

                    //To enable the Facebook Message Pop up
                    TempData["IsFacebookAccountAdded"] = 1;
                    TempData["FacebookAccount"]        = objfacebookAccount;
                }
            }
            return(RedirectToAction("SocialMedia", "Home"));
        }
        public bool AddFacebookFriendsGender(string ProfileId, string FacebookUserId)
        {
            Api.Myfashionmarketer.Models.FacebookAccount _FacebookAccount = new FacebookAccount();
            Domain.Myfashion.Domain.FacebookAccount      _facebookAccount = new Domain.Myfashion.Domain.FacebookAccount();

            _facebookAccount = (Domain.Myfashion.Domain.FacebookAccount)(new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(_FacebookAccount.getFacebookAccountDetailsById(ProfileId, FacebookUserId), typeof(Domain.Myfashion.Domain.FacebookAccount)));

            if (string.IsNullOrEmpty(_facebookAccount.AccessToken))
            {
                _facebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                Api.Myfashionmarketer.Models.FacebookAccountRepository _FacebookAccountRepository = new FacebookAccountRepository();

                System.Collections.ArrayList lstFacebookAccounts = _FacebookAccountRepository.getAllFacebookAccounts();

                Random _random = new Random();
                var    rnum    = _random.Next(0, lstFacebookAccounts.Count - 1);
                _facebookAccount = (Domain.Myfashion.Domain.FacebookAccount)lstFacebookAccounts[rnum];
            }
            int malecount   = 0;
            int femalecount = 0;

            Domain.Myfashion.Domain.FacebookStats objfbStats = new Domain.Myfashion.Domain.FacebookStats();
            FacebookStatsRepository objFBStatsRepo           = new FacebookStatsRepository();

            FacebookClient fb = new FacebookClient();

            fb.AccessToken = _facebookAccount.AccessToken;
            try
            {
                dynamic data = fb.Get("v2.0/me/friends?fields=gender");

                //dynamic data, dynamic profile, Guid userId


                foreach (var item in data["data"])
                {
                    try
                    {
                        if (item["gender"] == "male")
                        {
                            malecount++;
                        }
                        else if (item["gender"] == "female")
                        {
                            femalecount++;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                return(false);
            }
            objfbStats.EntryDate   = DateTime.Now;
            objfbStats.FbUserId    = _facebookAccount.FbUserId;//profile["id"].ToString();
            objfbStats.FemaleCount = femalecount;
            objfbStats.Id          = Guid.NewGuid();
            objfbStats.MaleCount   = malecount;
            objfbStats.UserId      = _facebookAccount.UserId;
            objfbStats.FanCount    = getfanCount(objfbStats, _facebookAccount.AccessToken);

            if (objFBStatsRepo.checkFacebookStatsExists(objfbStats.FbUserId.ToString(), objfbStats.UserId, objfbStats.FanCount, objfbStats.MaleCount, objfbStats.FemaleCount))
            {
                objFBStatsRepo.addFacebookStats(objfbStats);
            }


            return(true);
        }
        public ActionResult Facebook(string code)
        {
            string status="";
            if (Session["fblogin"] != null)
            {

                if ((string)Session["fblogin"] == "fblogin")
                {
                    Session["fblogin"] = null;
                    if (String.IsNullOrEmpty(code))
                    {
                        return RedirectToAction("Index", "Index");
                    }
                    Domain.Myfashion.Domain.User objUser = (Domain.Myfashion.Domain.User)Session["User"];
                    Domain.Myfashion.Domain.User checkuserexist = (Domain.Myfashion.Domain.User)Session["User"];
                    string facebookcode = code;
                    Api.Facebook.Facebook apiobjFacebook = new Api.Facebook.Facebook();
                    Api.User.User ApiobjUser = new Api.User.User();
                    string fbloginreturn = apiobjFacebook.FacebookLogin(code);
                    string[] arrfbloginreturn = Regex.Split(fbloginreturn,"_#_");

                    objUser = (Domain.Myfashion.Domain.User)(new JavaScriptSerializer().Deserialize(arrfbloginreturn[0], typeof(Domain.Myfashion.Domain.User)));
                    Session["AccesstokenFblogin"] = arrfbloginreturn[1];
                    Session["fblogin"] = "******";

                    try
                    {
                        Response.Write("Facebook Returned email : " + objUser.EmailId);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        logger.Error(ex.StackTrace);
                        logger.Error(ex.Message);
                    }

                    try
                    {
                        checkuserexist = (Domain.Myfashion.Domain.User)(new JavaScriptSerializer().Deserialize(ApiobjUser.getUserInfoByEmail(objUser.EmailId.ToString()), typeof(Domain.Myfashion.Domain.User)));
                        string pid = checkuserexist.PuId;
                        string puid = pid.Substring(36, pid.Length - 36);
                        SortedDictionary<string, string> strdic = new SortedDictionary<string, string>();
                        strdic.Add("username", checkuserexist.EmailId);
                        strdic.Add("password", puid);
                        string data = CustomHttpWebRequest.HttpWebRequest("GET", "myfashion/loginService.action", strdic);
                        data = data.Replace("func({", "{");
                        data = data.Replace("});", "}");
                        JObject jo = JObject.Parse(data);
                        status = Server.UrlDecode((string)jo["code"]);
                    }
                    catch (Exception e)
                    {
                        checkuserexist = null;
                    }
                    if (checkuserexist != null)
                    {
                        Session["User"] = checkuserexist;
                        int daysremaining = 0;

                        daysremaining = (checkuserexist.ExpiryDate.Date - DateTime.Now.Date).Days;
                        if (daysremaining > 0)
                        {
                            #region Count Used Accounts
                            try
                            {
                                Session["Paid_User"] = "******";
                                Api.SocialProfile.SocialProfile apiobjSocialProfile = new Api.SocialProfile.SocialProfile();
                                //Session["ProfileCount"] = Convert.ToInt32(apiobjSocialProfile.GetAllSocialProfilesOfUserCount(objUser.Id.ToString()).ToString());
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                            #endregion
                        }
                        else
                        {
                            Session["Paid_User"] = "******";
                        }

                        if (status == "101")
                        {
                            return RedirectToAction("Index", "Home");
                        }
                        else
                        {
                            return RedirectToAction("Index", "Index");
                        }
                    }
                    else
                    {
                        objUser.ActivationStatus = "1";
                        Session["User"] = objUser;
                        return RedirectToAction("SignUp", "Index");
                    }
                }
                else if ((string)Session["fblogin"] == "page")
                {
                    Session["fblogin"] = null;
                    Api.Facebook.Facebook apiobjFacebook = new Api.Facebook.Facebook();
                    List<Domain.Myfashion.Domain.AddFacebookPage> lstAddFacebookPage = new List<Domain.Myfashion.Domain.AddFacebookPage>();
                    lstAddFacebookPage = (List<Domain.Myfashion.Domain.AddFacebookPage>)(new JavaScriptSerializer().Deserialize(apiobjFacebook.GetFacebookPages(code), typeof(List<Domain.Myfashion.Domain.AddFacebookPage>)));
                    Session["fbpage"] = lstAddFacebookPage;
                    return RedirectToAction("Index", "Home", new { hint = "fbpage" });
                }
                else if ((string)Session["fblogin"] == "fbgroup")
                {
                    Session["fblogin"] = null;

                    Api.Facebook.Facebook apiobjFacebook = new Api.Facebook.Facebook();
                    List<Domain.Myfashion.Domain.AddFacebookGroup> lstAddFacebookGroup = new List<Domain.Myfashion.Domain.AddFacebookGroup>();
                    lstAddFacebookGroup = (List<Domain.Myfashion.Domain.AddFacebookGroup>)(new JavaScriptSerializer().Deserialize(apiobjFacebook.GetFacebookGroups(code), typeof(List<Domain.Myfashion.Domain.AddFacebookGroup>)));
                    Session["fbgrp"] = lstAddFacebookGroup;
                    return RedirectToAction("Index", "Home", new { hint = "fbgrp" });
                }
            }
            else
            {
                Domain.Myfashion.Domain.User objUser = (Domain.Myfashion.Domain.User)Session["User"];
                string facebookcode = code;
                Api.Facebook.Facebook apiobjFacebook = new Api.Facebook.Facebook();

                apiobjFacebook.Timeout = 120 * 1000;

                //string AddfacebookAccount = apiobjFacebook.AddFacebookAccount(facebookcode, objUser.Id.ToString(), Session["group"].ToString());
                string AddfacebookAccount = "";
                Domain.Myfashion.Domain.FacebookAccount objfacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                try
                {
                    var res_addFacebook = apiobjFacebook.AddFacebookAccount(facebookcode, objUser.Id.ToString(), Session["group"].ToString());
                    AddfacebookAccount = res_addFacebook;
                    try
                    {
                        objfacebookAccount = (Domain.Myfashion.Domain.FacebookAccount)new JavaScriptSerializer().Deserialize(res_addFacebook, typeof(Domain.Myfashion.Domain.FacebookAccount));
                        AddfacebookAccount = objfacebookAccount.FbUserId;
                    }
                    catch (Exception)
                    {
                        
                    }
                    
                }
                catch (Exception)
                {
                    AddfacebookAccount = "issue_access_token";
                }

                if (AddfacebookAccount == "issue_access_token")
                {
                    Response.Redirect(Helper.SBUtils.GetFacebookRedirectLink());
                }
                else if (AddfacebookAccount == "Account already Exist !")
                {
                }
                else
                {
                    Session["SocialManagerInfo"] = AddfacebookAccount;

                    //To enable the Facebook Message Pop up
                    TempData["IsFacebookAccountAdded"] = 1;
                    TempData["FacebookAccount"] = objfacebookAccount;
                }
            }
            return RedirectToAction("SocialMedia", "Home");

        }
        public bool AddFacebookFriendsGender(string ProfileId, string FacebookUserId)
        {
            Api.Myfashionmarketer.Models.FacebookAccount _FacebookAccount = new FacebookAccount();
            Domain.Myfashion.Domain.FacebookAccount _facebookAccount = new Domain.Myfashion.Domain.FacebookAccount();

            _facebookAccount = (Domain.Myfashion.Domain.FacebookAccount)(new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(_FacebookAccount.getFacebookAccountDetailsById(ProfileId, FacebookUserId), typeof(Domain.Myfashion.Domain.FacebookAccount)));

            if (string.IsNullOrEmpty(_facebookAccount.AccessToken))
            {
                _facebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                Api.Myfashionmarketer.Models.FacebookAccountRepository _FacebookAccountRepository = new FacebookAccountRepository();

                System.Collections.ArrayList lstFacebookAccounts = _FacebookAccountRepository.getAllFacebookAccounts();

                Random _random = new Random();
                var rnum = _random.Next(0, lstFacebookAccounts.Count - 1);
                _facebookAccount = (Domain.Myfashion.Domain.FacebookAccount)lstFacebookAccounts[rnum];
            }
            int malecount = 0;
            int femalecount = 0;

            Domain.Myfashion.Domain.FacebookStats objfbStats = new Domain.Myfashion.Domain.FacebookStats();
            FacebookStatsRepository objFBStatsRepo = new FacebookStatsRepository();

            FacebookClient fb = new FacebookClient(); 
            fb.AccessToken = _facebookAccount.AccessToken;
            try
            {
                dynamic data = fb.Get("v2.0/me/friends?fields=gender");

                //dynamic data, dynamic profile, Guid userId


                foreach (var item in data["data"])
                {

                    try
                    {
                        if (item["gender"] == "male")
                            malecount++;
                        else if (item["gender"] == "female")
                            femalecount++;
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                return false;
            }
            objfbStats.EntryDate = DateTime.Now;
            objfbStats.FbUserId = _facebookAccount.FbUserId;//profile["id"].ToString();
            objfbStats.FemaleCount = femalecount;
            objfbStats.Id = Guid.NewGuid();
            objfbStats.MaleCount = malecount;
            objfbStats.UserId = _facebookAccount.UserId;
            objfbStats.FanCount = getfanCount(objfbStats, _facebookAccount.AccessToken);

            if (objFBStatsRepo.checkFacebookStatsExists(objfbStats.FbUserId.ToString(), objfbStats.UserId, objfbStats.FanCount, objfbStats.MaleCount, objfbStats.FemaleCount))
            {
                objFBStatsRepo.addFacebookStats(objfbStats);
            }


            return true;
        }