using System;
using System.Collections.Generic;
using System.ComponentModel;
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>
    /// LoginPage.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class LoginPage : Page, INotifyPropertyChanged {
        public LoginPage() {
            InitializeComponent();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public readonly LoginManager LoginManager = Application.Current.Resources["loginManager"] as LoginManager;

        private bool serverConnected;
        public bool ServerConnected {
            get => serverConnected;
            set { serverConnected = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ServerConnected")); }
        }

        private string userId;
        public string UserId {
            get => userId;
            set { userId = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("UserId")); }
        }

        private string userPassword;
        public string UserPassword {

            get => userPassword;
            set { userPassword = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("UserPassword")); }
        }

        private string guestId;
        public string GuestId {
            get => guestId;
            set { guestId = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("GuestId")); }
        }

        private async void thisPage_Loaded(object sender, RoutedEventArgs e) {
            var result = await LoginManager.Connect(App.PcId, App.LocalServerAddress);
            if (result == LoginManagerError.None || result == LoginManagerError.AlreadyConnected) {
                ServerConnected = true;
            }
            //passwordBoxPlaceholder.IsTabStop = false;

            //FocusManager.SetIsFocusScope(IdTextBox, true);
            //IdTextBox.Focus();
        }

        private void ReviewButton_Click(object sender, RoutedEventArgs e) {
            var reviewWindow = new ReviewWindow("rsatang5");
            reviewWindow.ShowDialog();
            if (reviewWindow.DialogResult == true) {
                new AlertWindow("감사합니다!", "귀하의 평가는 더 나은 서비스 제공에 도움이 될 것입니다.").ShowDialog();
            }
        }

        private async void LoginButton_Click(object sender, RoutedEventArgs e) {
            if (string.IsNullOrWhiteSpace(GuestId)) {
                var result = await LoginManager.Login(UserId,passwordBox.Password);
                if (result == LoginManagerError.None || result == LoginManagerError.AlreadyLoggedIn) {
                    
                    var window = new MainWindow(Window.GetWindow(this) as LoginWindow, UserId);
                    UserId = passwordBox.Password = GuestId = "";
                    window.Show();
                    Window.GetWindow(this).Hide();
                } else {
                    var message = "알 수 없는 오류가 발생했습니다.";
                    switch (result) {
                        case LoginManagerError.WrongIdOrPassword: message = "아이디 또는 비밀번호가 일치하지 않습니다."; break;
                        case LoginManagerError.CashIsZero: message = "잔액이 부족합니다."; break;
                    }
                    new AlertWindow("로그인 실패", message).ShowDialog();
                }
            } else {
                var result = await LoginManager.Login(GuestId);
                if (result == LoginManagerError.None || result == LoginManagerError.AlreadyLoggedIn) {
                    UserId = passwordBox.Password = GuestId = "";
                    var window = new MainWindow(Window.GetWindow(this) as LoginWindow, null);
                    window.Show();
                    Window.GetWindow(this).Hide();
                } else {
                    var message = "알 수 없는 오류가 발생했습니다.";
                    switch (result) {
                        case LoginManagerError.WrongIdOrPassword: message = "존재하지 않는 비회원 번호입니다."; break;
                    }
                    new AlertWindow("로그인 실패", message).ShowDialog();
                }
            }
        }

        private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e) {
            passwordBoxPlaceholder.Text = string.IsNullOrEmpty(passwordBox.Password) ? "비밀번호" : "";
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            
        }
    }
}
