using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Self_Project
{
    public class BackUP : IControlable
    {
        private string Path_Name = "";
        //========================================================================
        private String _path = "";
        public String Path
        {
            get { return _path; }
            set 
            {
                if (value == "")
                    throw new BackUP_Exceptions_Path();
                else
                    _path = value; ;
            }
        }
        //-------------------------
        private string _name = "";

        public string Name
        {
            get { return _name; }
            set
            {
                if (value == "")
                    throw new BackUP_Exceptions_Name();
                else
                    _name = value + ".Bak";
            }
        }
        //========================================================================
        public void ControlInfo()
        {
            if (_path == "")
                throw new BackUP_Exceptions_Path();
            if (_name == "")
                throw new BackUP_Exceptions_Name();
        }
        //========================================================================
        public void SetAddress()
        {
            if (_path == "")
                throw new BackUP_Exceptions_Path();
            DataBase_SQL.Action(DBOperation.Update, null, "[BackUP]", "Address", _path);
        }
        //========================================================================
        public void LoadAddress()
        {
            _path = DataBase_SQL.Action(DBOperation.Select, null, "[BackUP]", "Address").Rows[0][0].ToString();
        }
        //========================================================================
        public void CreateBackUP()
        {
            ControlInfo();
            Path_Name = System.IO.Path.Combine(_path, _name);
            if (!Directory.Exists(_path)) Directory.CreateDirectory(_path);
            if (File.Exists(Path_Name)) File.Delete(Path_Name);
            DataBase_SQL.Bank_Action("Backup Database Self_Service To Disk='" + Path_Name + "' with NoFormat, Name='Reza_Ramezani',PASSWORD = '3240012677',Skip,NoRewind,NounLoad,Stats=10");
            //--------------------------------------------------------
            String T11, T22;
            T11 = DateTime.Now.Hour.ToString();
            if (T11.Length == 1) T11 = "0" + T11;
            T11 = T11 + " : ";
            T22 = DateTime.Now.Minute.ToString();
            if (T22.Length == 1) T22 = "0" + T22;
            T11 = T11 + T22;
            T11 = T11 + " : ";
            T22 = DateTime.Now.Second.ToString();
            if (T22.Length == 1) T22 = "0" + T22;
            T11 = T11 + T22;            
            Condition Cond = new Condition(1);
            Cond.AddCondition("Address", Operatotrs.مساوی, Path_Name, Concat_Type.None);
            DataBase_SQL.Action(DBOperation.Delete, Cond, "SavedBackup", "");
            DataBase_SQL.Action(DBOperation.Insert, null, "SavedBackUP", "Tarikh,Saat,Address", Functions.FarsiDate("/"), T11, Path_Name);
        }
        //========================================================================
        public void ReadBackUP(String Name_Path)
        {
            try
            {
                if (!File.Exists(Name_Path)) throw new BackUP_Exceptions("فایل کپی پشتیبان، پیدا نشد");
                DataBase_SQL.DataBaseName = "Hasan";
                DataBase_SQL.RestartDB();
                DataBase_SQL.Bank_Action("Restore Database Self_Service From Disk='" + Name_Path + "' With File=1,PASSWORD = '3240012677',NounLoad,Stats=10");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                DataBase_SQL.DataBaseName = "Self_Service";
                DataBase_SQL.RestartDB();
            }
        }
        //========================================================================
        public void DeleteBackUP(String Name_Path)
        {
            Condition Cond = new Condition(1);
            if (System.IO.File.Exists(Name_Path)) System.IO.File.Delete(Name_Path);
            Cond.AddCondition("Address", Operatotrs.مساوی, Name_Path, Concat_Type.None);
            DataBase_SQL.Action(DBOperation.Delete, Cond, "SavedBackup", "");
        }
        //========================================================================
        public void LoadBackUPInformations(System.Windows.Forms.DataGridView DGV)
        {
            DGV.DataSource = DataBase_SQL.Action(DBOperation.Select, null, "SavedBackUP", "Tarikh,Saat,Address", "Tarikh Desc");
            //------------------------------------
            DGV.Columns[0].HeaderText = "تاریخ";
            DGV.Columns[1].HeaderText = "ساعت";
            DGV.Columns[2].HeaderText = "مسیر";
            DGV.Columns[2].Width = 230;
        }
        //========================================================================
        //========================================================================
    }
}
