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

namespace Self_Project
{
    public class Student : IControlable
    {
        //===============================================================
        private String _name = "";
        public String Name
        {
            get { return _name; }
            set 
            {
                if (value == "")
                    throw new Student_Exceptions_Name();
                else
                    _name = value;
            }
        }
        //===============================================================
        private String _family = "";
        public String Family
        {
            get { return _family; }
            set 
            {
                if (value == "")
                    throw new Student_Exceptions_Family();
                else
                    _family = value;
            }
        }
        //===============================================================
        private Int32 _iD = 0;
        public Int32 ID
        {
            get { return _iD; }
            set 
            {
                if (value < 111111)
                    throw new Student_Exceptions_ID();
                else
                    _iD = value; 
            }
        }
        //===============================================================
        private Int32 _cartID = 0;
        public Int32 CartID
        {
            get { return _cartID; }
            set 
            {
                if (value < 111111)
                    throw new Student_Exceptions_CartID();
                else
                    _cartID = value; 
            }
        }
        //===============================================================
        private Int32 _price = -1;
        public Int32 Price
        {
            get { return _price; }
            set 
            {
                if (value < 0)
                    throw new Student_Exceptions_Price();
                else
                    _price = value; 
            }
        }
        //===============================================================
        private SByte _type = -1;
        public SByte Type
        {
            get { return _type; }
            set 
            {
                if (value < 0)
                    throw new Student_Exceptions_Type();
                else
                    _type = value; 
            }
        }
        //===============================================================
        private Boolean _active = true;
        public Boolean Active
        {
            get { return _active; }
            set { _active = value; }
        }
        //===============================================================
        public void ControlInfo()
        {
            if (_name == "")
                throw new Student_Exceptions_Name();
            if (_family == "")
                throw new Student_Exceptions_Family();
            if (_iD < 111111)
                throw new Student_Exceptions_ID();
            if (_cartID < 111111)
                throw new Student_Exceptions_CartID();
            if (_price < 0)
                throw new Student_Exceptions_Price();
            if (_type < 0)
                throw new Student_Exceptions_Type();
        }
        //===============================================================
        public enum SearchType
        {
            None, StudentID, CartID, Name_Family
        }
        //------------------
        public DataTable Search_Student(SearchType ST,params String[] Values)
        {
            if (ST != SearchType.None && Values.Length == 0) throw new Student_Exceptions("اطلاعات وارد شده برای جستجو، ناقص است");
            if (ST == SearchType.None)
            {
                return DataBase_SQL.Action(DBOperation.Select, null, "Students Inner Join Model On Students.Model=Model.Model", "StdNum,CartNum,(Students.Esm)as F1,Famil,Price,(Model.Esm)as F2,Active", "Famil,Students.Esm");
            }
            if (ST == SearchType.StudentID)
            {
                Condition Cond = new Condition(1);
                Cond.AddCondition("StdNum", Operatotrs.مساوی, Values[0], Concat_Type.None);
                return DataBase_SQL.Action(DBOperation.Select, Cond, "Students Inner Join Model On Students.Model=Model.Model", "StdNum,CartNum,(Students.Esm)as F1,Famil,Price,(Model.Esm)as F2,Active", "Famil,Students.Esm");
            }
            if (ST == SearchType.CartID)
            {
                Condition Cond = new Condition(1);
                Cond.AddCondition("CartNum", Operatotrs.مساوی, Values[0], Concat_Type.None);
                return DataBase_SQL.Action(DBOperation.Select, Cond, "Students Inner Join Model On Students.Model=Model.Model", "StdNum,CartNum,(Students.Esm)as F1,Famil,Price,(Model.Esm)as F2,Active", "Famil,Students.Esm");
            }
            if (ST == SearchType.Name_Family)
            {
                if (Values.Length < 2) throw new Student_Exceptions("اطلاعات وارد شده برای جستجو، ناقص است");
                return DataBase_SQL.Bank_Select("Select StdNum,CartNum,(Students.Esm)as F1,Famil,Price,(Model.Esm)as F2,Active From Students Inner Join Model On Students.Model=Model.Model Where (Students.Esm) Like '%" + Values[0] + "%' And Famil Like '%" + Values[1] + "%' Order By Famil,Students.Esm");
            }
            return null;
        }
        //===============================================================
        public void ShowInfo(String Student_ID)
        {
            DataTable DT = new DataTable();
            Condition Cond = new Condition(1);
            Cond.AddCondition("StdNum", Operatotrs.مساوی, Student_ID, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "Students", "StdNum,CartNum,Esm,Famil,Price,Model,Active");            
            //---------
            _iD = Convert.ToInt32(DT.Rows[0]["StdNum"]);
            _cartID = Convert.ToInt32(DT.Rows[0]["CartNum"]);
            _name = DT.Rows[0]["Esm"].ToString();
            _family = DT.Rows[0]["Famil"].ToString();
            _price=Convert.ToInt32( DT.Rows[0]["Price"]);
            _type = Convert.ToSByte(DT.Rows[0]["Model"]);
            _active = Convert.ToBoolean(DT.Rows[0]["Active"]);
        }
        //===============================================================
        public void ShowInfo(long Cart_ID)
        {
            DataTable DT = new DataTable();
            Condition Cond = new Condition(1);
            Cond.AddCondition("CartNum", Operatotrs.مساوی, Cart_ID, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "Students", "StdNum,CartNum,Esm,Famil,Price,Model,Active");
            //---------
            _iD = Convert.ToInt32(DT.Rows[0]["StdNum"]);
            _cartID = Convert.ToInt32(DT.Rows[0]["CartNum"]);
            _name = DT.Rows[0]["Esm"].ToString();
            _family = DT.Rows[0]["Famil"].ToString();
            _price = Convert.ToInt32(DT.Rows[0]["Price"]);
            _type = Convert.ToSByte(DT.Rows[0]["Model"]);
            _active = Convert.ToBoolean(DT.Rows[0]["Active"]);
        }
        //===============================================================
        public enum ChangeType
        {
            Student,Price,Active
        }
        //----------
        public void ChangeInfo(ChangeType CT)
        {
            Condition Cond = new Condition(1);
            if (CT == ChangeType.Active)
            {
                Cond.AddCondition("StdNum", Operatotrs.مساوی, _iD, Concat_Type.None);
                DataBase_SQL.Action(DBOperation.Update, Cond, "Students", "Active", Convert.ToByte(_active));
            }
            //-----------------
            if (CT == ChangeType.Price)
            {
                Cond.AddCondition("StdNum", Operatotrs.مساوی, _iD, Concat_Type.None);
                DataBase_SQL.Action(DBOperation.Update, Cond, "Students", "Price", _price);
            }
        }
        //===============================================================
        public void ChangeInfo(ChangeType CT,String StdNum)
        {
            Condition Cond = new Condition(1);
            //-----------------
            if (CT == ChangeType.Student)
            {
                ControlInfo();
                Cond.AddCondition("StdNum", Operatotrs.مساوی, StdNum, Concat_Type.None);
                DataBase_SQL.Action(DBOperation.Update, Cond, "Students", "StdNum,CartNum,Esm,Famil,Price,Model", _iD, _cartID, _name, _family, _price, Convert.ToInt16(_type));
                if (_iD.ToString() != StdNum)
                {
                    Condition Cond2 = new Condition(1);
                    Cond2.AddCondition("ID", Operatotrs.مساوی, StdNum, Concat_Type.None);
                    DataBase_SQL.Action(DBOperation.Update, Cond2, "ReservedFood", "ID", _iD);
                }
            }
        }
        //===============================================================
        public enum ControlType
        {
            Student_ID, Cart_ID
        }
        public Boolean IsDuplicate(ControlType CT)
        {
            Condition Cond=new Condition(1);
            DataTable DT = new DataTable();
            if (CT == ControlType.Student_ID)
                Cond.AddCondition("StdNum", Operatotrs.مساوی, _iD, Concat_Type.None);
            else
                Cond.AddCondition("CartNum", Operatotrs.مساوی, _cartID, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "Students", "Active");
            if (DT.Rows.Count > 0) return true; else return false;
        }
        //===============================================================
        public void CreateStudent()
        {
            ControlInfo();
            DataBase_SQL.Action(DBOperation.Insert, null, "Students", "StdNum,CartNum,Esm,Famil,Price,Model,Active", _iD, _cartID, _name, _family, _price,Convert.ToInt16(_type), 1);
        }
        //===============================================================
        public void DeleteStudent()
        {
            Condition Cond1 = new Condition(1);
            Condition Cond2 = new Condition(1);
            Cond1.AddCondition("StdNum", Operatotrs.مساوی, _iD, Concat_Type.None);
            Cond2.AddCondition("ID", Operatotrs.مساوی, _iD, Concat_Type.None);
            DataBase_SQL.Action(DBOperation.Delete, Cond1, "Students", "");
            DataBase_SQL.Action(DBOperation.Delete, Cond2, "ReservedFood", "");
        }
        //===============================================================
        //===============================================================
    }
}
