using MySql.Data.MySqlClient;
using System;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;

namespace SkyTeam
{
    public partial class SettingsPage : Page
    {
        public SettingsPage()
        {
            InitializeComponent();

            var bgBrush = Application.Current.Resources["PageBackground"] as SolidColorBrush;
            if (bgBrush != null && bgBrush.Color == Color.FromRgb(30, 30, 30))
            {
                DarkModeToggle.IsChecked = true;
                DarkModeToggle.Content = "An";
            }
            else
            {
                DarkModeToggle.Content = "Aus";
            }
        }

        private void DarkModeToggle_Checked(object sender, RoutedEventArgs e)
        {
            SetRes("PageBackground", Color.FromRgb(30, 30, 30));
            SetRes("CardBackground", Color.FromRgb(45, 45, 45));
            SetRes("SidebarBackground", Color.FromRgb(20, 20, 20));
            SetRes("PrimaryText", Colors.White);
            SetRes("SecondaryText", Color.FromRgb(200, 200, 200));

            DarkModeToggle.Content = "An";
        }

        private void DarkModeToggle_Unchecked(object sender, RoutedEventArgs e)
        {
            SetRes("PageBackground", Colors.White);
            SetRes("CardBackground", Color.FromRgb(245, 247, 250));
            SetRes("SidebarBackground", Color.FromRgb(227, 242, 253));
            SetRes("PrimaryText", Colors.Black);
            SetRes("SecondaryText", Color.FromRgb(102, 102, 102));

            DarkModeToggle.Content = "Aus";
        }

        private void SetRes(string key, Color color)
        {
            Application.Current.Resources[key] = new SolidColorBrush(color);
        }

        private void DeleteAccountButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Möchten Sie Ihr Konto wirklich löschen?\nAlle Buchungen werden ebenfalls gelöscht.", "Achtung", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
                try
                {
                    using (MySqlConnection conn = new MySqlConnection(DatenbankServices.GetConnection()))
                    {
                        conn.Open();
                        string deleteBookings = "DELETE FROM buchungen WHERE UserId = @uid";
                        MySqlCommand cmd1 = new MySqlCommand(deleteBookings, conn);
                        cmd1.Parameters.AddWithValue("@uid", SessionManager.CurrentUserId);
                        cmd1.ExecuteNonQuery();

                        string deleteUser = "DELETE FROM users WHERE Id = @uid";
                        MySqlCommand cmd2 = new MySqlCommand(deleteUser, conn);
                        cmd2.Parameters.AddWithValue("@uid", SessionManager.CurrentUserId);
                        cmd2.ExecuteNonQuery();
                    }

                    SessionManager.CurrentUserId = 0;
                    MessageBox.Show("Ihr Konto wurde erfolgreich gelöscht.");
                    NavigationService.Navigate(new LogInPage());
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Fehler beim Löschen: " + ex.Message);
                }
            }
        }

        private void HomeButton_Click(object sender, RoutedEventArgs e) => NavigationService.Navigate(new NavigationPage());
        private void BookingsButton_Click(object sender, RoutedEventArgs e) => NavigationService.Navigate(new BuchungenPage());
        private void SettingsButton_Click(object sender, RoutedEventArgs e) => NavigationService.Navigate(new SettingsPage()); 
        private void LogoutButton_Click(object sender, RoutedEventArgs e)
        {
            SessionManager.CurrentUserId = 0; 
            NavigationService.Navigate(new LogInPage());
        }

        private void LanguageComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (LanguageComboBox.SelectedItem is ComboBoxItem selectedItem)
            {
                string culture = selectedItem.Tag.ToString();
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

                if (culture == "ar")
                    this.FlowDirection = FlowDirection.RightToLeft;
                else
                    this.FlowDirection = FlowDirection.LeftToRight;

                NavigationService.Navigate(new SettingsPage());
            }
        }
    }
}