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 Aram_CRM_Project.UIL
{
    public partial class frmUserPassword : Form
    {
        public frmUserPassword()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.Entity.StaticVariable.FormUserPassword_Shown = false;
            this.Dispose();
        }

        private void frmUserPassword_FormClosed(object sender, FormClosedEventArgs e)
        {
            Aram_CRM_Project.BLL.Entity.StaticVariable.FormUserPassword_Shown = false;
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.UserCRUD UserCrud = new Aram_CRM_Project.BLL.UserCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            frmMessage fMessage = new frmMessage();

            int Result;
            bool ErrorFlag = false;

            errUserPassword.Clear();
            //if (txtOldPassword.Text.Trim() == "")
            //{
            //    errUserPassword.SetIconAlignment(this.txtOldPassword, ErrorIconAlignment.MiddleLeft);
            //    errUserPassword.SetError(this.txtOldPassword, Properties.Resources.ResourceManager.GetString("ErrEmptyOldPassword"));
            //    ErrorFlag = true;
            //}
            if (txtNewPassword.Text.Trim() == "")
            {
                errUserPassword.SetIconAlignment(this.txtNewPassword, ErrorIconAlignment.MiddleLeft);
                errUserPassword.SetError(this.txtNewPassword, Properties.Resources.ResourceManager.GetString("ErrEmptyNewPassword"));
                ErrorFlag = true;
            }
            if (txtConfrimPassword.Text.Trim() == "")
            {
                errUserPassword.SetIconAlignment(this.txtConfrimPassword, ErrorIconAlignment.MiddleLeft);
                errUserPassword.SetError(this.txtConfrimPassword, Properties.Resources.ResourceManager.GetString("ErrEmptyConfirmPassword"));
                ErrorFlag = true;
            }
            if (txtNewPassword.Text.Trim() != txtConfrimPassword.Text.Trim())
            {
                errUserPassword.SetIconAlignment(this.txtNewPassword, ErrorIconAlignment.MiddleLeft);
                errUserPassword.SetError(this.txtNewPassword, Properties.Resources.ResourceManager.GetString("ErrNotEqualNewPassword"));

                errUserPassword.SetIconAlignment(this.txtConfrimPassword, ErrorIconAlignment.MiddleLeft);
                errUserPassword.SetError(this.txtConfrimPassword, Properties.Resources.ResourceManager.GetString("ErrNotEqualNewPassword"));
                ErrorFlag = true;
            }
            if (txtNewPassword.Text.Length < 6)
            {
                errUserPassword.SetIconAlignment(this.txtNewPassword, ErrorIconAlignment.MiddleLeft);
                errUserPassword.SetError(this.txtNewPassword, Properties.Resources.ResourceManager.GetString("ErrPasswordLength"));
                ErrorFlag = true;
            }
            if (txtConfrimPassword.Text.Length < 6)
            {
                errUserPassword.SetIconAlignment(this.txtConfrimPassword, ErrorIconAlignment.MiddleLeft);
                errUserPassword.SetError(this.txtConfrimPassword, Properties.Resources.ResourceManager.GetString("ErrPasswordLength"));
                ErrorFlag = true;
            }
            if (utility.Encode(txtOldPassword.Text.Trim()) != Aram_CRM_Project.BLL.Entity.StaticVariable.Password)
            {
                errUserPassword.SetIconAlignment(this.txtOldPassword, ErrorIconAlignment.MiddleLeft);
                errUserPassword.SetError(this.txtOldPassword, Properties.Resources.ResourceManager.GetString("ErrOldPassword"));
                ErrorFlag = true;
            }
            if (txtNewPassword.Text.Trim() == Aram_CRM_Project.BLL.Entity.StaticVariable.Username)
            {
                errUserPassword.SetIconAlignment(this.txtNewPassword, ErrorIconAlignment.MiddleLeft);
                errUserPassword.SetError(this.txtNewPassword, Properties.Resources.ResourceManager.GetString("ErrEqualUserPass"));
                ErrorFlag = true;
            }
            if (ErrorFlag)
            {
                return;
            }

            fMessage.SureConfirm();
            if (fMessage.ResultQuestion)
            {
                Result = UserCrud.UpdatePassword(Aram_CRM_Project.BLL.Entity.StaticVariable.Id, utility.Encode(txtNewPassword.Text.Trim()));
                if (Result == 0)
                {
                    fMessage.RegUnSuccess();
                    return;
                }
                else if (Result == -1)
                {
                    fMessage.RepeatingUsername();
                    return;
                }
            }

            this.Dispose();
        }
    }
}
