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.Data.SqlClient;


namespace AzmoonGir
{
    public partial class FrmRestore : Form
    {


        SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\DbAzmoon.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        SqlCommand cmd;
        Boolean isrestore = false;

        public FrmRestore()
        {
            InitializeComponent();
        }

        private void rectangleShape1_Click(object sender, EventArgs e)
        {

        }

        private void btnbrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "(*.bak)|*.bak";

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                txtpatch.Text = openFileDialog1.FileName;

        }

        private void btnrestore_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtpatch.Text))
                errorProvider1.SetError(btnbrowse, "مسیر فایل پشتیبان را انتخاب کنید");
            else
            {
                 errorProvider1.SetError(txtpatch, "");
                 isrestore = true;
                 btnrestore.Enabled = false;


                if(!backgroundWorker1.IsBusy)
                {
                    backgroundWorker1.RunWorkerAsync();
                }
               
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {



            //try
           // {
            this.BeginInvoke((MethodInvoker)delegate()
            {

                this.Cursor = Cursors.WaitCursor;
       
               
                cmd = new SqlCommand();
                SqlConnection.ClearAllPools();
                 cmd.Connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\DbAzmoon.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
                cmd.Connection.Open();
                string dbname = cmd.Connection.Database;
                 cmd.Connection.Close();
                string query = "USE [master]; RESTORE DATABASE [" + dbname + "]  FROM  DISK = N'" + txtpatch.Text + "' WITH NOUNLOAD,  REPLACE,  STATS = 10";
                cmd.Connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\DbAzmoon.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
                cmd.Connection.Open();
                cmd.CommandText = query;
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();

               
                

                
            this.Close();

            });
              
       
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Frm_Message Fm = new Frm_Message();
            if (e.Error == null)
            {
                isrestore = false;
                btnrestore.Enabled = true;

                Fm.lbltext.Text = "فایل پشتیبان با موفقیت بازیابی شد.هم اکنون برنامه مجددا راه اندازی خواهد شد";
                Fm.ImgClose.Visible = true;
                Fm.ImgYes.Visible = false;
                Fm.ImgNo.Visible = false;
                Fm.ShowDialog();
                Application.Restart();

            }
            else
            {
                Fm.lbltext.Text = "بازیابی فایل پشتیبان با مشکل مواجه شد";
                Fm.ImgClose.Visible = true;
                Fm.ImgYes.Visible = false;
                Fm.ImgNo.Visible = false;
                Fm.ShowDialog();
                isrestore = false;
                btnrestore.Enabled = true;

            }
        }

        private void FrmRestore_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isrestore == true)
            {
                e.Cancel = true;

                Frm_Message Fm = new Frm_Message();
                Fm.lbltext.Text = "اکیدا توصیه میکنیم در حین عملیات بازیابی پروژه  را نبندید تا از بروز مشکل جلوگیری شود";
                Fm.ImgClose.Visible = true;
                Fm.ImgYes.Visible = false;
                Fm.ImgNo.Visible = false;
                Fm.ShowDialog();




            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
      
             
        }

        private void FrmRestore_Load(object sender, EventArgs e)
        {

        }
    }
}
