public ManageTrip(ParentViewModel parentViewModel)
        {
            this.InitializeComponent();
            this.tbTitle.Text = "Update Trip";

            InitializeFlyout();
            this.parentViewModel = parentViewModel;

            this.txtTitle.Text = parentViewModel.Title;
            this.txtDescription.Text = parentViewModel.Description;
            this.txtCost.Text = parentViewModel.Cost.ToString();
            cboYears.SelectedValue = parentViewModel.StartDate.Year;

            cboMonths.SelectedValue = this.cboMonths.Items[parentViewModel.StartDate.Month - 1];
            cboDays.SelectedValue = parentViewModel.StartDate.Day;
        }
        public void CreateFake()
        {
            ParentViewModel fake;

            ParentViewModel fakeTrip = this.Items.FirstOrDefault(p => p.Identifier == -1);
            if (fakeTrip != null)
                this.Items.Remove(fakeTrip);

            fake = new ParentViewModel()
            {
                Identifier = -1,
                Title = "New parent?",
                Description = "Click here to add a parent",
                LocalPathImage = loader.GetString("NewParentImage")
            };

            fake.ItemGroups.Clear();
            fake.ItemGroups = new ObservableCollection<CategoryViewModel>();
            this.Items.Add(fake);
        }
        public async Task<int> SaveParentAsync(ParentViewModel trip)
        {
            SQLite.SQLiteAsyncConnection context = new SQLite.SQLiteAsyncConnection(connectionString);

            Item newItem = new Item()
            {
                Identifier = trip.Identifier,
                Title = trip.Title,
                Description = trip.Description,
                Cost = trip.Cost,
                StartDate = trip.StartDate,
                Latitude = trip.Latitude,
                Longitude = trip.Longitude,
                User = trip.User.ToString(),
                Image = trip.LocalPathImage
            };

            await context.InsertAsync(newItem);
            return newItem.Identifier;
        }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        async protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // Allow saved page state to override the initial item to display
            if (pageState != null && pageState.ContainsKey("SelectedItem"))
            {
                navigationParameter = pageState["SelectedItem"];
            }

            // TODO: Create an appropriate data model for your problem domain to replace the sample data
            selectedTrip = await MainViewModel.GetFullTrip((Int32)navigationParameter);
            RemoveFakeTrip(selectedTrip);

            this.DefaultViewModel["ItemGroups"] = selectedTrip.ItemGroups;
            this.DefaultViewModel["Trips"] = selectedTrip.Group.Items;

            if (SecondaryTile.Exists("SuggesMePin_" + selectedTrip.Identifier.ToString()))
                this.btnPin.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                        
        }
        private static void RemoveFakeTrip(ParentViewModel item)
        {
            ParentViewModel fake = item.Group.Items[item.Group.Items.Count - 1];

            if (fake.Identifier == -1)
                item.Group.Items.Remove(fake);
        }
 public static void SetSelectedTrip(ParentViewModel trip)
 {
     _mainViewModel.SelectedTrip = trip;
     trip.SelectedItems = new ObservableCollection<ItemViewModel>();
 }
 private static void CreateFakeItem(ParentViewModel trip)
 {
     CategoryViewModel category = trip.CreateCategory(Category.Things);
     if (category.Items.Count == 0)
     {
         ItemViewModel item = new ItemViewModel();
         item.Identifier = -1;
         item.Title = "Note";
         item.Description = "Add items from the botton toolbar";
         item.Group = category;
         item.Trip = trip;
         category.Items.Add(item);
     }
 }
        async public static Task<int> SaveTripAsync(ParentViewModel trip)
        {
            int tripIdentifier = -1;

            if (trip.Identifier == -1)
            {
                tripIdentifier = await _model.SaveParentAsync(trip);

                if (tripIdentifier != -1)
                {
                    _mainViewModel.AllGroups[0].Items.Add(trip);
                    _mainViewModel.AllGroups[0].CreateFake();
                }
            }
            else
            {
                await _model.UpdateParent(trip);
                tripIdentifier = trip.Identifier;
            }

            return tripIdentifier;
        }
        async private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.txtStatusMessage.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.txtStatusMessage.Text = "Please wait...";

                if (AreFieldsValid())
                {
                    if (parentViewModel == null)
                    {
                        ParentViewModel newTrip = new ParentViewModel()
                        {
                            Identifier = -1,
                            Title = this.txtTitle.Text,
                            LocalPathImage = loader.GetString("DefaultParentImage"),
                            Group = MainViewModel.GetGroup(0),
                            Description = this.txtDescription.Text,
                            Cost = Convert.ToInt64(this.txtCost.Text),
                            User = MainViewModel.GetTraveler(),
                            StartDate = new DateTime(
                                Convert.ToInt32(cboYears.SelectedItem),
                                Convert.ToInt32(((PairViewModel)cboMonths.SelectedItem).Identifier + 1),
                                Convert.ToInt32(cboDays.SelectedItem))
                        };

                        newTrip.Identifier = await MainViewModel.SaveTripAsync(newTrip);

                        this.txtStatusMessage.Text = "Trip has been created";
                    }
                    else
                    {
                            parentViewModel.Title = this.txtTitle.Text;
                            parentViewModel.Description = this.txtDescription.Text;
                            parentViewModel.Cost = Convert.ToInt64(this.txtCost.Text);
                            parentViewModel.StartDate = new DateTime(
                                Convert.ToInt32(cboYears.SelectedItem),
                                Convert.ToInt32(((PairViewModel)cboMonths.SelectedItem).Identifier + 1),
                                Convert.ToInt32(cboDays.SelectedItem));

                            await MainViewModel.SaveTripAsync(parentViewModel);
                            this.txtStatusMessage.Text = "Trip has been updated";
                    }
                    
                    this.backButton_Click(sender, e);
                }
            }
            catch (Exception)
            {
                this.txtStatusMessage.Text = "Something is wrong, please review the fields and try again";
            }
        }
        async public Task<ParentViewModel> GetParentAsync(int identifier)
        {
            SQLite.SQLiteAsyncConnection context = new SQLite.SQLiteAsyncConnection(connectionString);

            ParentViewModel trip = new ParentViewModel();
            //trips have the parent null, items have a trip as parent
            Item item = await context.Table<Item>().Where(p => p.Identifier == identifier && p.Parent == 0).FirstOrDefaultAsync();

            trip = new ParentViewModel()
            {
                Title = item.Title,
                Description = item.Description,
                StartDate = Convert.ToDateTime(item.StartDate),
                Category = (Int16)Generic.UI.Logic.Enumerations.Category.Trips,
                Cost = item.Cost,
                Identifier = item.Identifier,
                Latitude = item.Latitude,
                Longitude = item.Longitude,
                LocalPathImage = item.Image == null ? loader.GetString("DefaultParentImage") : item.Image,
                User = new Guid(item.User)
            };

            return trip;
        }
        async public Task UpdateParent(ParentViewModel trip)
        {
            SQLite.SQLiteAsyncConnection context = new SQLite.SQLiteAsyncConnection(connectionString);

            List<ItemViewModel> result = new List<ItemViewModel>();
            Item item = await context.Table<Item>().Where(p => p.Identifier == trip.Identifier).FirstOrDefaultAsync();

            item.Image = trip.LocalPathImage;
            item.Title = trip.Title;
            item.Description = trip.Description;
            item.Cost = trip.Cost;
            item.StartDate = trip.StartDate;

            if (item != null)
            {
                await context.UpdateAsync(item);
            }
        }