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

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

        public RegistrationNewUser()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(RegistrationNewUserHeading);
            DefaultScreen.initializePanel(CreateNewUserPanel);
            RegistrationNewUserHeading.Text = Properties.Resources.RegistrationNewUserHeading;

            RegistrationUsernameLabel.Text = Properties.Resources.RegistrationUsernameLabel;
            RegistrationFirstnameLabel.Text = Properties.Resources.RegistrationFirstnameLabel;
            RegistrationLastnameLabel.Text = Properties.Resources.RegistrationLastnameLable;
            RegistrationPasswordLabel.Text = Properties.Resources.RegistrationPasswordLabel;
            RegistrationRetypePasswordLabel.Text = Properties.Resources.RegistrationRetypePasswordLabel;
        }

        private void RegistrationRegisterButton_Click(object sender, EventArgs e)
        {
            RegistrationUsernameTextBox.BackColor = default(Color);
            RegistrationFirstnameTextBox.BackColor = default(Color);
            RegistrationLastnameTextBox.BackColor = default(Color);
            RegistrationPasswordTextBox.BackColor = default(Color);
            RegistrationRetypePasswordTextBox.BackColor = default(Color);

            if (RegistrationUsernameTextBox.Text == "" || RegistrationUsernameTextBox.Text == " ")
            {
                RegistrationUsernameTextBox.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RegistrationUserNameNull;
            }
            else if (UserLayout.IsUsernameInDatabase(RegistrationUsernameTextBox.Text)==true)
            {
                RegistrationUsernameTextBox.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RegistrationUserNameExists;
            }
            else if (RegistrationFirstnameTextBox.Text == "" || RegistrationFirstnameTextBox.Text == " ")
            {
                RegistrationFirstnameTextBox.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RegistrationFirstNameNull;
            }
            else if (RegistrationLastnameTextBox.Text == "" || RegistrationLastnameTextBox.Text == " ")
            {
                RegistrationLastnameTextBox.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RegistrationLastNameNull;
            }
            else if (RegistrationPasswordTextBox.Text == "" || RegistrationPasswordTextBox.Text == " ")
            {
                RegistrationPasswordTextBox.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RegistrationPasswordNull;
            }
            else if (RegistrationPasswordTextBox.Text != RegistrationRetypePasswordTextBox.Text)
            {
                RegistrationPasswordTextBox.BackColor = Color.FromArgb(255, 232, 232);
                RegistrationRetypePasswordTextBox.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RegistrationPasswordsNotEqual;
            }
            else
            { 
                int status = UserLayout.CreateNewUser(RegistrationUsernameTextBox.Text, RegistrationFirstnameTextBox.Text, RegistrationLastnameTextBox.Text, RegistrationPasswordTextBox.Text, 0);
                if (status == 1)
                {
                    DefaultScreen.ShowStatusPage(1, Properties.Resources.UserSuccessfullyCreated, Properties.Resources.CanLogin);
                }
            }
        }
    }
}
