using BetterLyrics.WinUI3.Helper;
using BetterLyrics.WinUI3.Services.SettingsService;
using BetterLyrics.WinUI3.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using Microsoft.UI.Xaml;
using System;
using WinUIEx;

namespace BetterLyrics.WinUI3.ViewModels
{
    public partial class SystemTrayViewModel(ISettingsService settingsService) : BaseViewModel, IRecipient<PropertyChangedMessage<bool>>
    {
        private readonly ISettingsService _settingsService = settingsService;

        [ObservableProperty]
        [NotifyPropertyChangedRecipients]
        public partial bool IsLyricsWindowLocked { get; set; } = false;

        [ObservableProperty]
        public partial string ToolTipText { get; set; } = Constants.App.AppName;

        public void Receive(PropertyChangedMessage<bool> message)
        {
            if (message.Sender is LyricsWindowViewModel)
            {
                if (message.PropertyName == nameof(LyricsWindowViewModel.IsLyricsWindowLocked))
                {
                    if (IsLyricsWindowLocked != message.NewValue)
                    {
                        IsLyricsWindowLocked = message.NewValue;
                    }
                }
            }
        }

        [RelayCommand]
        private static void ExitApp()
        {
            WindowHelper.ExitApp();
        }

        [RelayCommand]
        private static void RestartApp()
        {
            WindowHelper.RestartApp();
        }

        [RelayCommand]
        private static void ResetWindowPosition()
        {
            LyricsWindow? lyricsWindow = WindowHelper.GetWindowByWindowType<LyricsWindow>();
            if (lyricsWindow != null)
            {
                lyricsWindow.MoveAndResize(0, 0, 800, 600);
            }
        }

        [RelayCommand]
        private static void OpenSettings()
        {
            WindowHelper.OpenWindow<SettingsWindow>();
        }

        [RelayCommand]
        private static void OpenMusicGallery()
        {
            WindowHelper.OpenWindow<MusicGalleryWindow>();
        }

        [RelayCommand]
        private static void OpenLyrics()
        {
            WindowHelper.OpenWindow<LyricsWindow>();
        }

        [RelayCommand]
        private void UnlockWindow()
        {
            var window = WindowHelper.GetWindowByWindowType<LyricsWindow>();
            if (window == null) return;

            DesktopModeHelper.SetClickThrough(window, false);
            IsLyricsWindowLocked = false;
        }
    }
}
