protected void DisplayMenu()
    {
        ConnectionClass ConnObj = new ConnectionClass();
        SqlCommand cmd1 = new SqlCommand("sp_select_brand_userpanelpermission");
        cmd1.Parameters.AddWithValue("@user_id", SessionState._BrandAdmin.user_id);
        ConnObj.GetDataTab(cmd1);

        //CheckIfActivitiesWaitingVerification();
        PrintChildItems(ConnObj.DataTab);
        ConnObj.ReleaseConnection();
    }
    private string getAccessToken(Int64 page_id)
    {
        string token="";
        if (page_tokens.ContainsKey(page_id)) token = page_tokens[page_id];
        else
        {
            // get user access token
            SqlCommand cmd = new SqlCommand("sp_Get_brands_sm_token_by_page_id");
            cmd.Parameters.AddWithValue("@page_id", page_id);
            ConnectionClass ConnObj1 = new ConnectionClass();
            ConnObj1.GetDataSet(cmd);
            if (ConnObj1.IsSuccess && ConnObj1.DataSet.Tables.Count > 0 && ConnObj1.DataSet.Tables[0].Rows.Count > 0)
            {
                page_tokens.Add(page_id, Convert.ToString(ConnObj1.DataSet.Tables[0].Rows[0]["token"]));
                page_verifier.Add(page_id, Convert.ToString(ConnObj1.DataSet.Tables[0].Rows[0]["verifier"]));
                token = page_tokens[page_id];

            }
            ConnObj1.ReleaseConnection();
        }
        return token;
    }
    public void checkIfUserHasAddedAnyPost(string accessToken, List<fbuser> user, string hashtag, string defaulttext, String last_cron_run, Int32 brand_id, Int64 campaign_id, Int64 action_id,
                        decimal reward_amount, decimal reward_amount_max, decimal reward_percent, decimal reward_percent_max, string reward_product, decimal reward_point, Int32 no_of_friends,
                        Int32 no_of_likes, Int32 no_of_shares, decimal reward_per_user, decimal reward_on_friend,
                        decimal reward_on_likes, decimal reward_on_shares)
    {
        fbuser t = user[0];

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.instagram.com/v1/users/" + t.sm_uid + "/media/recent/?access_token=" + accessToken_1);
        request.Method = "GET";
        request.ContentType = "application/x-www-form-urlencoded";
        WebResponse response = request.GetResponse();
        string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
        var tmpResult = JObject.Parse(responseData);

        var resultObject = tmpResult["data"].Values<JObject>()
        .Where(n => n["caption"].Count() > 0)
        .Where(n => n["caption"]["text"].Value<string>().Contains("@ichooseit"))
        .Select(n => new { img = n["images"]["thumbnail"]["url"], link = n["link"], text = n["caption"]["text"], mediaID = n["id"], created_time = n["created_time"], likes = n["likes"]["count"], Context = n.Parent }).ToArray();

        //Loop through the returned users
        foreach (var i in resultObject)
        {

            Int32 brand_id1; Int64 campaign_id1; Int64 action_id1;

            string message = Convert.ToString( i.text ).ToLower();
            hashtag = hashtag.Replace("@" + System.Configuration.ConfigurationManager.AppSettings["InstaIchooseItHashtag"], "").ToLower();
            string[] hashtag_arr = hashtag.Split(' ');
            bool valid = true;
            foreach (string hash in hashtag_arr)
            {
                if (!message.Contains(hash.Trim()))
                {
                    valid = false;
                    break;
                }
            }
            if (valid == false)
            {
                //continue;
            }

            // case : default text defined for campaign
            if (defaulttext != "")
            {
                if (!message.Contains(defaulttext))
                {
                    valid = false;
                    //continue;
                }
            }

                ConnObj = new ConnectionClass();
                SqlCommand cmd = new SqlCommand("sp1_IChooseIT_User_Activities_Insert");

                // case : valid
                if (valid == true)
                {
                    #region for brand post
                    brand_id1 = brand_id; campaign_id1 = campaign_id; action_id1 = action_id;
                    cmd.Parameters.AddWithValue("@reg_uid", t.reg_uid);
                    cmd.Parameters.AddWithValue("@brand_id", brand_id);
                    cmd.Parameters.AddWithValue("@campaign_id", campaign_id);
                    cmd.Parameters.AddWithValue("@action_id", action_id);
                    cmd.Parameters.AddWithValue("@created_on", ConvertFromUnixTimestamp(Convert.ToDouble(i.created_time)));
                    cmd.Parameters.AddWithValue("@pid", Convert.ToString(i.mediaID));
                    decimal points = 0;
                    no_of_friends = 0;
                    no_of_likes = 0;
                    // reward table details
                    if (reward_per_user > 0)
                    {
                        points += reward_per_user;
                    }
                    if (reward_on_friend > 0)
                    {
                        // get number of friends for the user
                        no_of_friends = t.no_of_friends;
                        points += (no_of_friends * reward_on_friend);
                    }
                    if (reward_on_likes > 0)
                    {
                        if ( Convert.ToInt32( i.likes ) > 0 )
                        {
                            no_of_likes = Convert.ToInt32( i.likes );
                        }
                        points += (no_of_likes * reward_on_likes);
                    }
                    if (reward_point > 0)
                    {
                        reward_amount = (reward_amount / reward_point) * points;
                        reward_percent = (reward_percent / reward_point) * points;

                        reward_amount = (reward_amount > reward_amount_max) ? reward_amount_max : reward_amount;
                        reward_percent = (reward_percent > reward_percent_max) ? reward_percent_max : reward_percent;
                    }

                    cmd.Parameters.AddWithValue("@reward_amount", reward_amount);
                    cmd.Parameters.AddWithValue("@reward_percent", reward_percent);
                    cmd.Parameters.AddWithValue("@reward_product", reward_product);
                    cmd.Parameters.AddWithValue("@no_of_friends", no_of_friends);
                    cmd.Parameters.AddWithValue("@no_of_likes", no_of_likes);
                    cmd.Parameters.AddWithValue("@no_of_shares", no_of_shares);
                    cmd.Parameters.AddWithValue("@reward_per_user", reward_per_user);
                    cmd.Parameters.AddWithValue("@reward_on_friend", reward_on_friend);
                    cmd.Parameters.AddWithValue("@reward_on_likes", reward_on_likes);
                    cmd.Parameters.AddWithValue("@reward_on_shares", reward_on_shares);
                    cmd.Parameters.AddWithValue("@returnid", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                    ConnObj.GetDataTab(cmd);
                    #endregion
                }
                else
                {
                    brand_id1 = 2; campaign_id1 = 8; action_id1 = 27;
                    cmd.Parameters.AddWithValue("@reg_uid", t.reg_uid);
                    cmd.Parameters.AddWithValue("@brand_id", brand_id1);
                    cmd.Parameters.AddWithValue("@campaign_id", campaign_id1);
                    cmd.Parameters.AddWithValue("@action_id", action_id1);
                    cmd.Parameters.AddWithValue("@created_on", Convert.ToDateTime(ConvertFromUnixTimestamp(Convert.ToDouble(i.created_time))));//
                    cmd.Parameters.AddWithValue("@pid", Convert.ToString( i.mediaID ));
                    cmd.Parameters.AddWithValue("@reward_amount", 0);
                    cmd.Parameters.AddWithValue("@reward_percent", 0);
                    cmd.Parameters.AddWithValue("@reward_product", "");
                    cmd.Parameters.AddWithValue("@no_of_friends", 0);
                    cmd.Parameters.AddWithValue("@no_of_likes", 0);
                    cmd.Parameters.AddWithValue("@no_of_shares", 0);
                    cmd.Parameters.AddWithValue("@reward_per_user", 0);
                    cmd.Parameters.AddWithValue("@reward_on_friend", 0);
                    cmd.Parameters.AddWithValue("@reward_on_likes", 0);
                    cmd.Parameters.AddWithValue("@reward_on_shares", 0);
                    cmd.Parameters.AddWithValue("@returnid", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                    ConnObj.GetDataTab(cmd);
                }

                string id = cmd.Parameters["@returnid"].Value.ToString();

                if (id == "")
                {
                    continue;
                }

                Int64 activity_id = Convert.ToInt64(id);

                // insert facebook post
                cmd = new SqlCommand("sp1_IChooseIT_User_Insta_Activities_Insert");
                cmd.Parameters.AddWithValue("@activity_id", activity_id);
                cmd.Parameters.AddWithValue("@reg_uid", t.reg_uid);
                cmd.Parameters.AddWithValue("@brand_id", brand_id1);
                cmd.Parameters.AddWithValue("@campaign_id", campaign_id1);
                cmd.Parameters.AddWithValue("@action_id", action_id1);
                cmd.Parameters.AddWithValue("@created_on", ConvertFromUnixTimestamp(Convert.ToDouble( i.created_time)) );
                cmd.Parameters.AddWithValue("@updated_time", ConvertFromUnixTimestamp(Convert.ToDouble(i.created_time)));
                cmd.Parameters.AddWithValue("@sm_img_link", Convert.ToString(i.img));
                cmd.Parameters.AddWithValue("@link", Convert.ToString(i.link));
                cmd.Parameters.AddWithValue("@sm_desc", Convert.ToString(i.text));

                cmd.Parameters.AddWithValue("@fb_post_id", Convert.ToString(i.mediaID));

                ConnObj.GetDataTab(cmd);
                ConnObj.ReleaseConnection();

        }
    }
    public void checkIfUserLikesAPage(string accessToken, List<fbuser> user, string page_id, Int32 brand_id, Int64 campaign_id, Int64 action_id,
                       decimal reward_amount, decimal reward_amount_max, decimal reward_percent, decimal reward_percent_max, string reward_product, decimal reward_point, Int32 no_of_friends,
                       Int32 no_of_likes, Int32 no_of_shares, decimal reward_per_user, decimal reward_on_friend,
                       decimal reward_on_likes, decimal reward_on_shares)
    {
        // database will have the list of pages that a user likes already
        // we only need to cross check with the database

        fbuser t = user[0];

        // get the list of user who like the given page
        ConnObj = new ConnectionClass();
        SqlCommand cmd = new SqlCommand("sp_IChooseIT_Get_Users_WhoLikeAPage");
        cmd.Parameters.AddWithValue("@page_id", page_id);
        cmd.Parameters.AddWithValue("@sm_id", 3);
        ConnObj.GetDataSet(cmd);
        decimal points = 0;
        if (ConnObj.IsSuccess && ConnObj.DataSet.Tables.Count > 0 && ConnObj.DataSet.Tables[0].Rows.Count > 0)
        {
            ConnectionClass ConnObj2 = new ConnectionClass();
            foreach (DataRow dr in ConnObj.DataSet.Tables[0].Rows)
            {
                if (Convert.ToInt64(dr["reg_uid"]) != t.reg_uid) continue;

                SqlCommand cmd1 = new SqlCommand("sp1_IChooseIT_User_Activities_Insert");
                cmd1.Parameters.AddWithValue("@reg_uid", t.reg_uid);
                cmd1.Parameters.AddWithValue("@brand_id", brand_id);
                cmd1.Parameters.AddWithValue("@campaign_id", campaign_id);
                cmd1.Parameters.AddWithValue("@action_id", action_id);
                cmd1.Parameters.AddWithValue("@created_on", DateTime.Now.ToString());
                cmd1.Parameters.AddWithValue("@pid", "");
                // reward table details
                if (reward_per_user > 0)
                {
                    points = reward_per_user;
                }
                if (reward_on_friend > 0)
                {
                    // get number of friends for the user
                    no_of_friends = t.no_of_friends;
                    points += (reward_on_friend * no_of_friends);
                }
                if (reward_point > 0)
                {
                    reward_amount = (reward_amount / reward_point) * points;
                    reward_percent = (reward_percent / reward_point) * points;

                    reward_amount = (reward_amount > reward_amount_max) ? reward_amount_max : reward_amount;
                    reward_percent = (reward_percent > reward_percent_max) ? reward_percent_max : reward_percent;
                }
                cmd1.Parameters.AddWithValue("@reward_amount", reward_amount);
                cmd1.Parameters.AddWithValue("@reward_percent", reward_percent);
                cmd1.Parameters.AddWithValue("@reward_product", reward_product);
                cmd1.Parameters.AddWithValue("@no_of_friends", no_of_friends);
                cmd1.Parameters.AddWithValue("@no_of_likes", no_of_likes);
                cmd1.Parameters.AddWithValue("@no_of_shares", no_of_shares);
                cmd1.Parameters.AddWithValue("@reward_per_user", reward_per_user);
                cmd1.Parameters.AddWithValue("@reward_on_friend", reward_on_friend);
                cmd1.Parameters.AddWithValue("@reward_on_likes", reward_on_likes);
                cmd1.Parameters.AddWithValue("@reward_on_shares", reward_on_shares);
                cmd1.Parameters.AddWithValue("@returnid", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                ConnObj2.GetDataTab(cmd1);
            }

            ConnObj2.ReleaseConnection();
            ConnObj.ReleaseConnection();
        }
    }
    public void getUserTaggedPlaces(string accessToken, Int64 reg_uid)
    {
        var client = new FacebookClient(accessToken);

        dynamic posts = client.Get("/me/tagged_places");

        String insert_stmt = ""; String insert_stmt_comma = "";
        string uid = Convert.ToString(reg_uid);

        string location; string place_name; DateTime created_time; string data_id; string place_id;

        //Loop through the returned locations
        foreach (var i in posts["data"])
        {

            created_time = Convert.ToDateTime(i["created_time"]);
            data_id = i["id"];

            location = System.Web.HttpUtility.HtmlEncode(i["place"]["location"]);
            place_id = System.Web.HttpUtility.HtmlEncode(i["place"]["id"]);
            place_name = System.Web.HttpUtility.HtmlEncode(i["place"]["name"]);

            insert_stmt += insert_stmt_comma + "(" + uid + ", '" + data_id + "', '" + place_id + "','" + location + "', '" + place_name + "', '" + created_time.ToString() + "' )";
            insert_stmt_comma = ",";

        }

        if (insert_stmt != "")
        {
            ConnObj = new ConnectionClass();
            SqlCommand cmd = new SqlCommand("sp1_IChooseIT_User_FB_Locations_Insert");
            cmd.Parameters.AddWithValue("@insert_stmt", insert_stmt);
            cmd.Parameters.AddWithValue("@reg_uid", reg_uid);
            ConnObj.GetDataTab(cmd);
            ConnObj.ReleaseConnection();
        }
    }
    public void getUserPageLikes(string accessToken, Int64 reg_uid)
    {
        var client = new FacebookClient(accessToken);

        dynamic posts = client.Get("/me/likes");

        String insert_stmt = ""; String insert_stmt_comma = "";
        string uid = Convert.ToString( reg_uid );

        string category; string page_name; DateTime created_time; string page_id;

        //Loop through the returned friends
        foreach (var i in posts["data"])
        {
            category = System.Web.HttpUtility.HtmlEncode(i["category"]);
            page_name = System.Web.HttpUtility.HtmlEncode(i["name"]);
            created_time = Convert.ToDateTime(  i["created_time"] );
            page_id = i["id"];

            insert_stmt += insert_stmt_comma + "(" + uid + ",'" + category + "', '" + page_name + "', '" + created_time.ToString() + "','" + page_id + "' )";
            insert_stmt_comma = ",";

        }

        if (insert_stmt != "")
        {
            ConnObj = new ConnectionClass();
            SqlCommand cmd = new SqlCommand("sp1_brandyy_User_FB_Likes_Insert");
            cmd.Parameters.AddWithValue("@insert_stmt", insert_stmt);
            cmd.Parameters.AddWithValue("@reg_uid", reg_uid);
            ConnObj.GetDataTab(cmd);
            ConnObj.ReleaseConnection();
        }
    }
    public void getUserFriends(string accessToken, Int64 reg_uid)
    {
        var client = new FacebookClient(accessToken);

        dynamic posts = client.Get("/me/friends");

        String insert_stmt = ""; String insert_stmt_comma = "";
        string uid = Convert.ToString(reg_uid);

        string data_id; string sm_id;
        sm_id="1";

        //Loop through the returned locations
        foreach (var i in posts["data"])
        {
            data_id = i["id"];

            insert_stmt += insert_stmt_comma + "(" + sm_id + ", " + uid + ", '" + data_id + "' )";
            insert_stmt_comma = ",";

        }

        if (insert_stmt != "")
        {
            ConnObj = new ConnectionClass();
            SqlCommand cmd = new SqlCommand("sp1_Insert_IChooseIT_UseR_SM_Friends");
            cmd.Parameters.AddWithValue("@insert_stmt", insert_stmt);
            cmd.Parameters.AddWithValue("@reg_uid", reg_uid);
            cmd.Parameters.AddWithValue("@sm_id", sm_id);
            ConnObj.GetDataTab(cmd);
            ConnObj.ReleaseConnection();
        }
    }
    public void getAllProfileDetails(string accessToken)
    {
        var client = new FacebookClient(accessToken);

        //http://blog.prabir.me/posts/facebook-csharp-sdk-making-requests

        dynamic posts = (IDictionary<string, object>)client.Get("/me");

        String insert_stmt = ""; String insert_stmt_comma = "";
        //string reg_uid = Convert.ToString( SessionState._SignInUser.reg_uid );
        string reg_uid = "1";
        int verified_status=0;
        foreach (KeyValuePair<string, object> admObj in posts)
        {
            var id = Convert.ToString(admObj.Key);
            var value = Convert.ToString(admObj.Value);
            insert_stmt += insert_stmt_comma + "(1," + reg_uid + ",'" + id + "', '" + value + "', '/me' )";
            insert_stmt_comma = ",";

            if (id == "verified")
            {
                verified_status = (value == "True") ? 1 : 0;
            }
        }

        if (insert_stmt != "")
        {
            ConnObj = new ConnectionClass();

            SqlCommand cmd = new SqlCommand("sp1_Insert_IChooseIT_User_SM_All_Details");
            cmd.Parameters.AddWithValue("@reg_uid", reg_uid);
            cmd.Parameters.AddWithValue("@insert_stmt", insert_stmt);
            cmd.Parameters.AddWithValue("@sm_id", 1);
            ConnObj.GetDataTab(cmd);

            cmd = new SqlCommand("sp1_Update_IChooseIT_Registrations_FB_Verify");
            cmd.Parameters.AddWithValue("@reg_uid", reg_uid);
            cmd.Parameters.AddWithValue("@verified_status", verified_status);
            cmd.Parameters.AddWithValue("@sm_id", 1);
            ConnObj.GetDataTab(cmd);

            ConnObj.ReleaseConnection();
        }
    }
    public void checkIfUserHasAddedAnyPost(string accessToken, List<fbuser> user, string hashtag, string defaulttext, String last_cron_run, Int32 brand_id, Int64 campaign_id, Int64 action_id,
                        decimal reward_amount, decimal reward_amount_max, decimal reward_percent, decimal reward_percent_max, string reward_product, decimal reward_point, Int32 no_of_friends,
                        Int32 no_of_likes, Int32 no_of_shares, decimal reward_per_user, decimal reward_on_friend,
                        decimal reward_on_likes, decimal reward_on_shares)
    {
        var client = new FacebookClient(accessToken);
        dynamic posts;
        if ((last_cron_run == ""))
        {
            posts = client.Get("/cooseit/tagged");
        }
        else
        {
            posts = client.Get("/cooseit/tagged?since=" + last_cron_run);
        }

        fbuser t = user[0];

        //Loop through the returned users
        foreach (var i in posts["data"])
        {

            Int32 brand_id1; Int64 campaign_id1; Int64 action_id1;

            string message = i["message"];
            hashtag = hashtag.Replace("@ichoose", "");
            string[] hashtag_arr = hashtag.Split(' ');
            bool valid = true;
            foreach (string hash in hashtag_arr)
            {
                if (!message.Contains(hash.Trim()))
                {
                    valid = false;
                    break;
                }
            }
            if (valid == false)
            {
                //continue;
            }

            // case : default text defined for campaign
            if (defaulttext != "")
            {
                if (!message.Contains(defaulttext))
                {
                    valid = false;
                    //continue;
                }
            }

            // check if the post link contains the post id
            if (t.sm_uid == i["from"].id)
            {

                ConnObj = new ConnectionClass();
                SqlCommand cmd = new SqlCommand("sp1_IChooseIT_User_Activities_Insert");

                // case : valid
                if (valid == true)
                {
                    brand_id1 = brand_id; campaign_id1 = campaign_id; action_id1 = action_id;
                    cmd.Parameters.AddWithValue("@reg_uid", t.reg_uid);
                    cmd.Parameters.AddWithValue("@brand_id", brand_id);
                    cmd.Parameters.AddWithValue("@campaign_id", campaign_id);
                    cmd.Parameters.AddWithValue("@action_id", action_id);
                    cmd.Parameters.AddWithValue("@created_on", Convert.ToDateTime(i["created_time"]));
                    cmd.Parameters.AddWithValue("@pid", i["id"]);
                    decimal points = 0;
                    no_of_friends = 0;
                    no_of_likes = 0;
                    // reward table details
                    if (reward_per_user > 0)
                    {
                        points += reward_per_user;
                    }
                    if (reward_on_friend > 0)
                    {
                        // get number of friends for the user
                        no_of_friends = t.no_of_friends;
                        points += (no_of_friends * reward_on_friend);
                    }
                    if (reward_on_likes > 0)
                    {
                        if (i.ContainsKey("likes"))
                        {
                            no_of_likes = i["likes"].data.Count;
                        }
                        points += (no_of_likes * reward_on_likes);
                    }
                    if (reward_point > 0)
                    {
                        reward_amount = (reward_amount / reward_point) * points;
                        reward_percent = (reward_percent / reward_point) * points;

                        reward_amount = (reward_amount > reward_amount_max) ? reward_amount_max : reward_amount;
                        reward_percent = (reward_percent > reward_percent_max) ? reward_percent_max : reward_percent;
                    }

                    cmd.Parameters.AddWithValue("@reward_amount", reward_amount);
                    cmd.Parameters.AddWithValue("@reward_percent", reward_percent);
                    cmd.Parameters.AddWithValue("@reward_product", reward_product);
                    cmd.Parameters.AddWithValue("@no_of_friends", no_of_friends);
                    cmd.Parameters.AddWithValue("@no_of_likes", no_of_likes);
                    cmd.Parameters.AddWithValue("@no_of_shares", no_of_shares);
                    cmd.Parameters.AddWithValue("@reward_per_user", reward_per_user);
                    cmd.Parameters.AddWithValue("@reward_on_friend", reward_on_friend);
                    cmd.Parameters.AddWithValue("@reward_on_likes", reward_on_likes);
                    cmd.Parameters.AddWithValue("@reward_on_shares", reward_on_shares);
                    cmd.Parameters.AddWithValue("@returnid", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                    ConnObj.GetDataTab(cmd);
                }
                else
                {
                    brand_id1 = 2; campaign_id1 = 8; action_id1 = 16;
                    cmd.Parameters.AddWithValue("@reg_uid", t.reg_uid);
                    cmd.Parameters.AddWithValue("@brand_id", brand_id1);
                    cmd.Parameters.AddWithValue("@campaign_id", campaign_id1);
                    cmd.Parameters.AddWithValue("@action_id", action_id1);
                    cmd.Parameters.AddWithValue("@created_on", Convert.ToDateTime(i["created_time"]));
                    cmd.Parameters.AddWithValue("@pid", i["id"]);
                    cmd.Parameters.AddWithValue("@reward_amount", 0);
                    cmd.Parameters.AddWithValue("@reward_percent", 0);
                    cmd.Parameters.AddWithValue("@reward_product", "");
                    cmd.Parameters.AddWithValue("@no_of_friends", 0);
                    cmd.Parameters.AddWithValue("@no_of_likes", 0);
                    cmd.Parameters.AddWithValue("@no_of_shares", 0);
                    cmd.Parameters.AddWithValue("@reward_per_user", 0);
                    cmd.Parameters.AddWithValue("@reward_on_friend", 0);
                    cmd.Parameters.AddWithValue("@reward_on_likes", 0);
                    cmd.Parameters.AddWithValue("@reward_on_shares", 0);
                    cmd.Parameters.AddWithValue("@returnid", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                    ConnObj.GetDataTab(cmd);
                }

                string id = cmd.Parameters["@returnid"].Value.ToString();

                if (id == "")
                {
                    continue;
                }

                Int64 activity_id = Convert.ToInt64(id);

                // insert facebook post
                cmd = new SqlCommand("sp1_IChooseIT_User_FB_Activities_Insert");
                cmd.Parameters.AddWithValue("@activity_id", activity_id);
                cmd.Parameters.AddWithValue("@reg_uid", t.reg_uid);
                cmd.Parameters.AddWithValue("@brand_id", brand_id1);
                cmd.Parameters.AddWithValue("@campaign_id", campaign_id1);
                cmd.Parameters.AddWithValue("@action_id", action_id1);
                cmd.Parameters.AddWithValue("@created_on", Convert.ToDateTime(i["created_time"]));
                cmd.Parameters.AddWithValue("@updated_time", Convert.ToDateTime(i["updated_time"]));
                if (i.ContainsKey("picture"))
                {
                    cmd.Parameters.AddWithValue("@sm_img_link", i["picture"]);
                }
                else
                {
                    cmd.Parameters.AddWithValue("@sm_img_link", "");
                }
                if (i.ContainsKey("link"))
                {
                    cmd.Parameters.AddWithValue("@link", i["link"]);
                }
                else
                {
                    if (i.ContainsKey("actions"))
                    {
                        cmd.Parameters.AddWithValue("@link", Convert.ToString(i["actions"][0]["link"]));
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@link", "");
                    }

                }
                cmd.Parameters.AddWithValue("@sm_desc", i["message"]);

                cmd.Parameters.AddWithValue("@fb_post_id", i["id"]);

                ConnObj.GetDataTab(cmd);
                ConnObj.ReleaseConnection();
            }
        }
    }
    public void checkIfUserSharedAPost(string accessToken, List<fbuser> user, string post_id, Int32 brand_id, Int64 campaign_id, Int64 action_id,
                        decimal reward_amount, decimal reward_amount_max, decimal reward_percent, decimal reward_percent_max, string reward_product, decimal reward_point, Int32 no_of_friends,
                        Int32 no_of_likes, Int32 no_of_shares, decimal reward_per_user, decimal reward_on_friend,
                        decimal reward_on_likes, decimal reward_on_shares)
    {
        var client = new FacebookClient(accessToken);

        dynamic posts = client.Get("/me/links/");
        fbuser u = user[0];

        //Loop through the returned link
        decimal points = 0;
        string post1 = post_id;
        if (post_id.Split('_').Length > 1)
        {
            post1 = post_id.Split('_')[1];
        }

        string json = Newtonsoft.Json.JsonConvert.SerializeObject(posts);
        JObject tmpResult = JObject.Parse(json);
        var i = tmpResult["data"].Values<JObject>()
        .Where(n => n["link"].Value<string>().Contains(post1))
        .Select(n => new { link = n["created_time"] }).ToArray();

            // check if the post link contains the post id
            if (i.Count() > 0 )
            {
                ConnObj = new ConnectionClass();
                SqlCommand cmd = new SqlCommand("sp1_IChooseIT_User_Activities_Insert");
                cmd.Parameters.AddWithValue("@reg_uid", u.reg_uid);
                cmd.Parameters.AddWithValue("@brand_id", brand_id);
                cmd.Parameters.AddWithValue("@campaign_id", campaign_id);
                cmd.Parameters.AddWithValue("@action_id", action_id);
                cmd.Parameters.AddWithValue("@created_on", Convert.ToDateTime(i[0].link));
                cmd.Parameters.AddWithValue("@pid", "");

                // reward table details
                if (reward_per_user > 0)
                {
                    points = reward_per_user;
                }
                if (reward_on_friend > 0)
                {
                    // get number of friends for the user
                    no_of_friends = u.no_of_friends;
                    points += (reward_on_friend * no_of_friends);
                }

                if (reward_point > 0)
                {
                    reward_amount = (reward_amount / reward_point) * points;
                    reward_percent = (reward_percent / reward_point) * points;

                    reward_amount = (reward_amount > reward_amount_max) ? reward_amount_max : reward_amount;
                    reward_percent = (reward_percent > reward_percent_max) ? reward_percent_max : reward_percent;
                }

                cmd.Parameters.AddWithValue("@reward_amount", reward_amount);
                cmd.Parameters.AddWithValue("@reward_percent", reward_percent);
                cmd.Parameters.AddWithValue("@reward_product", reward_product);
                cmd.Parameters.AddWithValue("@no_of_friends", no_of_friends);
                cmd.Parameters.AddWithValue("@no_of_likes", no_of_likes);
                cmd.Parameters.AddWithValue("@no_of_shares", no_of_shares);
                cmd.Parameters.AddWithValue("@reward_per_user", reward_per_user);
                cmd.Parameters.AddWithValue("@reward_on_friend", reward_on_friend);
                cmd.Parameters.AddWithValue("@reward_on_likes", reward_on_likes);
                cmd.Parameters.AddWithValue("@reward_on_shares", reward_on_shares);
                cmd.Parameters.AddWithValue("@returnid", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                ConnObj.GetDataTab(cmd);
                ConnObj.ReleaseConnection();
        }
    }
    public void checkIfUserHasAddedAnyPost(string accessToken, List<fbuser> user, string hashtag, string defaulttext, String last_cron_run, Int32 brand_id, Int64 campaign_id, Int64 action_id,
                        decimal reward_amount, decimal reward_amount_max, decimal reward_percent, decimal reward_percent_max, string reward_product, decimal reward_point, Int32 no_of_friends,
                        Int32 no_of_likes, Int32 no_of_shares, decimal reward_per_user, decimal reward_on_friend,
                        decimal reward_on_likes, decimal reward_on_shares)
    {
        hashtag = hashtag.Replace("@" + System.Configuration.ConfigurationManager.AppSettings["TwitterIchooseItHashtag"], "");
        string[] hashtag_arr = hashtag.Split(' ');
        hashtag = "";
        foreach (string hash in hashtag_arr)
        {
            string hash1 = hash.Replace('#', ' ').Trim();
            if (hash1 != "")
            {
                hashtag += "%23" + hash1;
            }
        }

        //
        //string str = "@ichooseit256%20from:" + SessionState._SignInUser.reg_email + "&result_type=mixed";
        string str = "@ichooseit256" + hashtag + "&result_type=mixed";

        string url = "https://api.twitter.com/1.1/search/tweets.json?q=" + str;

        string posts = Search(url, str);

        fbuser t = user[0];
        try
        {
            var jsonDat = (JObject.Parse(posts));
            foreach (var key in jsonDat)
            {
                if (key.Key.Equals("statuses"))
                {

                    for (int x = 0; x < key.Value.Count(); x++)
                    {
                        // case : default text defined for campaign
                        if (defaulttext != "")
                        {
                            // check if tweet contains the default text
                            if (!Convert.ToString(key.Value[x]["text"]).Contains(defaulttext))
                            {
                                continue;
                            }
                        }

                        // check if the post link contains the post id
                        if (t.sm_uid == Convert.ToString(key.Value[x]["user"]["id"]))
                        {
                            #region save to sp1_IChooseIT_User_Activities_Insert

                            ConnObj = new ConnectionClass();
                            SqlCommand cmd = new SqlCommand("sp1_IChooseIT_User_Activities_Insert");
                            cmd.Parameters.AddWithValue("@reg_uid", t.reg_uid);
                            cmd.Parameters.AddWithValue("@brand_id", brand_id);
                            cmd.Parameters.AddWithValue("@campaign_id", campaign_id);
                            cmd.Parameters.AddWithValue("@action_id", action_id);
                            string temp = Convert.ToString(key.Value[x]["created_at"]);
                            cmd.Parameters.AddWithValue("@created_on", ParseDateTime(Convert.ToString(key.Value[x]["created_at"])));
                            cmd.Parameters.AddWithValue("@pid", Convert.ToString(key.Value[x]["id"]));

                            decimal points = 0;
                            no_of_friends = 0;
                            no_of_likes = 0;
                            no_of_shares = 0;

                            // reward table details
                            if (reward_per_user > 0)
                            {
                                points = reward_per_user;
                            }
                            if (reward_on_friend > 0)
                            {
                                // get number of friends for the user
                                no_of_friends = t.no_of_friends;
                                points += (no_of_friends * reward_on_friend);
                            }
                            if (reward_on_likes > 0)
                            {
                                if (Convert.ToInt32(key.Value[x]["favorite_count"]) != 0)
                                {
                                    no_of_likes = Convert.ToInt32(key.Value[x]["favorite_count"]);
                                }
                                points += (no_of_likes * reward_on_likes);
                            }
                            if (reward_on_shares > 0)
                            {
                                if (Convert.ToInt32(key.Value[x]["retweeted"]) != 0)
                                {
                                    no_of_shares = Convert.ToInt32(key.Value[x]["retweeted"]);
                                }
                                points += (no_of_shares * reward_on_shares);
                            }
                            if (reward_point > 0)
                            {
                                reward_amount = (reward_amount / reward_point) * points;
                                reward_percent = (reward_percent / reward_point) * points;

                                reward_amount = (reward_amount > reward_amount_max) ? reward_amount_max : reward_amount;
                                reward_percent = (reward_percent > reward_percent_max) ? reward_percent_max : reward_percent;
                            }
                            cmd.Parameters.AddWithValue("@reward_amount", reward_amount);
                            cmd.Parameters.AddWithValue("@reward_percent", reward_percent);
                            cmd.Parameters.AddWithValue("@reward_product", reward_product);
                            cmd.Parameters.AddWithValue("@no_of_friends", no_of_friends);
                            cmd.Parameters.AddWithValue("@no_of_likes", no_of_likes);
                            cmd.Parameters.AddWithValue("@no_of_shares", no_of_shares);
                            cmd.Parameters.AddWithValue("@reward_per_user", reward_per_user);
                            cmd.Parameters.AddWithValue("@reward_on_friend", reward_on_friend);
                            cmd.Parameters.AddWithValue("@reward_on_likes", reward_on_likes);
                            cmd.Parameters.AddWithValue("@reward_on_shares", reward_on_shares);
                            cmd.Parameters.AddWithValue("@returnid", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                            ConnObj.GetDataTab(cmd);

                            string id = cmd.Parameters["@returnid"].Value.ToString();

                            if (id == "")
                            {
                                continue;
                            }

                            Int64 activity_id = Convert.ToInt64(id);
                            #endregion

                            #region save to IChooseIT_User_Activities_Tw_Post

                            // insert twitter post
                            cmd = new SqlCommand("sp1_IChooseIT_User_Tw_Activities_Insert");
                            cmd.Parameters.AddWithValue("@activity_id", activity_id);
                            cmd.Parameters.AddWithValue("@reg_uid", t.reg_uid);
                            cmd.Parameters.AddWithValue("@brand_id", brand_id);
                            cmd.Parameters.AddWithValue("@campaign_id", campaign_id);
                            cmd.Parameters.AddWithValue("@action_id", action_id);
                            cmd.Parameters.AddWithValue("@created_on", ParseDateTime(Convert.ToString(key.Value[x]["created_at"])));
                            cmd.Parameters.AddWithValue("@updated_time", ParseDateTime(Convert.ToString(key.Value[x]["created_at"])));

                            if (Convert.ToString(key.Value[x]["entities"]["media"]) != "")
                            {
                                cmd.Parameters.AddWithValue("@sm_img_link", Convert.ToString(key.Value[x]["entities"]["media"][0]["media_url"]));
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@sm_img_link", "");
                            }
                            cmd.Parameters.AddWithValue("@sm_desc", Convert.ToString(key.Value[x]["text"]));
                            string link = "https://twitter.com/" + Convert.ToString(key.Value[x]["user"]["screen_name"]) + "/status/" + Convert.ToString(key.Value[x]["id"]);
                            cmd.Parameters.AddWithValue("@link", link);
                            cmd.Parameters.AddWithValue("@fb_post_id", Convert.ToString(key.Value[x]["id"]));

                            ConnObj.GetDataTab(cmd);
                            ConnObj.ReleaseConnection();

                            #endregion
                        }
                    }
                }

            }
        }
        catch (Exception ex)
        {
        }
    }
    private void CheckIfActivitiesWaitingVerification()
    {
        Int32 b_id = SessionState._BrandAdmin.brand_id;
        SqlCommand cmd = new SqlCommand("sp_brandyy_GetAllPendingActivitiesOverview");
        cmd.Parameters.AddWithValue("@brand_id", SessionState._BrandAdmin.brand_id);
        cmd.Parameters.AddWithValue("@verification_status", 0);

        ConnectionClass ConnObj1 = new ConnectionClass();
        ConnObj1.GetDataSet(cmd);
        if (ConnObj1.IsSuccess && ConnObj1.DataSet.Tables.Count > 0 && ConnObj1.DataSet.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ConnObj1.DataSet.Tables[0].Rows)
            {
                if (Convert.ToInt64(dr["num_of_pending_activities"]) > 0)
                {
                    display_verification = true;
                    break;
                }
            }
        }
        ConnObj1.ReleaseConnection();
    }