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 System.Globalization;

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

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

        private void btnEnter_Click(object sender, EventArgs e)
        {
            //Check Validation

            bool ErrorFlag = false;
            if (txtUserName.Text.Trim() == "")
            {
                errLogin.SetIconAlignment(this.txtUserName, ErrorIconAlignment.MiddleLeft);
                errLogin.SetError(this.txtUserName, Properties.Resources.ResourceManager.GetString("ErrEmptyUsername"));
                ErrorFlag = true;
            }
            if (txtPassword.Text.Trim() == "")
            {
                errLogin.SetIconAlignment(this.txtPassword, ErrorIconAlignment.MiddleLeft);
                errLogin.SetError(this.txtPassword, Properties.Resources.ResourceManager.GetString("ErrEmptyPassword"));
                ErrorFlag = true;
            }
            if (ErrorFlag)
            {
                return;
            }

            //login
            this.Cursor = Cursors.WaitCursor;
            
            Aram_CRM_Project.BLL.Entity.User UserVar = new Aram_CRM_Project.BLL.Entity.User();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            frmParent fParent = new frmParent();
            lblComment.Visible = false;
            string TextFile;
            string[] NetworkConnection;
            string ConnectionString = string.Empty; ;
            string ConnectionStringMaster = string.Empty;


            //Aram_CRM_Project.BLL.Entity.StaticVariable.DataSource = "Data Source=.;Initial Catalog=DbCrm1;Integrated Security=true;";

            //ConnectionString
            //if (System.IO.File.Exists(Application.StartupPath + @"\NC.dat"))
            //{
            //    System.IO.StreamReader Reader = new System.IO.StreamReader(Application.StartupPath + @"\NC.dat");
            //    TextFile = Reader.ReadToEnd();
            //    Reader.Close();
            //    if (TextFile != "")
            //    {
            //        NetworkConnection = TextFile.Split('&');
            //        if (NetworkConnection[0] == "Server")
            //        {
            //            //"Data Source=HP-530\\SQL2005;Initial Catalog=DbAramCrm;Integrated Security=true;"
            //            ConnectionString = "Data Source=.;Initial Catalog=DbCrm1;Integrated Security=true;";
            //            ConnectionStringMaster = "Data Source=.;Initial Catalog=master;Integrated Security=true;";
            //        }
            //        else if (NetworkConnection[0] == "Client")
            //        {
            //            ConnectionString = "Data Source=" + NetworkConnection[1] + ";Initial Catalog=DbCrm1;User Id=sa;Password=" + utility.Decode(NetworkConnection[2]) + ";";
            //        }
            //    }
            //    else
            //    {
            //        lblComment.Text = "نتظیمات شبکه نادرست است";
            //        this.Cursor = Cursors.Default;
            //        return;
            //    }
            //}
            //else
            //{
            //    lblComment.Text = "نتظیمات شبکه نادرست است";
            //    this.Cursor = Cursors.Default;
            //    return;
            //}

            ////DataSource
            //Aram_CRM_Project.BLL.Entity.StaticVariable.DataSource = ConnectionString;
            //Aram_CRM_Project.BLL.Entity.StaticVariable.DataSourceMaster = ConnectionStringMaster;

            Aram_CRM_Project.BLL.Public PublicClass = new Aram_CRM_Project.BLL.Public();
            //Login
            UserVar = PublicClass.IdentifyUser(utility.ChangeChar(txtUserName.Text.Trim()), utility.Encode(txtPassword.Text.Trim()));
            if (UserVar != null)
            {
                if (UserVar.UserStatusId == "1")
                {
                    string[] MenuItem = UserVar.MenuItem.Split(' ');
                    string[] MenuItemCount = UserVar.MenuItemCount.Split(' ');
                    int Counter = 0;

                    for (int i = 0; i < fParent.mnuMain.Items.Count; i++)
                    {
                        for (int j = 0; j < int.Parse(MenuItemCount[i].ToString()); j++)
                        {
                            ((ToolStripMenuItem)((frmParent)(this.MdiParent)).mnuMain.Items[i]).DropDownItems[MenuItem[Counter].ToString()].Enabled = true;
                            Counter++;
                        }
                    }


                    //Set System Variable
                    //((frmParent)(this.MdiParent)).tslActiveUser.Text = UserVar.Name;
                    Aram_CRM_Project.BLL.Entity.StaticVariable.Id = UserVar.Id;
                    Aram_CRM_Project.BLL.Entity.StaticVariable.Name = UserVar.Name;
                    Aram_CRM_Project.BLL.Entity.StaticVariable.Username = UserVar.Username;
                    Aram_CRM_Project.BLL.Entity.StaticVariable.Password = UserVar.Password;
                    Aram_CRM_Project.BLL.Entity.StaticVariable.SystemDate = nudYear.Value.ToString() + "/" + dudMonth.Text + "/" + dudDay.Text;

                    //left Panel
                    //fParent.lblActiveUser.Text = UserVar.Name;
                    ((frmParent)(this.MdiParent)).lblActiveUser.Text = UserVar.Name;
                    ((frmParent)(this.MdiParent)).lblActiveUsername.Text = UserVar.Username;
                    ((frmParent)(this.MdiParent)).lblSystemDate.Text = nudYear.Value.ToString() + "/" + dudMonth.Text + "/" + dudDay.Text;

                    //Disable mnuLogin
                    ((ToolStripMenuItem)((frmParent)(this.MdiParent)).mnuMain.Items[0]).DropDownItems["mnuLogin"].Enabled = false;

                    //Disable Button
                    ((frmParent)(this.MdiParent)).btnChangeDate.Enabled = true;
                    ((frmParent)(this.MdiParent)).btnChangePassword.Enabled = true;

                    Aram_CRM_Project.BLL.Entity.StaticVariable.FormLogin_Shown = false;


                    //Reminder
                    Aram_CRM_Project.BLL.Entity.StaticVariable.ReminderCount = PublicClass.ReminderCount(UserVar.Id);
                    ((frmParent)(this.MdiParent)).lilReminder.Text = "شما  " + Aram_CRM_Project.BLL.Entity.StaticVariable.ReminderCount + "  یادآوری دارید";
                    
                    this.Cursor = Cursors.Default;
                    this.Dispose();
                    
                }
                else
                {
                    lblComment.Visible = true;
                    lblComment.Text = "کاربر مورد نظر غیر فعال می باشد";
                    this.Cursor = Cursors.Default;
                    return;
                }
            }
            else
            {
                lblComment.Visible = true;
                lblComment.Text = "نام کاربری یا رمز عبور صحیح نمی باشد";
                this.Cursor = Cursors.Default;
                return;
            }

            this.Cursor = Cursors.Default;

        }

        

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

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnEnter_Click(sender, e);
            }
        }

        private void dudDay_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnEnter_Click(sender, e);
            if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down))
                e.SuppressKeyPress = true;
        }

        private void dudMonth_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnEnter_Click(sender, e);
            if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down))
                e.SuppressKeyPress = true;
        }

        private void nudYear_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnEnter_Click(sender, e);
            if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down))
                e.SuppressKeyPress = true;
        }

        private void frmLogin_Activated(object sender, EventArgs e)
        {
            //ُSystem Date
            PersianCalendar PersianDate = new PersianCalendar();
            DateTime TodayDate = DateTime.Now;
            string Day;
            string Month;
            string Year;
            if (PersianDate.GetDayOfMonth(TodayDate) >= 1 && PersianDate.GetDayOfMonth(TodayDate) <= 9)
                Day = "0" + PersianDate.GetDayOfMonth(TodayDate).ToString();
            else
                Day = PersianDate.GetDayOfMonth(TodayDate).ToString();

            if (PersianDate.GetMonth(TodayDate) >= 1 && PersianDate.GetMonth(TodayDate) <= 9)
                Month = "0" + PersianDate.GetMonth(TodayDate).ToString();
            else
                Month = PersianDate.GetMonth(TodayDate).ToString();

            Year = PersianDate.GetYear(TodayDate).ToString();
            
            dudDay.Text = Day;
            dudMonth.Text = Month;
            nudYear.Value = decimal.Parse(Year);
        }

        private void btnConnection_Click(object sender, EventArgs e)
        {
            frmConnection fConncetion = new frmConnection();
            fConncetion.ShowDialog();
        }

        

        
    }
}
