using C4IT.FASD.Base;
using C4IT.Logging;
using C4IT.MultiLanguage;
using C4IT.F4SD.TAPI;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Pages.CustomMessageBox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static C4IT.Logging.cLogManager;
using FasdDesktopUi.Pages.SettingsPage;

namespace FasdDesktopUi.Pages.PhoneSettingsPage
{
    public partial class PhoneSettingsPage : SettingsPageBase
    {
        #region Properties

        private static PhoneSettingsPage _Instance = null;

        public static PhoneSettingsPage Instance { get
            {
                return _Instance ?? (_Instance = new PhoneSettingsPage());
            }
        }

        #region IsPhoneSupportDisabled
        private bool _IsPhoneSupportDisabled = false;

        public bool IsPhoneSupportDisabled
        {
            get { return _IsPhoneSupportDisabled; }
            set
            {
                if (_IsPhoneSupportDisabled != value)
                {
                    _IsPhoneSupportDisabled = value;
                    OnPropertyChanged(nameof(IsPhoneSupportDisabled));
                    UpdateEnableStatus();
                }
            }
        }
        #endregion

        #region IsNativeSwyxItPreferred
        private bool _IsNativeSwyxItPreferred = false;

        public bool IsNativeSwyxItPreferred
        {
            get { return _IsNativeSwyxItPreferred; }
            set
            {
                if (_IsNativeSwyxItPreferred != value)
                {
                    _IsNativeSwyxItPreferred = value;
                    OnPropertyChanged(nameof(IsNativeSwyxItPreferred));
                    UpdateEnableStatus();
                }
            }
        }

        #endregion

        #region IsBitMode32
        private bool _UseTapi32Bit = false;

        public bool UseTapi32Bit
        {
            get { return _UseTapi32Bit; }
            set
            {
                if (_UseTapi32Bit != value)
                {
                    _UseTapi32Bit = value;
                    OnPropertyChanged(nameof(UseTapi32Bit));
                    UpdateEnableStatus();
                }
            }
        }
        #endregion

        #region SignalOutgoingCalls
        private bool _boolSignalOutgoingCalls = false;

        public bool boolSignalOutgoingCalls
        {
            get { return _boolSignalOutgoingCalls; }
            set
            {
                if (_boolSignalOutgoingCalls != value)
                {
                    _boolSignalOutgoingCalls = value;
                    OnPropertyChanged(nameof(boolSignalOutgoingCalls));
                    UpdateEnableStatus();
                }
            }
        }

        #endregion

        #region ShowUnresolvedPhoneNumbers
        private bool _boolShowUnresolvedPhoneNumbers = false;

        public bool boolShowUnresolvedPhoneNumbers
        {
            get { return _boolShowUnresolvedPhoneNumbers; }
            set
            {
                if (_boolShowUnresolvedPhoneNumbers != value)
                {
                    _boolShowUnresolvedPhoneNumbers = value;
                    OnPropertyChanged(nameof(boolShowUnresolvedPhoneNumbers));
                    UpdateEnableStatus();
                }
            }
        }
        #endregion

        #region ExternalCallPrefix
        private string _strExternalCallPrefix = string.Empty;

        public string strExternalCallPrefix
        {
            get { return _strExternalCallPrefix; }
            set
            {
                if (_strExternalCallPrefix != value)
                {
                    _strExternalCallPrefix = value;
                    OnPropertyChanged(nameof(strExternalCallPrefix));
                    UpdateEnableStatus();
                }
            }
        }

        #endregion

        #endregion

        private PhoneSettingsPage()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            try
            {
                IsPhoneSupportDisabled = cFasdCockpitConfig.Instance.PhoneSupport.DisablePhoneSupport;
                IsNativeSwyxItPreferred = cFasdCockpitConfig.Instance.PhoneSupport.PreferSwyxitNative;
                UseTapi32Bit = cFasdCockpitConfig.Instance.PhoneSupport.UseTapi32Bit;
                boolSignalOutgoingCalls = cFasdCockpitConfig.Instance.PhoneSupport.SignalOutgoingCalls;
                strExternalCallPrefix = cFasdCockpitConfig.Instance.PhoneSupport.ExternalCallPrefix;
                boolShowUnresolvedPhoneNumbers = cFasdCockpitConfig.Instance.PhoneSupport.ShowUnresolvedPhoneNumbers;

                UpdateTapiLines();
                UpdateEnableStatus();

                base.OnInitialized(e);
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void PhoneSettingsWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            UpdateEnableStatus();
            BlurInvoker_IsActiveChanged(sender, e);
        }

        private void UpdateTapiLines()
        {
            try
            {
                if (PreferredTapiLineComboBox.Items?.Count > 0)
                    return;

                var tapiLines = C4TapiHelper.Lines;

                if (tapiLines?.Count > 0)
                {
                    foreach (var line in tapiLines)
                    {
                        var comboBoxItem = new ComboBoxItem() { Content = line };

                        if (tapiLines?.First() == line && tapiLines?.Last() == line)
                            comboBoxItem.SetResourceReference(StyleProperty, "ComboBoxSingleItem");
                        else if (tapiLines?.First() == line)
                            comboBoxItem.SetResourceReference(StyleProperty, "ComboBoxFirstItem");
                        else if (tapiLines?.Last() == line)
                            comboBoxItem.SetResourceReference(StyleProperty, "ComboBoxLastItem");

                        PreferredTapiLineComboBox.Items.Add(comboBoxItem);
                    }
                }
                else
                {
                    PreferredTapiLineComboBox.IsEnabled = false;
                }

                var currentLineIndex = tapiLines.IndexOf(cFasdCockpitConfig.Instance.PhoneSupport.PreferedTapiLine);
                PreferredTapiLineComboBox.SelectedIndex = currentLineIndex;
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void UpdateEnableStatus()
        {
            try
            {
                UpdateTapiLines();

                SwyxNativeCheck.ClearValue(ToolTipProperty);
                UseTapi32BitCheck.ClearValue(ToolTipProperty);
                PreferredTapiLineComboBox.ClearValue(ToolTipProperty);


                SwyxNativeCheck.IsEnabled = true;

                PreferredTapiLineComboBox.IsEnabled = !IsNativeSwyxItPreferred;

                if (IsNativeSwyxItPreferred)
                    PreferredTapiLineComboBox.ToolTip = cMultiLanguageSupport.GetItem("PhoneSettings.Tapi.Disabled.SwyxItNativeEnabled");

                if (IsPhoneSupportDisabled)
                {
                    SwyxNativeCheck.IsEnabled = false;
                    SwyxNativeCheck.ToolTip = cMultiLanguageSupport.GetItem("PhoneSettings.Tapi.Disabled.PhoneSupportDisabled");

                    UseTapi32BitCheck.IsEnabled = false;
                    PreferredTapiLineComboBox.IsEnabled = false;
                    PreferredTapiLineComboBox.ToolTip = cMultiLanguageSupport.GetItem("PhoneSettings.Tapi.Disabled.PhoneSupportDisabled");
                    UseTapi32BitCheck.ToolTip = cMultiLanguageSupport.GetItem("PhoneSettings.Tapi.Disabled.PhoneSupportDisabled");
                }

                if (C4TapiHelper.Lines?.Count <= 0)
                {
                    UseTapi32BitCheck.IsEnabled = false;
                    PreferredTapiLineComboBox.IsEnabled = false;
                    PreferredTapiLineComboBox.ToolTip = cMultiLanguageSupport.GetItem("PhoneSettings.Tapi.Disabled.NoLine");
                    UseTapi32BitCheck.ToolTip = cMultiLanguageSupport.GetItem("PhoneSettings.Tapi.Disabled.NoLine");
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        #region DialogButton_Click

        public void DialogButton_Click(object sender)
        {
            if (!(sender is FrameworkElement senderElement))
                return;

            if (!(senderElement.Tag is bool tagValue))
                return;

            if (tagValue)
            {
                cFasdCockpitConfig.Instance.PhoneSupport.DisablePhoneSupport = IsPhoneSupportDisabled;
                cFasdCockpitConfig.Instance.PhoneSupport.PreferSwyxitNative = IsNativeSwyxItPreferred;
                cFasdCockpitConfig.Instance.PhoneSupport.UseTapi32Bit = UseTapi32Bit;
                cFasdCockpitConfig.Instance.PhoneSupport.SignalOutgoingCalls = boolSignalOutgoingCalls;
                cFasdCockpitConfig.Instance.PhoneSupport.ExternalCallPrefix = ExternalCallPrefix.Text;
                cFasdCockpitConfig.Instance.PhoneSupport.ShowUnresolvedPhoneNumbers = boolShowUnresolvedPhoneNumbers;

                if (PreferredTapiLineComboBox.SelectedItem is ComboBoxItem selectedItem)
                    cFasdCockpitConfig.Instance.PhoneSupport.PreferedTapiLine = selectedItem.Content?.ToString();

                cFasdCockpitConfig.Instance.Save("PhoneSupport\\DisablePhoneSupport");
                cFasdCockpitConfig.Instance.Save("PhoneSupport\\PreferSwyxitNative");
                cFasdCockpitConfig.Instance.Save("PhoneSupport\\UseTapi32Bit");
                cFasdCockpitConfig.Instance.Save("PhoneSupport\\PreferedTapiLine");
                cFasdCockpitConfig.Instance.Save("PhoneSupport\\SignalOutgoingCalls");
                cFasdCockpitConfig.Instance.Save("PhoneSupport\\ExternalCallPrefix");
                cFasdCockpitConfig.Instance.Save("PhoneSupport\\ShowUnresolvedPhoneNumbers");
            }

            Close();

            if (tagValue)
            {
                var _h = Dispatcher.Invoke(async () =>
                {
                    await Task.Delay(300);

                    var dialogResult = CustomMessageBox.CustomMessageBox.Show(cMultiLanguageSupport.GetItem("PhoneSettings.RestartDialog.Text"), cMultiLanguageSupport.GetItem("PhoneSettings.RestartDialog.Caption"), enumHealthCardStateLevel.Info, null, true);

                    if (dialogResult == true)
                    {
                        System.Windows.Forms.Application.Restart();
                        Application.Current.Shutdown();
                    }
                });
            }
        }

        private void DialogButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            DialogButton_Click(sender);
        }

        private void DialogButton_TouchDown(object sender, TouchEventArgs e)
        {
            DialogButton_Click(sender);
        }

        #endregion

        #region CloseButton_Click

        private void CloseButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Close();
        }

        private void CloseButton_TouchDown(object sender, TouchEventArgs e)
        {
            Close();
        }

        #endregion

        private void PhoneSettingsWindow_Closed(object sender, EventArgs e)
        {
            _Instance = null;
        }

    }
}
