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.IO;
using System.Data.SqlClient;
using System.Threading;


namespace Entity_FrameWork
{
    public partial class Backup : Form
    {
        public Backup()
        {
            InitializeComponent();
            backgroundWorkerBackup.WorkerReportsProgress = true;
            backgroundWorkerBackup.WorkerSupportsCancellation = true;
            backgroundWorkerRestore.WorkerReportsProgress = true;
            this.MinimumSize = new System.Drawing.Size(776, 490);

        }
        Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5();
        Boolean falg = false;
        private void button_Folder_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Directory.Exists(@"D:\Backup Program"))
                {
                    Directory.CreateDirectory(@"D:\Backup Program");
                    FMessegeBox.FarsiMessegeBox.Show("فايل پشتيبان با موفقيت ساخته شد");
                }
                else
                {
                    FMessegeBox.FarsiMessegeBox.Show("فولدر پشتيان قبلا ساخته شده است", "اخطار", FMessegeBox.FMessegeBoxButtons.Ok, FMessegeBox.FMessegeBoxIcons.Information);
                }
            }
            catch
            {
                FMessegeBox.FarsiMessegeBox.Show("ساخت فولدر پشتيبان موفقيت آميز نبود", "اخطار", FMessegeBox.FMessegeBoxButtons.Ok, FMessegeBox.FMessegeBoxIcons.Error);
            }
        }

        private void button_Backup_Click(object sender, EventArgs e)
        {
         
            
            
            try
            {
                if (textBox_MasirBackup.Text == "")
                {
                    FMessegeBox.FarsiMessegeBox.Show("لطفامسير پشتيان گيري را وارد كنيد","اخطار",FMessegeBox.FMessegeBoxButtons.Ok,FMessegeBox.FMessegeBoxIcons.Exclamtion);
                }
                else if (textBox_MasirBackup.Text != "")
                {
                    backgroundWorkerBackup.RunWorkerAsync();
                }
            }
            catch
            {
                FMessegeBox.FarsiMessegeBox.Show("عمليات پشتيبان گيري با مشكل روبه رو شد", "اخطار", FMessegeBox.FMessegeBoxButtons.Ok, FMessegeBox.FMessegeBoxIcons.Exclamtion);
            }
        }

        private void Backup_Load(object sender, EventArgs e)
        {
            button_Restore.Enabled = false;
            button_Backup.Enabled = false;
            textBox_MasirBackup.Enabled = false;
        }

        private void button_Masir_Click(object sender, EventArgs e)
        {
            System.Globalization.PersianCalendar pc = new System.Globalization.PersianCalendar();
            string date = pc.GetYear(DateTime.Now) + "." + pc.GetMonth(DateTime.Now) + "." + pc.GetDayOfMonth(DateTime.Now);


            saveFileDialog1.FilterIndex = 1;
              saveFileDialog1.Filter = "SQL Backup files (*.Bak) |*.bak|All files (*.*) |*.*";


              saveFileDialog1.FileName = date;
            saveFileDialog1.ShowDialog();
            textBox_MasirBackup.Text = saveFileDialog1.FileName;


        }

        private void radioButtonBackup_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonBackup.Checked == true)
            {
                button_Backup.Enabled = true;
                textBox_MasirBackup.Enabled = true;
                button_MasirBackup.Enabled = true;
                textBoxMasirRestore.Enabled = false;
                button_MasirRestore.Enabled = false;
                button_Restore.Enabled = false;
            }
        }

        private void radioButtonRestore_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonRestore.Checked == true)
            {
                button_Restore.Enabled = true;
                textBox_MasirBackup.Enabled = false;
                textBoxMasirRestore.Enabled = true;
                button_MasirRestore.Enabled = true;
                button_MasirBackup.Enabled = false;


            }
        }

        private void button_Restore_Click(object sender, EventArgs e)
        {
            if (textBoxMasirRestore.Text == "")
            {
                FMessegeBox.FarsiMessegeBox.Show("لطفامسير بازيابي اطلاعات را وارد كنيد", "اخطار", FMessegeBox.FMessegeBoxButtons.Ok, FMessegeBox.FMessegeBoxIcons.Exclamtion);
            }
            else if (textBoxMasirRestore.Text != "")
            {
               
                backgroundWorkerRestore.RunWorkerAsync();
            }
            
         }

     private void Restore(string strFileName)
        {
              falg = true;
            #region Restore File Bak

            SqlCommand oCommand = null;
            SqlConnection oConnection = null;

            try
            {
                string ConectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\EntityFrameWork.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                string Command = "ALTER DATABASE [" + Application.StartupPath + "\\EntityFrameWork.mdf] SET SINGLE_USER with ROLLBACK IMMEDIATE use master RESTORE DATABASE [" + Application.StartupPath + "\\EntityFrameWork.mdf] FROM DISK='" +strFileName + "'";

                this.Cursor = Cursors.WaitCursor;

                oConnection = new SqlConnection(ConectionString);
                if (oConnection.State != ConnectionState.Open)
                    oConnection.Open();

                oCommand = new SqlCommand(Command, oConnection);
                oCommand.ExecuteNonQuery();

                this.Cursor = Cursors.Default;
                FMessegeBox.FarsiMessegeBox.Show("عمليات بازيابي اطلاعات با موفقيت انجام شد");
                falg = false;
                Application.Restart();
            }
            catch (Exception)
            {
                FMessegeBox.FarsiMessegeBox.Show("عمليات موفقيت آميز نبود", "هشدار", FMessegeBox.FMessegeBoxButtons.Ok, FMessegeBox.FMessegeBoxIcons.Error);
            }
            finally
            {
                oConnection.Close();
                oCommand.Parameters.Clear();
                oCommand.Dispose();
            }
            #endregion
               

            

        }


        private void buttonMasir_Click(object sender, EventArgs e)
        {
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "SQL Restore files (*.Bak) |*.bak|All files (*.*) |*.*";
            openFileDialog1.ShowDialog();
            textBoxMasirRestore.Text = openFileDialog1.FileName;
        }



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

        private void Backup_FormClosed(object sender, FormClosedEventArgs e)
        {
          
        }

        private void backgroundWorkerBackup_DoWork(object sender, DoWorkEventArgs e)
        {
           

            MyModel.Sp_Backup(Application.StartupPath + @"\EntityFrameWork.mdf", textBox_MasirBackup.Text);

       
            for (int i = 0; i <= 100; i++)
            {
                Thread.Sleep(1);
                backgroundWorkerBackup.ReportProgress(i);
            }



        }

        private void backgroundWorkerBackup_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

                  
                   progressBarBackup.Value = e.ProgressPercentage;
          
           
            


        }

        private void backgroundWorkerBackup_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            FMessegeBox.FarsiMessegeBox.Show("عمليات پشتيبان گيري با موفقيت انجام شد");
            progressBarBackup.Value = 0;
        }





        private void backgroundWorkerRestore_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (backgroundWorkerRestore.IsBusy)
            {
                progressBarRestore.Value = e.ProgressPercentage;
            }
        }

        private void backgroundWorkerRestore_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            FMessegeBox.FarsiMessegeBox.Show("بازيابي اطلاعات با موفقيت انجام شد");
            falg = false;
            progressBarRestore.Value = 0;
        }

        private void backgroundWorkerRestore_DoWork_1(object sender, DoWorkEventArgs e)
        {

            falg = true;

                MyModel.ExecuteStoreCommand("ALTER DATABASE [" + Application.StartupPath + "\\EntityFrameWork.mdf] SET SINGLE_USER with ROLLBACK IMMEDIATE use master RESTORE DATABASE [" + Application.StartupPath + "\\EntityFrameWork.mdf] FROM DISK='" + textBoxMasirRestore.Text + "'");
                for (int i = 0; i <= 100;i++)
                {
                    Thread.Sleep(1);
                    backgroundWorkerRestore.ReportProgress(i);
                }
                
            
               // FMessegeBox.FarsiMessegeBox.Show("عمليات بازيابي با خطا روبه رو شد","اخطار",FMessegeBox.FMessegeBoxButtons.Ok,FMessegeBox.FMessegeBoxIcons.Error);
            }

        private void Backup_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (falg == true)
            {
                FMessegeBox.FarsiMessegeBox.Show("به علت عمليات ريستور نمي توانيد از فرم خارج شويد لطفا تا پايان عمليات منتظر بمانيد", "اخطار", FMessegeBox.FMessegeBoxButtons.Ok, FMessegeBox.FMessegeBoxIcons.Error);
            }
        }
            

        }


        }


    


