using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace JtkPcClientWpf {
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window {
        private readonly LoginWindow loginWindow;
        public readonly LoginManager LoginManager = Application.Current.Resources["loginManager"] as LoginManager;
        private bool exitApp = true;
        private string loggedInUserId = "";
        public MainWindow(LoginWindow loginWindow, string userId) {
            if (string.IsNullOrWhiteSpace(userId) == false) {
                loggedInUserId = userId;
            }
            InitializeComponent();
            this.loginWindow = loginWindow;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            LoginManager.LoggedOut += LoginManager_LoggedOut;
        }

        private void LoginManager_LoggedOut(object sender, LoginManagerError e) {
            loginWindow.Show();
            loginWindow.OpenReviewDialog(loggedInUserId);
            exitApp = false;
            this.Close();
        }

        private void Window_Closed(object sender, EventArgs e) {
            LoginManager.LoggedOut -= LoginManager_LoggedOut;
            if (exitApp) {
                loginWindow.Close();
            }
        }

        private async void Logout_Click(object sender, RoutedEventArgs e) {
            EndButton.IsEnabled = false;
            await LoginManager.Logout();
            EndButton.IsEnabled = true;
        }

        private void MinimizeButton_Click(object sender, RoutedEventArgs e) {
            this.WindowState = WindowState.Minimized;
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e) {
            this.DragMove();
        }

        private void Order_Click(object sender, RoutedEventArgs e) {
            var window = new OrderWindow();
            window.ShowDialog();
            if (window.DialogResult == true) {
                var alert = new AlertWindow("주문", "주문이 완료되었습니다.");

                alert.ShowDialog();
            }
        }
    }
}
