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;
using Aram_CRM_Project.BLL.Entity;

namespace Aram_CRM_Project.UIL
{
    public partial class frmUser : Form
    {
        public frmUser()
        {
            InitializeComponent();
        }

        private bool ShowPasswordFlag = true;
        private string _Mode = string.Empty;
        public string Mode
        {
            set { _Mode = value; }
            get { return _Mode; }
        }

        public void IdentifyMode(string Id)
        {
            Aram_CRM_Project.BLL.UserCRUD UserCrud = new Aram_CRM_Project.BLL.UserCRUD();
            User UserVar = new User();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();

            switch (Mode)
            {
                case "New":
                    {
                        btnShowPassword.Visible = false;
                        lblConfirmNewPssword.Enabled = false;
                        txtConfirmNewPassword.Enabled = false;

                        this.Text = "  تعریف کاربران جدید";
                        this.ShowDialog();
                        break;
                    }
                case "Edit":
                    {
                        lblPassword.Text = "رمز عبور جدید :";
                        UserVar = UserCrud.ReadUser(Id);
                        if (UserVar != null)
                        {
                            string[] MenuItem = UserVar.MenuItem.Split(' ');
                            string[] MenuItemCount = UserVar.MenuItemCount.Split(' ');
                            int Counter = 0;

                            lblId.Text = UserVar.Id;
                            txtName.Text = UserVar.Name;
                            txtUsername.Text = UserVar.Username;
                            txtPassword.Text = utility.Decode(UserVar.Password);
                            if (UserVar.UserStatusId == "1")
                                rdoActive.Checked = true;
                            else
                                rdoInActive.Checked = true;
                            txtComment.Text = UserVar.Comment;

                            for (int i = 0; i < mnuMain.Items.Count; i++)
                            {
                                for (int j = 0; j < int.Parse(MenuItemCount[i].ToString()); j++)
                                {
                                    ((ToolStripMenuItem)((ToolStripMenuItem)mnuMain.Items[i]).DropDownItems[MenuItem[Counter].ToString()]).CheckState = CheckState.Checked;
                                    Counter++;
                                }
                            }

                            this.Text = "  ویرایش کاربران";
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
                case "Show":
                    {
                        UserVar = UserCrud.ReadUser(Id);
                        if (UserVar != null)
                        {
                            string[] MenuItem = UserVar.MenuItem.Split(' ');
                            string[] MenuItemCount = UserVar.MenuItemCount.Split(' ');
                            int Counter = 0;

                            lblId.Text = UserVar.Id;
                            txtName.Text = UserVar.Name;
                            txtUsername.Text = UserVar.Username;
                            txtPassword.Text = utility.Decode(UserVar.Password);
                            if (UserVar.UserStatusId == "1")
                            {
                                rdoActive.Checked = true;
                                rdoInActive.Enabled = false;
                            }
                            else
                            {
                                rdoInActive.Checked = true;
                                rdoActive.Enabled = false;
                            }
                            txtComment.Text = UserVar.Comment;

                            for (int i = 0; i < mnuMain.Items.Count; i++)
                            {
                                for (int j = 0; j < int.Parse(MenuItemCount[i].ToString()); j++)
                                {
                                    ((ToolStripMenuItem)((ToolStripMenuItem)mnuMain.Items[i]).DropDownItems[MenuItem[Counter].ToString()]).CheckState = CheckState.Checked;
                                    Counter++;
                                }
                            }

                            rdoSelectAll.Enabled = false;
                            rdoNoSelect.Enabled = false;
                            txtPass.Enabled = false;
                            txtConfirmNewPassword.Enabled = false;
                            btnEnter.Enabled = false;

                            this.ActiveControl = this.btnExit;
                            this.Text = "  نمایش جزئیات کاربران";
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

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

            int Result;
            bool ErrorFlag = false;

            erpUser.Clear();
            if (txtName.Text.Trim() == "")
            {
                erpUser.SetIconAlignment(this.txtName, ErrorIconAlignment.MiddleLeft);
                erpUser.SetError(this.txtName, Properties.Resources.ResourceManager.GetString("ErrEmptyUsername"));
                ErrorFlag = true;
            }
            if (txtUsername.Text.Trim() == "")
            {
                erpUser.SetIconAlignment(this.txtUsername, ErrorIconAlignment.MiddleLeft);
                erpUser.SetError(this.txtUsername, Properties.Resources.ResourceManager.GetString("ErrEmptyUser"));
                ErrorFlag = true;
            }
            if (txtPassword.Text.Trim() == txtUsername.Text.Trim())
            {
                erpUser.SetIconAlignment(this.txtPassword, ErrorIconAlignment.MiddleLeft);
                erpUser.SetError(this.txtPassword, Properties.Resources.ResourceManager.GetString("ErrEqualUserPass"));
                ErrorFlag = true;
            }
            if (txtPassword.Text.Length < 6)
            {
                erpUser.SetIconAlignment(this.txtPassword, ErrorIconAlignment.MiddleLeft);
                erpUser.SetError(this.txtPassword, Properties.Resources.ResourceManager.GetString("ErrPasswordLength"));
                ErrorFlag = true;
            }
            if (txtPassword.Text.Trim() == "")
            {
                erpUser.SetIconAlignment(this.txtPassword, ErrorIconAlignment.MiddleLeft);
                erpUser.SetError(this.txtPassword, Properties.Resources.ResourceManager.GetString("ErrEmptyPassword"));
                ErrorFlag = true;
            }
            if (ErrorFlag)
            {
                return;
            }

            string MenuItem = string.Empty;
            string MenuItemCount = string.Empty;
            int i, j;
            int Counter;

            for (i = 0; i < mnuMain.Items.Count; i++)
            {
                Counter = 0;
                for (j = 0; j < ((ToolStripMenuItem)mnuMain.Items[i]).DropDownItems.Count; j++)
                {
                    if (((ToolStripMenuItem)((ToolStripMenuItem)mnuMain.Items[i]).DropDownItems[j]).Checked)
                    {
                        MenuItem = MenuItem + " " + ((ToolStripMenuItem)mnuMain.Items[i]).DropDownItems[j].Name;
                        Counter++;
                    }
                }

                MenuItemCount = MenuItemCount + " " + Counter;

            }

            UserVar.Id = lblId.Text;
            UserVar.Name = utility.ChangeChar(txtName.Text.Trim());
            UserVar.Username = utility.ChangeChar(txtUsername.Text.Trim());
            UserVar.Password = utility.Encode(txtPassword.Text.Trim());
            if (rdoActive.Checked)
                UserVar.UserStatusId = "1";
            else
                UserVar.UserStatusId = "2";
            UserVar.Comment = txtComment.Text.Trim();
            UserVar.MenuItem = MenuItem.Trim();
            UserVar.MenuItemCount = MenuItemCount.Trim();

            switch (Mode)
            {
                case "New":
                    {
                        if (txtPassword.Text.Trim() != txtPass.Text.Trim())
                        {
                            erpUser.SetIconAlignment(this.txtPassword, ErrorIconAlignment.MiddleLeft);
                            erpUser.SetError(this.txtPassword, Properties.Resources.ResourceManager.GetString("ErrNotEqualPassword"));
                            return;
                        }

                        Result = UserCrud.InsertUser(UserVar);
                        if (Result == 0)
                        {
                            fMessage.RegUnSuccess();
                            return;
                        }
                        else if (Result == -1)
                        {
                            fMessage.RepeatingUsername();
                            return;
                        }
                        break;
                    }
                case "Edit":
                    {
                        bool EditFlag = false;
                        if (txtPass.Text.Trim() != "")
                        {
                            if (txtPass.Text.Length < 6)
                            {
                                erpUser.SetIconAlignment(this.txtPass, ErrorIconAlignment.MiddleLeft);
                                erpUser.SetError(this.txtPass, Properties.Resources.ResourceManager.GetString("ErrPasswordLength"));
                                EditFlag = true;
                            }
                            if (txtPass.Text.Trim() != txtConfirmNewPassword.Text.Trim())
                            {
                                erpUser.SetIconAlignment(this.txtPass, ErrorIconAlignment.MiddleLeft);
                                erpUser.SetError(this.txtPass, Properties.Resources.ResourceManager.GetString("ErrNotEqualNewPassword"));
                                EditFlag = true;
                            }
                            if (txtPass.Text.Trim() == txtUsername.Text.Trim())
                            {
                                erpUser.SetIconAlignment(this.txtPass, ErrorIconAlignment.MiddleLeft);
                                erpUser.SetError(this.txtPass, Properties.Resources.ResourceManager.GetString("ErrEqualUserPass"));
                                EditFlag = true;
                            }
                            if (EditFlag)
                            {
                                return;
                            }

                            UserVar.Password = utility.Encode(txtPass.Text.Trim());
                        }

                        Result = UserCrud.UpdateUser(UserVar);
                        if (Result == 0)
                        {
                            fMessage.RegUnSuccess();
                            return;
                        }
                        else if (Result == -1)
                        {
                            fMessage.RepeatingUsername();
                            return;
                        }
                        break;
                    }
            }

            this.Dispose();

        }

       

        

        private void btnShowPassword_Click(object sender, EventArgs e)
        {
            if (ShowPasswordFlag)
            {
                txtPassword.PasswordChar = '\0';
                btnShowPassword.Text = "عدم نمایش";
                ShowPasswordFlag = false;
            }
            else
            {
                txtPassword.PasswordChar = '*';
                btnShowPassword.Text = "نمایش رمز عبور";
                ShowPasswordFlag = true;
            }
        }

        private void rdoSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoSelectAll.Checked == true)
            {
                for (int i = 0; i < mnuMain.Items.Count; i++)
                {
                    for (int j = 0; j < ((ToolStripMenuItem)mnuMain.Items[i]).DropDownItems.Count; j++)
                    {
                        ((ToolStripMenuItem)((ToolStripMenuItem)mnuMain.Items[i]).DropDownItems[j]).CheckState = CheckState.Checked;
                    }
                }
            }
        }

        private void rdoNoSelect_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoNoSelect.Checked == true)
            {
                for (int i = 0; i < mnuMain.Items.Count; i++)
                {
                    for (int j = 0; j < ((ToolStripMenuItem)mnuMain.Items[i]).DropDownItems.Count; j++)
                    {
                        ((ToolStripMenuItem)((ToolStripMenuItem)mnuMain.Items[i]).DropDownItems[j]).CheckState = CheckState.Unchecked;
                    }
                }

                mnuLogin.Checked = true;
                mnuLogout.Checked = true;
                mnuExit.Checked = true;
            }
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show" || Mode == "Edit")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void txtPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void txtConfirmNewPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void txtComment_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void mnuSystem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (Mode != "Show")
            {
                if (((ToolStripMenuItem)e.ClickedItem).Checked)
                    ((ToolStripMenuItem)e.ClickedItem).CheckState = CheckState.Unchecked;
                else
                    ((ToolStripMenuItem)e.ClickedItem).CheckState = CheckState.Checked;
            }
        }

        private void mnuBaseInformation_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (Mode != "Show")
            {
                if (((ToolStripMenuItem)e.ClickedItem).Checked)
                    ((ToolStripMenuItem)e.ClickedItem).CheckState = CheckState.Unchecked;
                else
                    ((ToolStripMenuItem)e.ClickedItem).CheckState = CheckState.Checked;
            }
        }

        private void mnuOperation_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (Mode != "Show")
            {
                if (((ToolStripMenuItem)e.ClickedItem).Checked)
                    ((ToolStripMenuItem)e.ClickedItem).CheckState = CheckState.Unchecked;
                else
                    ((ToolStripMenuItem)e.ClickedItem).CheckState = CheckState.Checked;
            }
        }


    }
}
