using PlantBox.Client.Models;
using PlantBox.Client.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace PlantBox.Client.Forms
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : MasterDetailPage
    {
        public MainPage()
        {
            InitializeComponent();

            var master = new MenuPage() { Title = "PlantBox" };
            master.items.ItemTapped += Items_ItemTapped;

            Detail = new NavigationPage(new HomePage() { Title = Locale.HomePageTitle });
            Master = master;
        }

        private async void Items_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            if (e.Item is MenuPageItem item)
            {
                IsPresented = false;
                var page = item.Page;

                if (Detail.Navigation.NavigationStack.Count > 1 || page == null)
                {
                    await Detail.Navigation.PopToRootAsync();
                }

                if (page != null)
                {
                    page.Title = item.Title;
                    await Detail.Navigation.PushAsync(page);
                }
            }
        }
    }
}