using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Shell;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Sonex;

/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.ExtendsContentIntoTitleBar = true; // Extend the content into the title bar and hide the default title bar
        this.AppWindow.TitleBar.PreferredHeightOption = Microsoft.UI.Windowing.TitleBarHeightOption.Tall;
        this.SetTitleBar(titleBar); // Set the custom title bar
        this.AppWindow.SetIcon("Assets/Tiles/GalleryIcon.ico");
        navView.SelectedItem = navView.MenuItems.OfType<NavigationViewItem>().First();
        navFrame.Navigated += NavFrame_Navigated;
        this.Activated += MainWindow_Activated;
        string iconPath = System.IO.Path.Combine(AppContext.BaseDirectory, "Assets\\AppIcon.ico");
        this.AppWindow.SetIcon(iconPath);
    }

    private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
    {
    }

    private void NavFrame_Navigated(object sender, NavigationEventArgs e)
    {
        var pageName = e.SourcePageType?.Name;
        var item = FindItemByTag(navView.MenuItems, pageName??"");

        if (item != null)
            navView.SelectedItem = item;
    }

    private void TitleBar_PaneToggleRequested(TitleBar sender, object args)
    {
        navView.IsPaneOpen = !navView.IsPaneOpen;
    }

    private void TitleBar_BackRequested(TitleBar sender, object args)
    {
        if (navFrame.CanGoBack)
        {
            navFrame.GoBack(new SuppressNavigationTransitionInfo());
        }
    }

    private void navView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
    {
        if (args.SelectedItem == null) return;

        var item = (NavigationViewItem)args.SelectedItem;
        var pageType = Type.GetType("Sonex.Pages." + (string)item.Tag);

        if (pageType != null)
        {
            navFrame.Navigate(pageType, null, new SuppressNavigationTransitionInfo());
        }
    }

    private static NavigationViewItem? FindItemByTag(IList<object> items, string tag)
    {
        foreach (var obj in items)
        {
            if (obj is NavigationViewItem nvi)
            {
                if ((nvi.Tag as string) == tag) return nvi;
                var sub = FindItemByTag(nvi.MenuItems, tag);
                if (sub != null) return sub;
            }
        }
        return null;
    }
    
}
