using System;
using System.Drawing;
using System.Windows.Forms;

namespace AgencySystem
{
    public partial class LoginPage : UserControl
    {
        private static LoginPage _instance;
        public static LoginPage Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new LoginPage();
                return _instance;
            }
        }

        public LoginPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(LoginHeading);
            DefaultScreen.initializePanel(LoginPagePanel);
            LoginHeading.Text = Properties.Resources.LoginHeading;
            LoginUsernameLabel.Text = Properties.Resources.LoginUsernameLabel;
            LoginPasswordLabel.Text = Properties.Resources.LoginPasswordLabel;
            ClearAllStates();
        }

        private void ClearAllStates()
        {
            UsernameTextBox.BackColor = default(Color);
            PasswordTextBox.BackColor = default(Color);
        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            ClearAllStates();
            /* All possible returned states
            3 = Unspecified error
            2 = User does not exist
            1 = Password is wrong
            0 = Successfull
            */

            if (UsernameTextBox.Text != "" && UsernameTextBox.Text != " " && PasswordTextBox.Text != "" && PasswordTextBox.Text != " ")
            {
                int loginState = UserLayout.VerifyLogin(UsernameTextBox.Text, PasswordTextBox.Text);
                switch (loginState)
                {
                    case 0:
                        DefaultScreen.loggedIn = true;
                        DefaultScreen.ShowStatusPage(1, Properties.Resources.SucessfullyLoggedIn, Properties.Resources.LoginWasSuccessfull);
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.SucessfullyLoggedIn;
                        DefaultScreen DefaultScreenForm = (DefaultScreen)Application.OpenForms["DefaultScreen"];
                        DefaultScreenForm.ChangeMenu.Invoke("");
                        break;
                    case 1:
                        WrongPasswordAlert();
                        break;
                    case 2:
                        WrongUsernameAlert();
                        break;
                    case 3:
                        GeneralLoginErrorAlert();
                        break;
                    default:
                        GeneralLoginErrorAlert();
                        break;
                }
            }
            else if (UsernameTextBox.Text == "")
            {
                UsernameTextBox.BackColor = Color.FromArgb(255, 232, 232);
            }
            else if (PasswordTextBox.Text == "")
            {
                PasswordTextBox.BackColor = Color.FromArgb(255, 232, 232);
            }
        }


        private void WrongPasswordAlert()
        {
            PasswordTextBox.BackColor = Color.FromArgb(255, 232, 232);
            DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.WrongPassword;
        }

        private void WrongUsernameAlert()
        {
            UsernameTextBox.BackColor = Color.FromArgb(255, 232, 232);
            DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.WrongUsername;
        }

        private void GeneralLoginErrorAlert()
        {
            DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.UnspecifiedError;
        }

        private void UsernameTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            LoginButtonPerformClickAfterEnter(sender, e);
        }

        private void PasswordTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            LoginButtonPerformClickAfterEnter(sender, e);
        }

        private void LoginButtonPerformClickAfterEnter(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                LoginButton.PerformClick();
            }
        }

    }
}
