using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Self_Project
{
    public class User : IControlable
    {
        public Boolean Access_Student = true, Access_Food = true, Access_Vacation = true, Access_Reserve = true, Access_BackUP = true, Access_Users = true;
        //--------------------------------------------------------------
        private String _userName = "";
        public String UserName
        {
            get { return _userName; }
            set 
            {
                if (value == "")
                    throw new User_Exceptions_UserName();
                else
                _userName = value; 
            }
        }
        //--------------------------------------------------------------
        private String _password = "";
        public String Password
        {
            get 
            {
                return _password; 
            }
            set
            {
                if (value.Length < 6)
                    throw new User_Exceptions_Password();
                else
                    _password = value;

            }
        }
        //--------------------------------------------------------------
        public void ControlInfo()
        {
            if (_userName == "")
                throw new User_Exceptions_UserName();
            if (_password.Length < 6)
                throw new User_Exceptions_Password();
        }
        //--------------------------------------------------------------
        public Boolean IsCorrectUser()
        {
            ControlInfo();
            Condition Cond = new Condition(2);
            Cond.AddCondition("UserName", Operatotrs.مساوی, _userName, Concat_Type.And);
            Cond.AddCondition("Pass", Operatotrs.مساوی, _password, Concat_Type.None);
            DataTable DT = new DataTable();
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "Users", "UserName");
            if (DT.Rows.Count > 0)
            {
                _userName = DT.Rows[0][0].ToString();
                return true;
            }
            else
                return false;
        }
        //--------------------------------------------------------------
        public void ShowUsers(System.Windows.Forms.ComboBox CMB)
        {
            CMB.DataSource = DataBase_SQL.Action(DBOperation.Select, null, "Users", "UserName", "UserName");
            CMB.DisplayMember = "UserName";
            CMB.ValueMember = "UserName";
        }
        //--------------------------------------------------------------
        public Boolean IsDuplicate()
        {
            Condition Cond = new Condition(1);
            DataTable DT = new DataTable();
            Cond.AddCondition("UserName", Operatotrs.مساوی, _userName, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "Users", "UserName");
            if (DT.Rows.Count > 0) return true; else return false;
        }
        //--------------------------------------------------------------
        public void CreateUser()
        {
            ControlInfo();
            DataBase_SQL.Action(DBOperation.Insert, null, "Users", "UserName,Pass,Access_Student, Access_Food, Access_Vacation, Access_Reserve, Access_BackUP, Access_Users", _userName, _password, Access_Student, Access_Food, Access_Vacation, Access_Reserve, Access_BackUP, Access_Users);
        }
        //--------------------------------------------------------------
        public void ShowInfo(String UserName)
        {
            Condition Cond = new Condition(1);
            DataTable DT = new DataTable();
            Cond.AddCondition("UserName", Operatotrs.مساوی, UserName, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "Users", "UserName,Pass");
            _userName = DT.Rows[0]["UserName"].ToString();
            _password = DT.Rows[0]["Pass"].ToString();
        }
        //--------------------------------------------------------------
        public void ChangeUserInfo(String UserName)
        {
            if (Access_Users == false)
            {
                DataTable DT = new DataTable();
                DT = DataBase_SQL.Bank_Select("Select UserName From Users Where UserName<>'" + UserName + "' And Access_Users=1");
                if (DT.Rows.Count == 0) throw new User_Exceptions_NoManageUserRemine();
            }
            Condition Cond = new Condition(1);
            Cond.AddCondition("UserName", Operatotrs.مساوی, UserName, Concat_Type.None);
            DataBase_SQL.Action(DBOperation.Update, Cond, "Users", "UserName,Pass,Access_Student, Access_Food, Access_Vacation, Access_Reserve, Access_BackUP, Access_Users", _userName, _password, Access_Student, Access_Food, Access_Vacation, Access_Reserve, Access_BackUP, Access_Users);
        }
        //--------------------------------------------------------------
        public void DeleteUser()
        {
            DataTable DT = new DataTable();
            DT = DataBase_SQL.Bank_Select("Select UserName From Users Where UserName<>'" + _userName + "' And Access_Users=1");
            if (DT.Rows.Count == 0) throw new User_Exceptions_NoManageUserRemine();
            Condition Cond = new Condition(1);
            Cond.AddCondition("UserName", Operatotrs.مساوی, _userName);
            DataBase_SQL.Action(DBOperation.Delete, Cond, "Users", "");
        }
        //--------------------------------------------------------------
        public void LoadAccess(String UserName)
        {
            DataTable DT = new DataTable();
            Condition Cond = new Condition(1);
            Cond.AddCondition("UserName", Operatotrs.مساوی, UserName);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "Users", "Access_Student, Access_Food, Access_Vacation, Access_Reserve, Access_BackUP, Access_Users");
            Access_Student = Convert.ToBoolean(DT.Rows[0]["Access_Student"]);
            Access_Food = Convert.ToBoolean(DT.Rows[0]["Access_Food"]);
            Access_Vacation = Convert.ToBoolean(DT.Rows[0]["Access_Vacation"]);
            Access_Reserve = Convert.ToBoolean(DT.Rows[0]["Access_Reserve"]);
            Access_BackUP = Convert.ToBoolean(DT.Rows[0]["Access_BackUP"]);
            Access_Users = Convert.ToBoolean(DT.Rows[0]["Access_Users"]);
        }

    }
}
