/// <summary>
 /// The event that takes care of the clicked button inside the table of friends.
 /// </summary>
 /// <param name="i_BirthdayWish">The wish that the user wants to send</param>
 /// <param name="i_FriendWithBirthday">The friend that needs to receive the wish</param>
 public void temporaryMessageRow_SubmitClicked(string i_BirthdayWish, Friend i_FriendWithBirthday)
 {
     // Adds the birthday wish to the correct friend.
     if (m_UserFriends[i_FriendWithBirthday.Id] != null)
     {
         m_UserFriends[i_FriendWithBirthday.Id].BirthdayMessage = i_BirthdayWish;
     }
 }
 /// <summary>
 /// The event that takes care of the clicked button inside the table of friends.
 /// </summary>
 /// <param name="i_BirthdayWish">The wish that the user wants to send</param>
 /// <param name="i_FriendWithBirthday">The friend that needs to receive the wish</param>
 public void temporaryMessageRow_SubmitClicked(string i_BirthdayWish, Friend i_FriendWithBirthday)
 {
     Friend temporaryFriend = m_UserFriends[i_FriendWithBirthday.Id];
     // Adds the birthday wish to the correct friend.
     if (temporaryFriend != null)
     {
         // NEEDS TO CREATE THIS METHOD
         //m_DataBaseHandlerObject.DeleteFriendFromDatabase(m_ApplicationUser, temporaryFriend);
     }
 }
 /// <summary>
 /// The event that takes care of the clicked button inside the table of friends.
 /// </summary>
 /// <param name="i_BirthdayWish">The wish that the user wants to send</param>
 /// <param name="i_FriendWithBirthday">The friend that needs to receive the wish</param>
 public void temporaryMessageRow_SubmitClicked(string i_BirthdayWish, Friend i_FriendWithBirthday)
 {
     Friend temporaryFriend = m_UserFriends[i_FriendWithBirthday.Id];
     // Adds the birthday wish to the correct friend.
     if (temporaryFriend != null)
     {
         temporaryFriend.BirthdayMessage = i_BirthdayWish;
         m_DataBaseHandlerObject.UpdateBirthdayMessage(m_ApplicationUser, temporaryFriend);
     }
 }
        /// <summary>
        /// return a dictionary of users friends. sorted as {ID,FRIEND}
        /// </summary>
        /// <param name="i_AccessToken"></param>
        /// <returns></returns>
        public static Dictionary<string, Friend> GetUsersFriends(string i_AccessToken)
        {
            Dictionary<string, Friend>  result = new Dictionary<string, Friend>();
            FacebookClient fbClient = new FacebookClient(i_AccessToken);

            dynamic userFriends = fbClient.Query(
             "SELECT uid, name, first_name, last_name, pic_small, pic_big, pic_square, pic, birthday_date FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())");
            foreach (dynamic friend in userFriends)
            {
                Friend tempFriend = new Friend();
                createUserFromDynamicUser(friend, tempFriend);
                result.Add(tempFriend.Id, tempFriend);
            }

            return result;
        }
        public TableMessageRow(Friend i_Friend)
        {
            if (i_Friend != null)
            {
                this.Friend = i_Friend;
                this.Picture = new Image();
                this.Label = new Label();
                this.TextBox = new TextBox();
                this.ConfirmButton = new Button();
                initRow();
            }

            setRowProperties();

            // Creates the cells and adds them to the row
            populateRow(createCells());
        }
 private void testyoav()
 {
     ApplicationUser me = FacebookUtilities.GetUser(m_AccessToken);
     Dictionary<string, Friend> friends = FacebookUtilities.GetUsersFriends(m_AccessToken);
     DataBaseHandler db = new DataBaseHandler();
     db.InsertSingleApplicationUser(me);
     bool result = db.IsUserInDataBase(me);
     Dictionary<string, Friend> friendsThatAreInDB1 = db.GetUserFriendsThatAreInDataBase(me);
     Friend testfriend = new Friend();
     foreach (Friend friend in friends.Values)
     {
         friend.BirthdayMessage = string.Format("mazal tov {0} from {1}", friend.FullName, me.FullName);
         testfriend = friend;
     }
     db.InsertFriendsIntoDataBase(me, friends.Values);
     Dictionary<string, Friend> friendsThatAreInDB2 = db.GetUserFriendsThatAreInDataBase(me);
     testfriend.BirthdayMessage = "CHANGED!!!!!!";
     db.UpdateBirthdayMessage(me, testfriend);
 }
        /// <summary>
        /// checks if a given messgae i.e. combination of appuser-friend is in db
        /// </summary>
        /// <param name="i_ApplicationUser"></param>
        /// <param name="i_Friend"></param>
        /// <returns></returns>
        private bool messageIsInDatabaseWithoutConnection(ApplicationUser i_ApplicationUser, Friend i_Friend)
        {
            bool result = false;
            SqlDataReader dbReader = null;
            string query = string.Format(
                "SELECT * FROM {0} WHERE {1} = {2} AND {3} = {4}",
                   eTabelsInDataBase.Birthday_Messages.ToString(),
                   eTbale_Birthday_Messages_Columns.From_Application_User_ID.ToString(),
                   i_ApplicationUser.Id,
                   eTbale_Birthday_Messages_Columns.To_Friend_ID.ToString(),
                   i_Friend.Id);

            dbReader = commandDataBase(query, true);
            if (dbReader.HasRows)
            {
                result = true;
            }

            closeDataBaseReader(dbReader);
            return result;
        }
 /// <summary>
 /// this method updates the message from an applicatoin user to his friend.
 /// this method assumes that there was a message before and both the user and friend are in the db
 /// </summary>
 /// <param name="i_ApplicationUser"></param>
 /// <param name="i_Friend"></param>
 public void UpdateBirthdayMessage(ApplicationUser i_ApplicationUser, Friend i_Friend)
 {
     openConnection();
     if (checkIfUserIsInDataBaseWithOutConnection(i_ApplicationUser) && checkIfUserIsInDataBaseWithOutConnection(i_Friend) && messageIsInDatabaseWithoutConnection(i_ApplicationUser,i_Friend))
     {
         string command = string.Format(
             "UPDATE {0} SET {1} = '{2}' WHERE {3} = {4} AND {5} = {6}",
             eTabelsInDataBase.Birthday_Messages.ToString(),
             eTbale_Birthday_Messages_Columns.Birthday_Greet.ToString(),
             i_Friend.BirthdayMessage,
             eTbale_Birthday_Messages_Columns.To_Friend_ID.ToString(),
             i_Friend.Id,
             eTbale_Birthday_Messages_Columns.From_Application_User_ID.ToString(),
             i_ApplicationUser.Id);
         commandDataBase(command, false);
     }
     closeConnection();
 }
        /// <summary>
        /// This method returns the friends of the user that are already in the database.
        /// Returns a dictionary by {ID,Friend}.
        /// if the user dosn't have any friends in the DB, The count field of the dictionary will 
        /// be 0
        /// Query:
        /// SELECT FacebookUser.*,Birthday_Greet
        /// FROM Friend, Birthday_Messages,FacebookUser
        /// WHERE From_Application_User_ID = '2' AND Friend_ID = To_Friend_ID and ID = Friend_ID
        /// </summary>
        /// <param name="i_User"></param>
        /// <returns></returns>
        public Dictionary<string, Friend> GetUserFriendsThatAreInDataBase(ApplicationUser i_User)
        {
            Dictionary<string, Friend> result = new Dictionary<string, Friend>();

            //build query
            string query = string.Format(
                "SELECT {0}, {1} FROM {2}, {3}, {4} WHERE {5} = {6} AND {7} = {8} AND {9} = {10}",
                addDotAndStarToString(eTabelsInDataBase.FacebookUser.ToString()),
                eTbale_Birthday_Messages_Columns.Birthday_Greet.ToString(),
                eTabelsInDataBase.Friend.ToString(),
                eTabelsInDataBase.Birthday_Messages.ToString(),
                eTabelsInDataBase.FacebookUser.ToString(),
                eTbale_Birthday_Messages_Columns.From_Application_User_ID.ToString(),
                i_User.Id,
                eTable_Friend.Friend_ID.ToString(),
                eTbale_Birthday_Messages_Columns.To_Friend_ID.ToString(),
                eTable_FacebookUser.ID.ToString(),
                eTable_Friend.Friend_ID.ToString());

            openConnection();
            SqlDataReader dbReader = commandDataBase(query, true);

            //create friends from result
            while (dbReader.Read())
            {
                Friend tempFriend = new Friend();
                tempFriend.Id = dbReader[eTable_FacebookUser.ID.ToString()].ToString();
                tempFriend.FirstName = (string)dbReader[eTable_FacebookUser.First_Name.ToString()];
                tempFriend.LastName = (string)dbReader[eTable_FacebookUser.Last_Name.ToString()];
                tempFriend.FullName = (string)dbReader[eTable_FacebookUser.Full_Name.ToString()];
                tempFriend.BirthdayDateTime = (DateTime)dbReader[eTable_FacebookUser.Birthday.ToString()];
                tempFriend.BirthdayMessage = (string)dbReader[eTbale_Birthday_Messages_Columns.Birthday_Greet.ToString()];

                //build pictures
                Dictionary<string, string> tempFriendPics = new Dictionary<string, string>();
                tempFriendPics[ePictureTypes.pic.ToString()] = (string)dbReader[eTable_FacebookUser.Pic.ToString()];
                tempFriendPics[ePictureTypes.pic_big.ToString()] = (string)dbReader[eTable_FacebookUser.Pic_Big.ToString()];
                tempFriendPics[ePictureTypes.pic_small.ToString()] = (string)dbReader[eTable_FacebookUser.Pic_Small.ToString()];
                tempFriendPics[ePictureTypes.pic_square.ToString()] = (string)dbReader[eTable_FacebookUser.Pic_Square.ToString()];
                tempFriend.Pictures = tempFriendPics;
                result[tempFriend.Id] = tempFriend;
            }
            closeReaderAndConnection(dbReader);
            return result;
        }