using FSI.BT.Tools.Global.Commands;
using FSI.BT.Tools.RadialMenu;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

using FSI.Lib.Wpf.ExtensionMethods;

namespace FSI.BT.Tools.RadialMenu.UserInterface
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class FrmRadialMenu : Window//, INotifyPropertyChanged
    {
        private CmdCommand _cmd;
        private MainViewModel _radialMenu;

        public FrmRadialMenu()
        {
            InitializeComponent();

            List<string> cmds = new List<string>();
            foreach (var cmd in Global.Vars.GlobalSettings.Cmds)
            {
                cmds.Add(cmd.Cmd);
            }

            cmds = cmds.Distinct().ToList(); // Duplikate entfernen
            cmds.Sort(); // Liste sortieren

            _radialMenu = new MainViewModel(this, cmds);
            DataContext = _radialMenu;

            tbversion.Text = "v" + Assembly.GetExecutingAssembly().GetName().Version.Major + "." + Assembly.GetExecutingAssembly().GetName().Version.Minor + "b";
            _cmd = new();
        }

        private void Window_Activated(object sender, EventArgs e)
        {            
            ChangeBtnIcon();
            tbCmd.Focus();
        }

        private void Window_Deactivated(object sender, EventArgs e)
        {
            tbCmd.Text = String.Empty;
            tbCmd.Focus();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ChangeBtnIcon();

            tbCmd.Focus();
        }

        private void tbCmd_KeyDown(object sender, KeyEventArgs e)
        {
            if (_cmd.CanExecute(tbCmd.Text))
                tbCmd.Background = new SolidColorBrush(Colors.Green);
            else
                tbCmd.Background = new SolidColorBrush(Colors.White);

            if (e.Key == Key.Enter && _cmd.CanExecute(tbCmd.Text))
            {
                _cmd.Execute(tbCmd.Text);
            }
        }

        private void btnMute_Click(object sender, RoutedEventArgs e)
        {
            Lib.Audio.AudioManager.SetMasterVolumeMute(!Lib.Audio.AudioManager.GetMasterVolumeMute());
            ChangeBtnIcon();
        }

        private void btnVolUp_Click(object sender, RoutedEventArgs e)
        {
            Lib.Audio.AudioManager.StepMasterVolume(2F);
        }

        private void btnVolDwn_Click(object sender, RoutedEventArgs e)
        {
            Lib.Audio.AudioManager.StepMasterVolume(-2F);
        }

        private void ChangeBtnIcon()
        {
            if (FSI.Lib.Audio.AudioManager.GetMasterVolumeMute())
            {
                btnMute.Content = new System.Windows.Controls.Image
                {
                    Source = new BitmapImage(new Uri("/Global/Icons/VolOff.png", UriKind.RelativeOrAbsolute)),
                    Width = 15,
                };
            }
            else
            {
                btnMute.Content = new System.Windows.Controls.Image
                {
                    Source = new BitmapImage(new Uri("/Global/Icons/VolOn.png", UriKind.RelativeOrAbsolute)),
                    Width = 15,
                };
            }

            btnVolUp.Content = new System.Windows.Controls.Image
            {
                Source = new BitmapImage(new Uri("/Global/Icons/VolUp.png", UriKind.RelativeOrAbsolute)),
                Width = 15,
            };

            btnVolDwn.Content = new System.Windows.Controls.Image
            {
                Source = new BitmapImage(new Uri("/Global/Icons/VolDown.png", UriKind.RelativeOrAbsolute)),
                Width = 15,
            };
        }

        private void tbCmd_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {

        }
    }


}
