using System;
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Input;
using Cogwheel;
using SessionZero.Cogwheel;
using SessionZero.Data.Sztl;
using SessionZero.Pages;

namespace SessionZero;

public partial class MainWindow : Window
{
    private Dictionary<string, IPageBase> _pages = new();
    
    private ContentControl? _mainContentControl;
    private ContentControl? _overlayContentControl;
    private Button? _homeButton;
    private Button? _sessionsButton;
    private Button? _libraryButton;
    private Button? _settingsButton;
    
    public MainWindow()
    {
        InitializeComponent();
        InitControls();
        InitPages();
        
        KeyDownEvent.AddClassHandler<TopLevel>(OnKeyDown, handledEventsToo: true);
        
        ChangePage("Home");
        
        AppManager.Settings.AddSetting(new AppSetting("test", typeof(int)));
        AppManager.Settings.SetSettingValue("test", 1);
    }
    
    public void ChangePage(string pageName)
    {
        if (_pages.TryGetValue(pageName.ToLower(), out var page))
        {
            _mainContentControl?.Content = page;
            page.Refresh();
        }
        else
        {
            COGWHEEL.LogError($"Page '{pageName}' does not exist");
        }
    }

    public void ToggleOverlay(bool visible)
    {
        _overlayContentControl?.IsVisible = visible;
        _overlayContentControl?.IsHitTestVisible = visible;
    }

    public void ToggleConsole(bool visible)
    {
        ToggleOverlay(visible);
        _overlayContentControl?.Content = visible ? AppManager.ConsoleControl : null;
    }
    
    private void OnKeyDown(TopLevel topLevel, KeyEventArgs keyEventArgs)
    {
        if (keyEventArgs.Key == Key.OemTilde && keyEventArgs.KeyModifiers.HasFlag(KeyModifiers.Control))
        {
            ToggleConsole(!_overlayContentControl.IsVisible);
        }
    }
    
    private void InitControls()
    {
        _mainContentControl = this.FindControl<ContentControl>("MainContentControl");
        _overlayContentControl = this.FindControl<ContentControl>("Overlay");
        _homeButton = this.FindControl<Button>("HomeButton");
        _sessionsButton = this.FindControl<Button>("SessionsButton");
        _libraryButton = this.FindControl<Button>("LibraryButton");
        _settingsButton = this.FindControl<Button>("SettingsButton");
        
        SetupButtonEvents();
    }
    
    private void InitPages()
    {
        var homePage = new HomePage();
        var sessionsPage = new SessionsPage();
        var libraryPage = new LibraryPage();
        var settingsPage = new SettingsPage();
        
        _pages.Add(homePage.PageName.ToLower(), homePage);
        _pages.Add(sessionsPage.PageName.ToLower(), sessionsPage);
        _pages.Add(libraryPage.PageName.ToLower(), libraryPage);
        _pages.Add(settingsPage.PageName.ToLower(), settingsPage);
    }

    private void SetupButtonEvents()
    {
        _homeButton?.Click += (_, _) => ChangePage("Home");
        _sessionsButton?.Click += (_, _) => ChangePage("Sessions");
        _libraryButton?.Click += (_, _) => ChangePage("Library");
        _settingsButton?.Click += (_, _) => ChangePage("Settings");
    }
    
    
    
}
