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.Threading;
using Aram_CRM_Project.BLL.Entity;
using System.Globalization;
using System.Reflection;

namespace Aram_CRM_Project.UIL
{
    public partial class frmParent : Form
    {
        private int childFormNumber = 0;
        private bool FirstLoad = true;
        BackgroundWorker bw;
        DataTable dtTemp = new DataTable();
        string Date = string.Empty;
        NotifyIcon noiAlarmReminder = new NotifyIcon();
        private string Reminder = string.Empty;
        public frmParent()
        {
            InitializeComponent();
            //Control.CheckForIllegalCrossThreadCalls = false;

            //Local Setup
            //Aram_CRM_Project.BLL.Entity.StaticVariable.DataSource = "Data Source=.\\SQLEXPRESS;Initial Catalog=DbKimiaOffice;Integrated Security=true;";
            //Aram_CRM_Project.BLL.Entity.StaticVariable.DataSourceMaster = "Data Source=.\\SQLEXPRESS;Initial Catalog=master;Integrated Security=true;";
            //Aram_CRM_Project.BLL.Entity.StaticVariable.DataSource = "Data Source=HP530\\SQL2005;Initial Catalog=DbCrm1;Integrated Security=true;";
            Aram_CRM_Project.BLL.Entity.StaticVariable.DataSource = "Data Source=(Local);Initial Catalog=DbCrm1;Integrated Security=true;";


            if (Aram_CRM_Project.BLL.Entity.StaticVariable.FirstLoad)
            {
                Aram_CRM_Project.BLL.Public PublicClass = new Aram_CRM_Project.BLL.Public();
                Aram_CRM_Project.BLL.Entity.StaticVariable.FirstLoad = false;
                Thread th1 = new Thread(new ThreadStart(ShowSplashForm));
                th1.Start();
                Thread.Sleep(2000);
                //PublicClass.CheckConnectionToDataBase();
                //PublicClass.CreateDataBase();
                th1.Abort();
                Thread.Sleep(1000);

                bw = new BackgroundWorker();
                bw.WorkerReportsProgress = true;
                bw.WorkerSupportsCancellation = true;
                bw.DoWork += bw_DoWork;
                bw.ProgressChanged += bw_ProgressChanged;
                bw.RunWorkerCompleted += bw_RunWorkerCompleted;
            }
        }

        private void ShowSplashForm()
        {
            frmSplash fSplash = new frmSplash();
            fSplash.ShowDialog();
            fSplash.Dispose();
        }

        private void frmParent_Load(object sender, EventArgs e)
        {
            lilReminder.Text = "";

            Aram_CRM_Project.BLL.Entity.StaticVariable.FormLogin_Shown = true;
            frmLogin fLogin = new frmLogin();
            fLogin.MdiParent = this;
            fLogin.Show();

            //Start AlarmReminder In BackGround
            bw.RunWorkerAsync();
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            if (bw.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            else
            {
                if (Aram_CRM_Project.BLL.Entity.StaticVariable.Id != "")
                {
                    if (FirstLoad)
                    {
                        FirstLoad = false;
                    }
                    else
                    {
                        Thread.Sleep(30000);
                    }

                    Aram_CRM_Project.BLL.ReminderCRUD ReminderCrud = new Aram_CRM_Project.BLL.ReminderCRUD();
                    
                    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();
                    Date = Year + "/" + Month + "/" + Day;
                    Reminder = "";
                    dtTemp = ReminderCrud.SearchReminderForAlarm(Aram_CRM_Project.BLL.Entity.StaticVariable.Id, Date);
                    if (dtTemp.Rows.Count != 0)
                    {
                        for (int i = 0; i < dtTemp.Rows.Count; i++)
                        {
                            Reminder = Reminder + dtTemp.Rows[i][0].ToString() + "\r\n\r\n";
                        }
                        Reminder = Reminder + "\r\n\r\n";
                        e.Result = "2";
                    }
                    else
                    {
                        e.Result = "1";
                    }

                    
                    
                }
                else
                {
                    Thread.Sleep(3000);
                    e.Result = "1";
                }
            }
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lilReminder.Text = "شما  " + Aram_CRM_Project.BLL.Entity.StaticVariable.ReminderCount + "  یادآوری دارید";
            if (e.Cancelled)
                MessageBox.Show("The Alarm Reminder Cancelled");
            else if (e.Error != null)
                MessageBox.Show(e.Error.ToString());
            else
            {
                if (e.Result.ToString() == "1")
                {
                    bw.RunWorkerAsync();
                }
                else
                {
                    noiAlarmReminder.Visible = false;
                    noiAlarmReminder.Icon = this.Icon;
                    noiAlarmReminder.Visible = true;
                    noiAlarmReminder.ShowBalloonTip(20000, "هشدار یادآوری ها", Reminder, ToolTipIcon.Info);


                    bw.RunWorkerAsync();
                }
                
            }
        }

        void bw_ProgressChanged (object sender,ProgressChangedEventArgs e)
        {
        }

        

        
        private void mnuReminder_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormReminder_Shown)
                {
                    StaticVariable.FormReminder_Shown = true;
                    frmShowReminder fShowReminder = new frmShowReminder();
                    fShowReminder.MdiParent = this;
                    fShowReminder.Show();

                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

            this.Cursor = Cursors.Default;
        }

        private void lilReminder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            mnuReminder_Click(sender, e);
        }

        
        private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childForm.MdiParent = this;
            childForm.Text = "Window " + childFormNumber++;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void mnuLogin_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormLogin_Shown)
                {
                    Aram_CRM_Project.BLL.Entity.StaticVariable.FormLogin_Shown = true;
                    frmLogin fLogin = new frmLogin();
                    fLogin.MdiParent = this;
                    fLogin.Show();
                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
            this.Cursor = Cursors.Default;
        }

        

        private void mnuCityState_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormCityState_Shown)
                {
                    StaticVariable.FormCityState_Shown = true;
                    frmShowCityState fShowCityState = new frmShowCityState();
                    fShowCityState.MdiParent = this;
                    fShowCityState.Show();
                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

            this.Cursor = Cursors.Default;
        }

        private void mnuRelationshipWay_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormRelationshipWay_Shown)
                {
                    StaticVariable.FormRelationshipWay_Shown = true;
                    frmShowRelationshipWay fShowRelationshipWay = new frmShowRelationshipWay();
                    fShowRelationshipWay.MdiParent = this;
                    fShowRelationshipWay.Show();
                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
            this.Cursor = Cursors.Default;
        }

        private void mnuCrType_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormCrType_Shown)
                {
                    StaticVariable.FormCrType_Shown = true;
                    frmShowCrType fShowCrType = new frmShowCrType();
                    fShowCrType.MdiParent = this;
                    fShowCrType.Show();
                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
            this.Cursor = Cursors.Default;
        }

        private void mnuStuffGroup_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormStuffGroup_Shown)
                {
                    StaticVariable.FormStuffGroup_Shown = true;
                    frmShowStuffGroup fShowStuffGroup = new frmShowStuffGroup();
                    fShowStuffGroup.MdiParent = this;
                    fShowStuffGroup.Show();
                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
            this.Cursor = Cursors.Default;
        }

        private void mnuCustomerGroup_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormCustomerGroup_Shown)
                {
                    StaticVariable.FormCustomerGroup_Shown = true;
                    frmShowCustomerGroup fShowCustomerGroup = new frmShowCustomerGroup();
                    fShowCustomerGroup.MdiParent = this;
                    
                    fShowCustomerGroup.Show();
                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

            this.Cursor = Cursors.Default;
        }

        private void mnuStuff_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormStuff_Shown)
                {
                    StaticVariable.FormStuff_Shown = true;
                    frmShowStuff fShowStuff = new frmShowStuff();
                    fShowStuff.MdiParent = this;
                    fShowStuff.Show();
                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

            this.Cursor = Cursors.Default;
        }

        private void mnuUser_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormUser_Shown)
                {
                    StaticVariable.FormUser_Shown = true;
                    frmShowUser fShowUser = new frmShowUser();
                    fShowUser.MdiParent = this;
                    fShowUser.Show();
                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

            this.Cursor = Cursors.Default;
        }

        private void mnuOrder_Click(object sender, EventArgs e)
        {
            
        }

        

        private void mnuCustomer_Click(object sender, EventArgs e)
        {
            
        }

        private void mnuCr_Click(object sender, EventArgs e)
        {
            
        }

        private void mnuReserve_Click(object sender, EventArgs e)
        {
            
        }
        

        private void mnuExit_Click(object sender, EventArgs e)
        {
            frmMessage fMessage = new frmMessage();
            fMessage.ExitConfirm();
            if (fMessage.ResultQuestion)
                this.Dispose();
        }

        private void mnuLogout_Click(object sender, EventArgs e)
        {
            frmMessage fMessage = new frmMessage();
            fMessage.ExitConfirm();
            if (fMessage.ResultQuestion)
            {
                this.Cursor = Cursors.WaitCursor;

                mnuLogin.Enabled = true;
                mnuLogout.Enabled = false;
                mnuUser.Enabled = false;
                mnuSetting.Enabled = false;
                mnuBackup.Enabled = false;
                mnuRestore.Enabled = false;

                mnuCityState.Enabled = false;
                mnuRelationshipWay.Enabled = false;
                mnuCrType.Enabled = false;
                mnuCustomerGroup.Enabled = false;
                mnuStuffGroup.Enabled = false;
                mnuStuff.Enabled = false;

                mnuReminder.Enabled = false;
                mnuOrder.Enabled = false;
                mnuCustomer.Enabled = false;
                mnuReserve.Enabled = false;
                mnuCr.Enabled = false;

                //left panel
                lblActiveUser.Text = "?";
                lblActiveUsername.Text = "?";
                lblSystemDate.Text = "0000/00/00";
                btnChangePassword.Enabled = false;
                btnChangeDate.Enabled = false;
                lilReminder.Text = "";

                //Set System Variable
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormCityState_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormCustomer_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormCustomerGroup_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormLogin_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormOrder_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormRelationshipWay_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormReminder_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormStuff_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormStuffGroup_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormUser_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormUserPassword_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormReserve_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormCr_Shown = false;
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormCrType_Shown = false;

                Aram_CRM_Project.BLL.Entity.StaticVariable.Id = "";
                Aram_CRM_Project.BLL.Entity.StaticVariable.Name = "";
                Aram_CRM_Project.BLL.Entity.StaticVariable.Password = "";
                Aram_CRM_Project.BLL.Entity.StaticVariable.Username = "";
                Aram_CRM_Project.BLL.Entity.StaticVariable.SystemDate = "";

                //Close All The MDIChildForms
                int MDIChildFormCount = Application.OpenForms.Count - 1;
                for (int i = 0; i < MDIChildFormCount; i++)
                {
                    Form frm = Application.OpenForms[1];
                    frm.Dispose();
                }

                //Open frmLogin
                Aram_CRM_Project.BLL.Entity.StaticVariable.FormLogin_Shown = true;
                frmLogin fLogin = new frmLogin();
                fLogin.MdiParent = this;
                fLogin.Show();

                this.Cursor = Cursors.Default;
            }
            
        }

   
        private void timClock_Tick(object sender, EventArgs e)
        {
            PersianCalendar pc = new PersianCalendar();
            DateTime Time = DateTime.Now;
            string Hour;
            string Minute;
            string Second;
            //hour
            if (pc.GetHour(Time) >= 0 && pc.GetHour(Time) <= 9)
                Hour = "0" + pc.GetHour(Time).ToString();
            else
                Hour = pc.GetHour(Time).ToString();
            //Minute
            if (pc.GetMinute(Time) >= 0 && pc.GetMinute(Time) <= 9)
                Minute = "0" + pc.GetMinute(Time).ToString();
            else
                Minute = pc.GetMinute(Time).ToString();
            //Second
            if (pc.GetSecond(Time) >= 0 && pc.GetSecond(Time) <= 9)
                Second = "0" + pc.GetSecond(Time).ToString();
            else
                Second = pc.GetSecond(Time).ToString();

            lblTime.Text = Hour + ":" + Minute + ":" + Second;
        }

        private void btnChangePassword_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormUserPassword_Shown)
                {
                    StaticVariable.FormUserPassword_Shown = true;
                    frmUserPassword fUserPassword = new frmUserPassword();
                    fUserPassword.MdiParent = this;
                    fUserPassword.Show();
                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

            this.Cursor = Cursors.Default;
        }

        private void btnChangeDate_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmMessage fMessage = new frmMessage();

            try
            {
                if (!StaticVariable.FormDate_Shown)
                {
                    StaticVariable.FormDate_Shown = true;
                    frmDate fDate = new frmDate();
                    fDate.MdiParent = this;
                    fDate.Show();
                }
                else
                {
                    fMessage.RepeatingForm();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

            this.Cursor = Cursors.Default;
        }

        private void mnuBackup_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.Public PublicClass = new Aram_CRM_Project.BLL.Public();
            string[] SystemDate = Aram_CRM_Project.BLL.Entity.StaticVariable.SystemDate.Split('/');
            saveFileDialog1.FileName = "BackUp" + SystemDate[0] + "_" + SystemDate[1] + "_" + SystemDate[2] + "_" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString();
            saveFileDialog1.Filter = "BackUpFiles(*.bak)|*.bak";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                PublicClass.BackupDatabase(saveFileDialog1.FileName.ToString());
                this.Cursor = Cursors.Default;
            }
        }

        private void mnuRestore_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.Public PublicClass = new Aram_CRM_Project.BLL.Public();

            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "BackUpFiles(*.bak)|*.bak";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                PublicClass.RestoreDatabase(openFileDialog1.FileName.ToString());
                this.Cursor = Cursors.Default;
            }
        }

        private void mnuSetting_Click(object sender, EventArgs e)
        {
            frmSetting fSetting = new frmSetting();
            fSetting.ShowDialog();
        }

        

        

        
    }
}
