protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {
                if (NavigationContext.QueryString.TryGetValue(UriParameter.FriendId, out Parameter))
                {
                    Action = ActionType.Details;
                    FriendEntity = BirthdayUtility.GetFriendDetailsById(Convert.ToInt32(Parameter));
                    DataContext = FriendEntity;
                    var birthday = DateTime.Now;

                    BirthdayText.Text = FriendEntity.Birthday.HasValue
                        ? AppResources.BirthdayOnLabel + " : " + FriendEntity.Birthday.Value.ToString("dd MMM")
                        : AppResources.BirthdayOnLabel + " : " + AppResources.NotKnownLabel;

                    //update the UI based on the action
                    UpdateUIForAction(Action);

                    AutoEmailToggle.IsChecked = FriendEntity.SendAutoEmailOnBirthday;
                    AutoEmailToggle.Content = (FriendEntity.SendAutoEmailOnBirthday) ? "Yes" : "No";

                    //format datetime to current culture
                    BirthdayPicker.Value = new DateTime(birthday.Year, birthday.Month, birthday.Day, CultureInfo.CurrentCulture.Calendar);

                    //add buttons to app bar
                    AddAppBarButtons(FriendEntity);
                }
                else
                {
                    Action = ActionType.Add;
                    ActivateSaveButton();
                }

                //remove backstack
                while (NavigationService.BackStack.Any() && !NavigationService.BackStack.ElementAt(0).Source.ToString().Contains("/MainPage.xaml"))
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error On Navigation", MessageBoxButton.OK);
            }
        }
        public static void MigrateXmlDataToDatabase()
        {
            var friendList = BirthdayUtility.GetFriendBirthdayList();

            if (friendList != null && friendList.Count > 0)
            {
                using (var context = new BirthdayDataContext(Database.DbConnectionString))
                {
                    foreach (var friend in friendList)
                    {
                        var dbFriend = new FriendEntity
                        {
                            BigProfilePictureUrl = friend.BigProfilePictureUrl,
                            Birthday =
                                string.IsNullOrEmpty(friend.Birthday)
                                    ? new DateTime?()
                                    : Convert.ToDateTime(friend.Birthday),
                            Email = friend.Email,
                            FacebookId = friend.Email,
                            IsReminderCreated = false,
                            IsHidden = friend.isHidden,
                            LastToastRaisedYear = friend.LastToastRaisedYear,
                            Name = friend.Name,
                            PhoneNumber = friend.PhoneNumber,
                            ProfilePictureLocation = friend.ProfilePictureLocation,
                            ProfilePictureUrl = friend.ProfilePictureUrl,
                            SendAutoEmailOnBirthday = friend.SendAutoEmailOnBirthday,
                            TypeOfContact = friend.TypeOfContact
                        };

                        context.Friends.InsertOnSubmit(dbFriend);
                    }

                    context.SubmitChanges();
                }
            }

            CleanUpOldFiles();
        }
 /// <summary>
 /// Update the saved friend details with the new details downloaded
 /// </summary>
 /// <param name="downloadedFriend">The newly downloaded friend object</param>
 /// <param name="existingFriend">The existing friend details stored locally</param>
 private static void UpdateExistingFriendToList(FriendEntity downloadedFriend, FriendEntity existingFriend)
 {
     //update the properties of existing friend
     existingFriend.Birthday = downloadedFriend.Birthday;
     existingFriend.Name = downloadedFriend.Name;
     existingFriend.ProfilePictureUrl = downloadedFriend.ProfilePictureUrl;
 }
        private static async Task<List<FriendEntity>> DownloadFriends(String accessToken)
        {
            var facebookFriendList = new List<FriendEntity>();
            var client = new Facebook.FacebookClient(accessToken);
            dynamic result = await client.GetTaskAsync("fql", new
            {
                q = "SELECT uid,name,pic_big,birthday_date FROM user WHERE uid in (SELECT uid2 FROM friend where uid1 = me());"
            });
            //JArray resultsList = JObject.Parse(result.ToString())["data"];
            var resultsList = result["data"];
            for (var i = 0; i < resultsList.Count; i++)
            {
                try
                {
                    var friendObject = resultsList[i];

                    var friend = new FriendEntity
                    {
                        FacebookId = friendObject.uid.ToString(),
                        Name = friendObject.name,
                        ProfilePictureUrl = friendObject.pic_big,
                        Birthday =
                            (friendObject.birthday_date == null)
                                ? String.Empty
                                : BirthdayUtility.FormatFBUserBirthday(friendObject.birthday_date.ToString())
                    };

                    facebookFriendList.Add(friend);
                }
                catch (Exception ex)
                {
                    AppLog.WriteToLog(DateTime.Now, "Error getting friend data from object" + ex.Message + ex.StackTrace + ex.InnerException + ex.Data, LogLevel.Error);
                }
            }

            return facebookFriendList;
        }
        private void SaveButtonClick(object sender, EventArgs e)
        {
            try
            {
                if (App.IsTrial)
                {
                    //messagebox to prompt to buy
                    var buyAppForSaveFriendMessageBox = new CustomMessageBox
                    {
                        Height = 300,
                        Caption = AppResources.BuyFullVersion,
                        Message = AppResources.BuyAppForAddFriend,
                        LeftButtonContent = AppResources.BuyLabel,
                        RightButtonContent = AppResources.LaterLabel,
                        VerticalAlignment = VerticalAlignment.Center
                    };

                    buyAppForSaveFriendMessageBox.Dismissed += BuyAppForSaveFriendBoxDismissed;
                    buyAppForSaveFriendMessageBox.Show();

                    return;
                }
                if (String.IsNullOrEmpty(NameTextBox.Text) || !BirthdayPicker.Value.HasValue)
                {
                    MessageBox.Show(AppResources.WarnNameBdayCompulsory, AppResources.WarnDtlsReq, MessageBoxButton.OK);
                    return;
                }

                //display status message
                StatusText.Text = AppResources.SavingFrndDtls;
                ContentStackPanel.Opacity = 0.2;
                StatusPanel.Visibility = Visibility.Visible;
                int friendGuid;

                if (Action.Equals(ActionType.Add))
                {
                    friendGuid = AddNewFriend();
                    MessageBox.Show(AppResources.NewFrndCreatedMsg, AppResources.NewFrndAddedTitle, MessageBoxButton.OK);
                }
                else
                {
                    friendGuid = UpdateFriend();
                    MessageBox.Show(AppResources.FrndDtlsUpdated, AppResources.FrndDtlsUpdatedTitle, MessageBoxButton.OK);
                }

                //change the app bar buttons
                FriendEntity = BirthdayUtility.GetFriendDetailsById(friendGuid);
                AddAppBarButtons(FriendEntity);

                //remove focus from textbox
                RemoveFocus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, AppResources.ErrSavingFrndDtls, MessageBoxButton.OK);
            }
            finally
            {
                //hide status screen
                StatusPanel.Visibility = Visibility.Collapsed;
                ContentStackPanel.Opacity = 1;
            }
        }
        private int AddNewFriend()
        {
            //gather friend details
            var friendDetails = new FriendEntity
            {
                Name = NameTextBox.Text,
                Birthday =
                    (BirthdayPicker.Value.HasValue)
                        ? BirthdayPicker.Value.Value
                        : new DateTime?(),
                Email = EmailTextBox.Text,
                PhoneNumber = ContactTextBox.Text,
                SendAutoEmailOnBirthday = AutoEmailToggle.IsChecked != null && AutoEmailToggle.IsChecked.Value,
                ProfilePictureUrl = Constants.DefaultProfilePic,
                TypeOfContact = ContactType.Custom
            };

            //write updated details to db
            return BirthdayUtility.AddNewFriend(friendDetails);
        }
        private void AddAppBarButtons(FriendEntity friendDetails)
        {
            ApplicationBar.Buttons.Clear();
            IsSaveButtonEnabled = false;

            if (friendDetails.TypeOfContact == ContactType.Facebook)
            {
                var postToWallButton = new ApplicationBarIconButton
                {
                    IconUri = new Uri("/Assets/Images/FbButtonIcon_48x48 copy.PNG", UriKind.Relative),
                    Text = AppResources.PostToWall
                };

                postToWallButton.Click += PostToWallButtonClick;
                ApplicationBar.Buttons.Add(postToWallButton);
            }

            if (!string.IsNullOrEmpty(friendDetails.Email))
            {
                var sendEmailButton = new ApplicationBarIconButton
                {
                    IconUri = new Uri("/Assets/Images/EmailButtonIcon_48x48.png", UriKind.Relative),
                    Text = AppResources.EmailLabel
                };

                sendEmailButton.Click += SendEmailButtonClick;
                ApplicationBar.Buttons.Add(sendEmailButton);
            }

            if (!string.IsNullOrEmpty(friendDetails.PhoneNumber))
            {
                var callButton = new ApplicationBarIconButton
                {
                    IconUri = new Uri("/Assets/AppBar/feature.phone.png", UriKind.Relative),
                    Text = AppResources.CallLabel
                };

                callButton.Click += CallButtonClick;
                ApplicationBar.Buttons.Add(callButton);

                var sendMessageButton = new ApplicationBarIconButton
                {
                    IconUri = new Uri("/Assets/AppBar/feature.email.png", UriKind.Relative),
                    Text = AppResources.MessageLabel
                };

                sendMessageButton.Click += SendMessageButtonClick;
                ApplicationBar.Buttons.Add(sendMessageButton);
            }

            ApplicationBar.IsVisible = ApplicationBar.Buttons.Count>0;
        }
        /// <summary>
        /// Add a new friend
        /// </summary>
        /// <param name="friendDetails">The details of the friend to be added</param>
        /// <returns>The Guid of the new friend</returns>
        public static int AddNewFriend(FriendEntity friendDetails)
        {
            using (var context =new BirthdayDataContext(Database.DbConnectionString))
            {
                friendDetails.SendAutoEmailOnBirthday = true;
                friendDetails.IsReminderCreated = false;
                friendDetails.IsHidden = false;
                friendDetails.LastToastRaisedYear = 1900;

                context.Friends.InsertOnSubmit(friendDetails);
                context.SubmitChanges();

                return friendDetails.UniqueId;
            }
        }
 /// <summary>
 /// Update the properties if the freind details
 /// </summary>
 /// <param name="existingFriend">Existing friend details</param>
 /// <param name="updatedFriend">Friend details with updated properties</param>
 private static void CopyProperties(FriendEntity existingFriend, FriendEntity updatedFriend)
 {
     existingFriend.BigProfilePictureUrl = updatedFriend.BigProfilePictureUrl;
     existingFriend.Birthday = updatedFriend.Birthday;
     existingFriend.Email = updatedFriend.Email;
     existingFriend.FacebookId = updatedFriend.FacebookId;
     existingFriend.IsHidden = updatedFriend.IsHidden;
     existingFriend.IsReminderCreated = updatedFriend.IsReminderCreated;
     existingFriend.LastToastRaisedYear = updatedFriend.LastToastRaisedYear;
     existingFriend.Name = updatedFriend.Name;
     existingFriend.PhoneNumber = updatedFriend.PhoneNumber;
     existingFriend.ProfilePictureLocation = updatedFriend.ProfilePictureLocation;
     existingFriend.ProfilePictureUrl = updatedFriend.ProfilePictureUrl;
     existingFriend.SendAutoEmailOnBirthday = updatedFriend.SendAutoEmailOnBirthday;
     existingFriend.TypeOfContact = updatedFriend.TypeOfContact;
     existingFriend.UniqueId = updatedFriend.UniqueId;
 }
        /// <summary>
        /// Updates the details of a particulat friend
        /// </summary>
        /// <param name="friendDetails">The details of the friend to update</param>
        public static void UpdateFriendDetails(FriendEntity friendDetails)
        {  
            using (var context = new BirthdayDataContext(Database.DbConnectionString))
            {
                var existingFriend = (from friend in context.Friends
                    where friend.UniqueId == friendDetails.UniqueId
                    select friend).FirstOrDefault();

                CopyProperties(existingFriend, friendDetails);
                //considering the scenario where the data should be automatically updated as reference has been updated
                context.SubmitChanges();
            }
        }