using System.Windows;
using ComputerShopWpf.Services;

namespace ComputerShopWpf.Views
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ApplyRole();
            ContentFrame.Navigate(new LateOrdersPage());
        }

        private void ApplyRole()
        {
            SessionTextBlock.Text = UserSession.Login + " | роль: " + GetRoleTitle(UserSession.RoleName);
            UsersButton.Visibility = UserSession.CanManageUsers() ? Visibility.Visible : Visibility.Collapsed;
        }

        private void ClientsButton_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(new ClientsPage());
        }

        private void ProductsButton_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(new ProductsPage());
        }

        private void OrdersButton_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(new OrdersPage());
        }

        private void LateOrdersButton_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(new LateOrdersPage());
        }

        private void UsersButton_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(new UsersPage());
        }

        private void LogoutButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show(
                "Вы действительно хотите выйти из аккаунта?",
                "Подтверждение выхода",
                MessageBoxButton.YesNo,
                MessageBoxImage.Question);

            if (result != MessageBoxResult.Yes)
            {
                return;
            }

            UserSession.Clear();
            LoginWindow loginWindow = new LoginWindow();
            loginWindow.Show();
            Close();
        }

        private static string GetRoleTitle(string roleName)
        {
            if (roleName == "admin")
            {
                return "администратор";
            }

            if (roleName == "manager")
            {
                return "менеджер";
            }

            return "просмотр";
        }
    }
}
