using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hukpiWinApp.Forms.Admin
{
    public partial class AddUserForm : BaseForm
    {
        public AddUserForm()
        {
            InitializeComponent();
            fillComboUserGroups(true);
            checkBoxIsActive.Checked = true;
        }

        private void fillComboUserGroups(bool justActives)
        {
            comboBoxUserGroup.DataSource = Utils.getUserGroupsForCombo(Properties.Settings.Default.ComboBoxNotSelected,justActives);
            comboBoxUserGroup.DisplayMember = "Description";
            comboBoxUserGroup.ValueMember = "Id";
        }

        private void textBoxUserName_Validating(object sender, CancelEventArgs e)
        {
            Utils.validateUserName(errorProviderAddEditUser, textBoxUserName);    
        }

        private void textBoxFullName_Validating(object sender, CancelEventArgs e)
        {
            Utils.validateMustNonEmpty(errorProviderAddEditUser, textBoxFullName);
        }

        private void textBoxPassword_Validating(object sender, CancelEventArgs e)
        {
            Utils.validateMustNonEmpty(errorProviderAddEditUser, textBoxPassword);
        }

        private bool validatePasswordRe()
        {
            if (Utils.validateMustNonEmpty(errorProviderAddEditUser, textBoxPasswordRe))
            {
                if (!textBoxPasswordRe.Text.Equals(textBoxPassword.Text))
                {
                    errorProviderAddEditUser.SetError(textBoxPasswordRe, "A jelszó és a jelszó megerősítése nem egyezik!");
                    return false;
                }
                else
                {
                    errorProviderAddEditUser.SetError(textBoxPasswordRe, "");
                    return true;
                }
            }
            return false;
        }

        private void textBoxPasswordRe_Validating(object sender, CancelEventArgs e)
        {
            validatePasswordRe();
        }

        private void comboBoxUserGroup_Validating(object sender, CancelEventArgs e)
        {
            Utils.validateMustNonEmpty(errorProviderAddEditUser, comboBoxUserGroup);
        }

        private bool validateForm()
        {
            bool isValidUserName = Utils.validateUserName(errorProviderAddEditUser, textBoxUserName);
            bool isValidFullName = Utils.validateMustNonEmpty(errorProviderAddEditUser, textBoxFullName);
            bool isValidPassword = Utils.validateMustNonEmpty(errorProviderAddEditUser, textBoxPassword);
            bool isValidPasswordRe = validatePasswordRe();
            bool isValidUserGroup = Utils.validateMustNonEmpty(errorProviderAddEditUser, comboBoxUserGroup);

            return isValidUserName && isValidFullName && isValidPassword && isValidPasswordRe && isValidUserGroup;
        }

        private bool saveNewUser()
        {
            if (!validateForm())
            {
                MessageBox.Show(ErrorMessages.Default.invalidFormInputData);
                return false;
            }
            else
            {
                using (hukpiLINQDataContext linqContext = new hukpiLINQDataContext())
                {
                    using (var transaction = new System.Transactions.TransactionScope())
                    {
                        User userData = new User();

                        userData.userName = textBoxUserName.Text;
                        userData.userFullName = textBoxFullName.Text;
                        userData.groupId = Convert.ToDecimal(comboBoxUserGroup.SelectedValue);
                        userData.userPassword = Utils.getMD5Hash(textBoxPassword.Text);
                        userData.isActive = 1;

                        linqContext.Users.InsertOnSubmit(userData);
                        linqContext.SubmitChanges();
                        transaction.Complete();
                    }
                }

                MessageBox.Show("Sikeres mentés!");
                return true;
            }
        }

        private void saveLayoutUser_SaveButtonClick()
        {
            if (saveNewUser())
            {
                ((MainForm)this.MdiParent).refreshDataInAllChildForm();
                this.Dispose();
            }
        }

        private void saveLayoutUser_CancelButtonClick()
        {
            this.Dispose();
        }
    }
}
