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;
using System.Globalization;
using Stimulsoft.Report.Dictionary;
using Stimulsoft.Report;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Rendering;
using System.IO;

namespace Library_Red_Diamonds
{
    public partial class restore : Form
    {
        public restore()
        {
            InitializeComponent();
        }

        static SqlConnection con = new SqlConnection(Connection.con);
        OpenFileDialog dlg = new OpenFileDialog();
        int _time = 60;

        private void restoredata()
        {
            if (txt_address.Text == "")
            {
                timer.Enabled = false;
                MessageBox.Show("آدرسی برای بازیابی پشتیبان انتخاب نشده است", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                timer.Enabled = true;
                MessageBox.Show("در حین بازیابی پشتیبان به چیزی دست نزنید", "هشدار", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                string rs;
                rs = @"use master ALTER DATABASE [" + Application.StartupPath + "\\Red-Diamonds-DB.mdf]  SET SINGLE_USER with ROLLBACK IMMEDIATE " +
                        "RESTORE DATABASE [" + Application.StartupPath + "\\Red-Diamonds-DB.mdf] FROM DISK='" + dlg.FileName + "' with REPLACE " +
                        "ALTER DATABASE [" + Application.StartupPath + "\\Red-Diamonds-DB.mdf] SET MULTI_USER ";
                SqlCommand com = new SqlCommand(rs, con);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
            }
        }
     

        private void btn_browse_Click_1(object sender, EventArgs e)
        {
           
            dlg.Filter = "Backup Files(*.BAK)|*.BAK";
            dlg.FilterIndex = 1;
            dlg.Title = "بازیابی پشتیبان";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txt_address.Text = dlg.FileName.ToString();
            }
        }

        private void btn_restore_Click_1(object sender, EventArgs e)
        {
            restoredata();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (_time < 60 && progressbar.Value < 97)
                progressbar.Value += 2;
            _time -= 1;
            lblTime.Text = _time.ToString();
            lblProgress.Text = progressbar.Value.ToString() + "%";
            if (_time == 0)
            {
                progressbar.Value = 100;
                lblProgress.Text = "100%";
                timer.Enabled = false;
                MessageBox.Show("عملیات بازیابی پشتیبان با موفقیت انجام شد", "پیغام", MessageBoxButtons.OK, MessageBoxIcon.Information);
                progressbar.Value = 0;
                lblProgress.Text = "0%";
                _time = 60;
                Application.Restart();
            }
        }
    }
}
