using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;



namespace GymSystem
{
    public partial class FrmLogin : Form
    {
        DbGymEntities2 database = new DbGymEntities2();
        Boolean BlResult = false;

        public Boolean BSabteNam = false;
        public Boolean BModiriatAza = false;
        public Boolean BSavabeq = false;
        public Boolean BForooshgahManage = false;
        public Boolean BForoosh = false;
        public Boolean Bkomod = false;
        public Boolean BAdminManagement = false;
        public Boolean BDoBackUp = false;
        public Boolean BDoRestore = false;




        public FrmLogin()
        {
            InitializeComponent();
        }

        private void Checking()
        {
            var query = (from users in database.TblLogins where users.UserName == txtUserName.Text && users.UserPass == txtUserPass.Text select users);

            if (query.Count() != 0)
            {

                var sUser = (from users in database.TblLogins where users.UserName == txtUserName.Text && users.UserPass == txtUserPass.Text select users).SingleOrDefault();


                BlResult = true;
                this.BackgroundImage = PicTrue.Image;
                
                BSabteNam = sUser.SabteNam;
                BModiriatAza = sUser.ModiriatAza;
                BSavabeq = sUser.Savabeq;
                BForooshgahManage = sUser.ForooshgahManage;
                BForoosh = sUser.Foroosh;
                Bkomod = sUser.komod;
                BAdminManagement = sUser.AdminManagement;
                BDoBackUp = sUser.DoBackUp;
                BDoRestore = sUser.DoRestore;

            }
            else
            {
                BlResult = false;
                this.BackgroundImage = PicFalse.Image;
            }


        }



        private void FrmLogin_Load(object sender, EventArgs e)
        {

            StreamReader sr = new StreamReader(Application.StartupPath + "\\ServerName.bas");
            string StrServerName = sr.ReadLine();
            sr.Close();
            SqlConnection con;

            try
            {
                con = new SqlConnection("Server = " + StrServerName + " ; DataBase = DbGym ; Integrated Security = True");
                con.Open();
                con.Close();

            }
            catch
            {
                con = new SqlConnection("Server = " + StrServerName + " ; DataBase = master ; Integrated Security = True");

                con.Open();

                string str = "use master;" +
                "EXECUTE sp_attach_db @dbname = N'DbGym' , " +
                " @filename1 = N'" + System.Environment.CurrentDirectory + "\\DbGym.mdf'," +
                "@filename2 = N'" + System.Environment.CurrentDirectory + "\\DbGym_log.ldf'";

                SqlCommand cmd = new SqlCommand(str, con);
                cmd.CommandTimeout = 100;
                cmd.ExecuteNonQuery();
                con.Close();

                Application.Restart();
            }

            





            Checking();
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            Checking();
        }

        private void txtUserPass_TextChanged(object sender, EventArgs e)
        {
            Checking();
        }

        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                if (BlResult == true)
                {
                    this.Close();
                }
            }
            else if (Convert.ToInt32(e.KeyChar) == 27)
            {
                Application.Exit();
            }


        }

        private void txtUserPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                if (BlResult == true)
                {
                    this.Close();
                }
            }
            else if (Convert.ToInt32(e.KeyChar) == 27)
            {
                Application.Exit();
            }
        }

        private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (BlResult == false)
            {
                Application.Exit();
            }

        }
          
    }
}
