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

namespace Self_Project
{
    class Food : IControlable
    {
        //=================================================================
        public enum FoodTypes
        {
            صبحانه, نهار, شام, هیچکدام
        }
        //--------------------
        public FoodTypes FoodType;
        public int Radif = 0, Radif_BreakFast = 0, Radif_Launch = 0, Radif_Dinner = 0;
        //--------------------
        private String _tarikh = "";

        public String Tarikh
        {
            get { return _tarikh; }
            set
            {
                if (value.Length != 5)
                    throw new Food_Exceptions_Tarikh();
                else
                    _tarikh = value;
            }
        }
        //--------------------
        private string _foodName;

        public string FoodName
        {
            get { return _foodName; }
            set 
            {
                if (value == "")
                    throw new Food_Exceptions_FoodName();
                else
                    _foodName = value;
            }
        }
        //=================================================================
        public void ControlInfo()
        {
            if (_foodName == "")
                throw new Food_Exceptions_FoodName();
        }
        //=================================================================
        private Boolean ControlDuplicate()
        {
            byte T;
            if (FoodType == FoodTypes.صبحانه)
                T = 1;
            else if (FoodType == FoodTypes.نهار)
                T = 2;
            else
                T = 3;
            Condition Cond = new Condition(2);
            Cond.AddCondition("FoodType", Operatotrs.مساوی, T, Concat_Type.And);
            Cond.AddCondition("FoodName", Operatotrs.مساوی, _foodName, Concat_Type.None);
            if (DataBase_SQL.Action(DBOperation.Select, Cond, "FoodName", "Radif").Rows.Count > 0)
                return true;
            else
                return false;
        }
        //=================================================================
        public void Create()
        {
            ControlInfo();
            byte T;
            if (FoodType == FoodTypes.صبحانه)
                T = 1;
            else if (FoodType == FoodTypes.نهار)
                T = 2;
            else
                T = 3;
            //-----------------
            if (ControlDuplicate())
                throw new Food_Exceptions("نام غذای وارد شده تکراری است");
            DataBase_SQL.Action(DBOperation.Insert, null, "FoodName", "FoodType,FoodName", T, _foodName);
        }
        //=================================================================
        public void Update()
        {
            ControlInfo();
            //-----------------
            if (ControlDuplicate())
                throw new Food_Exceptions("نام غذای وارد شده تکراری است");
            Condition Cond = new Condition(1);
            Cond.AddCondition("Radif", Operatotrs.مساوی, Radif, Concat_Type.None);
            DataBase_SQL.Action(DBOperation.Update, Cond, "FoodName", "FoodName", _foodName);
        }
        //=================================================================
        public void ShowFoodNames(FoodTypes FT,System.Windows.Forms.ListBox LST)
        {
            DataTable DT = new DataTable();
            byte T;
            if (FT == FoodTypes.صبحانه)
                T = 1;
            else if (FT == FoodTypes.نهار)
                T = 2;
            else
                T = 3;
            Condition Cond = new Condition(1);
            Cond.AddCondition("FoodType", Operatotrs.مساوی, T, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "FoodName", "Radif,FoodName", "FoodName");
            LST.DataSource = DT;
            LST.DisplayMember = "FoodName";
            LST.ValueMember = "Radif";
        }
        //=================================================================
        public void ShowFoodNames(FoodTypes FT, System.Windows.Forms.ComboBox CMB)
        {
            DataTable DT = new DataTable();
            byte T;
            if (FT == FoodTypes.صبحانه)
                T = 1;
            else if (FT == FoodTypes.نهار)
                T = 2;
            else
                T = 3;
            Condition Cond = new Condition(1);
            Cond.AddCondition("FoodType", Operatotrs.مساوی, T, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond, "FoodName", "Radif,FoodName", "FoodName");
            CMB.DataSource = DT;
            CMB.DisplayMember = "FoodName";
            CMB.ValueMember = "Radif";
        }
        //=================================================================
        public void SaveReserveFood()
        {
            Condition Cond = new Condition(1);
            Cond.AddCondition("Tarikh", Operatotrs.مساوی, _tarikh, Concat_Type.None);
            DataBase_SQL.Action(DBOperation.Delete, Cond, "Food_Reserve", "");
            DataBase_SQL.Action(DBOperation.Insert, null, "Food_Reserve", "Tarikh,BreakFast,Launch,Dinner", _tarikh, Radif_BreakFast, Radif_Launch, Radif_Dinner);
        }
        //=================================================================
        public string GiveFoodName(String WeekNum, Int32 Row, Int32 Col)
        {
            Condition Cond1 = new Condition(1);
            Condition Cond2 = new Condition(1);
            DataTable DT = new DataTable();
            Cond1.AddCondition("Num", Operatotrs.مساوی, WeekNum, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond1, "HafteDetail", "HafteEsm");
            string Tarikh = Functions.AddDate(DT.Rows[0][0].ToString(), Col);
            string FieldName;
            if (Row == 0)
                FieldName = "BreakFast";
            else if (Row == 1)
                FieldName = "Launch";
            else
                FieldName = "Dinner";
            Cond2.AddCondition("Tarikh", Operatotrs.مساوی, Tarikh, Concat_Type.None);
            DT = DataBase_SQL.Action(DBOperation.Select, Cond2, "Food_Reserve INNER JOIN FoodName ON Food_Reserve." + FieldName + " = FoodName.Radif", "FoodName.FoodName");
            if (DT.Rows.Count == 0)
                return "نا مشخص";
            else
                return DT.Rows[0][0].ToString();
        }
        //=======================================================
        public void SetFoodTime(String T11, String T12, String T21, String T22, String T31, String T32)
        {
            DataBase_SQL.Action(DBOperation.Update, null, "FoodTime", "T11,T12,T21,T22,T31,T32", T11, T12, T21, T22, T31, T32);
        }
        //=======================================================
        public void LoadFoodTime(MaskedTextBox T1, MaskedTextBox T2, MaskedTextBox T3, MaskedTextBox T4, MaskedTextBox T5, MaskedTextBox T6)
        {
            DataTable DT = new DataTable();
            DT = DataBase_SQL.Action(DBOperation.Select, null, "FoodTime", "*");
            T1.Text = DT.Rows[0][0].ToString();
            T2.Text = DT.Rows[0][1].ToString();
            T3.Text = DT.Rows[0][2].ToString();
            T4.Text = DT.Rows[0][3].ToString();
            T5.Text = DT.Rows[0][4].ToString();
            T6.Text = DT.Rows[0][5].ToString();
        }
        //=================================================================
        public FoodTypes WhichFoodTime()
        {
            DataTable DT = new DataTable();
            DT = DataBase_SQL.Action(DBOperation.Select, null, "FoodTime", "T11,T12,T21,T22,T31,T32");
            if (DT.Rows.Count == 0)
            {
                return FoodTypes.هیچکدام;
            }
            //-----------------------------------------
            DateTime T11, T12, T21, T22, T31, T32, Time;
            T11 = Convert.ToDateTime(DT.Rows[0][0]);
            T12 = Convert.ToDateTime(DT.Rows[0][1]);
            T21 = Convert.ToDateTime(DT.Rows[0][2]);
            T22 = Convert.ToDateTime(DT.Rows[0][3]);
            T31 = Convert.ToDateTime(DT.Rows[0][4]);
            T32 = Convert.ToDateTime(DT.Rows[0][5]);
            Time = DateTime.Now;
            //-----------------------------------------
            if (!((Time >= T11 && Time <= T12) || (Time >= T21 && Time <= T22) || (Time >= T31 && Time <= T32)))
                return FoodTypes.هیچکدام;
            else if (Time >= T11 && Time <= T12)
                return FoodTypes.صبحانه;
            else if (Time >= T21 && Time <= T22)
                return FoodTypes.نهار;
            else
                return FoodTypes.شام;
        }
        //=================================================================
        public void EatFood(String Hafte, Int32 Rooz, String StdNum)
        {
            string FName = "";
            if (Rooz == 1) FName = "Day11";
            if (Rooz == 2) FName = "Day12";
            if (Rooz == 3) FName = "Day13";
            if (Rooz == 4) FName = "Day21";
            if (Rooz == 5) FName = "Day22";
            if (Rooz == 6) FName = "Day23";
            if (Rooz == 7) FName = "Day31";
            if (Rooz == 8) FName = "Day32";
            if (Rooz == 9) FName = "Day33";
            if (Rooz == 10) FName = "Day41";
            if (Rooz == 11) FName = "Day42";
            if (Rooz == 12) FName = "Day43";
            if (Rooz == 13) FName = "Day51";
            if (Rooz == 14) FName = "Day52";
            if (Rooz == 15) FName = "Day53";
            if (Rooz == 16) FName = "Day61";
            if (Rooz == 17) FName = "Day62";
            if (Rooz == 18) FName = "Day63";
            if (Rooz == 19) FName = "Day71";
            if (Rooz == 20) FName = "Day72";
            if (Rooz == 21) FName = "Day73";
            //-------------------------------
            Condition Cond = new Condition(2);
            Cond.AddCondition("Num", Operatotrs.مساوی, Hafte, Concat_Type.And);
            Cond.AddCondition("ID", Operatotrs.مساوی, StdNum, Concat_Type.None);
            DataBase_SQL.Action(DBOperation.Update, Cond, "ReservedFood", FName, 0);
        }
        //=================================================================
        //=================================================================
        //=================================================================
    }
}
