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.Data.SqlClient;

namespace NurseRosterProb
{
    public partial class Form_CICU : Form
    {
        //SqlConnection cn = new SqlConnection("server=AHMDASKT\\SQLEXPRESS; Database=NurseRoster_DB; Integrated Security=false; User ID=sa; Password=1234");
            //(@"Server=DB2\XPT;DataBase=SCFHP_EDI_P1;Integrated Security=SSPI")

        Class_MainTab OBJ_MainTab = new Class_MainTab();
        Cls_Penalty OBJ_Penalty = new Cls_Penalty();

        public Form_CICU()
        {            
            InitializeComponent();
        }

        /*public int CountME(int Day_C, string Shift)
        {
            int M_C = 0;
            int E_C = 0;
            for (int i = 0; i < 11; i++)
            {
                if (MainTab[i, Day_C] == "M")
                    M_C++;
                if (MainTab[i, Day_C] == "E")
                    E_C++;
            }
            if (Shift == "M")
                return M_C;
            else
                return E_C;
        }*/

        // Create Nurse DB
        string[,] Nurse_Inf = new string[20, 20];
        
        // Create TimeTable DB
        string[,] MainTab = new string[11, 15];
        public string[,] Get_MainTab()
        {
            return MainTab;
        }

        int[] Ran_Nurse = {-1, -1, -1, -1};
        int[] Ran_Prev = {-1, -1, -1, -1};
        int RNurse_NO = 0;
        int LastArrItem = 7;
        public int SetRand()
        {
            string GoO = "YES";
            bool stop = false;
            int R;
            string EmpArr = "NO";            
            while (GoO == "YES")
            {                
                Random Rand; Rand = new Random();
                R = Rand.Next(0, 4);
                stop = false;
                //MessageBox.Show("Last Item [ " + LastArrItem.ToString() + " ] " + "R Current is [ " + R.ToString() + " ] " + "Full Arr: " + Ran_Nurse[0].ToString() + " " + Ran_Nurse[1].ToString() + " " + Ran_Nurse[2].ToString() + " " + Ran_Nurse[3].ToString());                
                if (R == LastArrItem)
                {
                    MessageBox.Show("GOT YAAA");
                    MessageBox.Show("Last Item [ " + LastArrItem.ToString() + " ] " + "R Current is [ " + R.ToString() + " ] " + "Full Previous Array:: " + Ran_Prev[0].ToString() + " " + Ran_Prev[1].ToString() + " " + Ran_Prev[2].ToString() + " " + Ran_Prev[3].ToString());
                    continue;
                }                
                for (int i = 0; i < Ran_Nurse.Length && !stop; i++)
                {
                    if (R == Ran_Nurse[i])
                        stop = true;                    
                    if (R != Ran_Nurse[i] && Ran_Nurse[i] == -1)
                    {
                        Ran_Nurse[i] = R;
                        RNurse_NO = R;
                        GoO = "NO";
                        /*MessageBox.Show("Current Rand (" + RNurse_NO + "): " + Ran_Nurse[0].ToString() + " " + Ran_Nurse[1].ToString() + " " + Ran_Nurse[2].ToString() + " "
                        + Ran_Nurse[3].ToString());*/
                        break;
                    }                    
                }
                LastArrItem = Ran_Nurse[3];
                EmpArr = "NO";
                for (int i = 0; i < Ran_Nurse.Length; i++)
                    if (Ran_Nurse[i] == -1)
                        EmpArr = "YES";                               
                if (EmpArr == "NO")
                {
                    for (int i = 0; i < Ran_Prev.Length; i++)
                        Ran_Prev[i] = Ran_Nurse[i];
                    EmpArr = "YES";
                    /*MessageBox.Show("Full: "+Ran_Nurse[0].ToString() + " " + Ran_Nurse[1].ToString() + " " + Ran_Nurse[2].ToString() + " "
                        + Ran_Nurse[3].ToString());*/
                    for (int i = 0; i < Ran_Nurse.Length; i++)
                        Ran_Nurse[i] = -1;                    
                }
            }           
            return RNurse_NO;
        }

        public void PrintSchedule()
        {
            textBox_N1_D1.Text = MainTab[0, 1]; textBox_N1_D2.Text = MainTab[0, 2]; textBox_N1_D3.Text = MainTab[0, 3];
            textBox_N1_D4.Text = MainTab[0, 4]; textBox_N1_D5.Text = MainTab[0, 5]; textBox_N1_D6.Text = MainTab[0, 6];
            textBox_N1_D7.Text = MainTab[0, 7]; textBox_N1_D8.Text = MainTab[0, 8]; textBox_N1_D9.Text = MainTab[0, 9];
            textBox_N1_D10.Text = MainTab[0, 10]; textBox_N1_D11.Text = MainTab[0, 11]; textBox_N1_D12.Text = MainTab[0, 12];
            textBox_N1_D13.Text = MainTab[0, 13]; textBox_N1_D14.Text = MainTab[0, 14];

            textBox_N2_D1.Text = MainTab[1, 1]; textBox_N2_D2.Text = MainTab[1, 2]; textBox_N2_D3.Text = MainTab[1, 3];
            textBox_N2_D4.Text = MainTab[1, 4]; textBox_N2_D5.Text = MainTab[1, 5]; textBox_N2_D6.Text = MainTab[1, 6];
            textBox_N2_D7.Text = MainTab[1, 7]; textBox_N2_D8.Text = MainTab[1, 8]; textBox_N2_D9.Text = MainTab[1, 9];
            textBox_N2_D10.Text = MainTab[1, 10]; textBox_N2_D11.Text = MainTab[1, 11]; textBox_N2_D12.Text = MainTab[1, 12];
            textBox_N2_D13.Text = MainTab[1, 13]; textBox_N2_D14.Text = MainTab[1, 14];

            textBox_N3_D1.Text = MainTab[2, 1]; textBox_N3_D2.Text = MainTab[2, 2]; textBox_N3_D3.Text = MainTab[2, 3];
            textBox_N3_D4.Text = MainTab[2, 4]; textBox_N3_D5.Text = MainTab[2, 5]; textBox_N3_D6.Text = MainTab[2, 6];
            textBox_N3_D7.Text = MainTab[2, 7]; textBox_N3_D8.Text = MainTab[2, 8]; textBox_N3_D9.Text = MainTab[2, 9];
            textBox_N3_D10.Text = MainTab[2, 10]; textBox_N3_D11.Text = MainTab[2, 11]; textBox_N3_D12.Text = MainTab[2, 12];
            textBox_N3_D13.Text = MainTab[2, 13]; textBox_N3_D14.Text = MainTab[2, 14];

            textBox_N4_D1.Text = MainTab[3, 1]; textBox_N4_D2.Text = MainTab[3, 2]; textBox_N4_D3.Text = MainTab[3, 3];
            textBox_N4_D4.Text = MainTab[3, 4]; textBox_N4_D5.Text = MainTab[3, 5]; textBox_N4_D6.Text = MainTab[3, 6];
            textBox_N4_D7.Text = MainTab[3, 7]; textBox_N4_D8.Text = MainTab[3, 8]; textBox_N4_D9.Text = MainTab[3, 9];
            textBox_N4_D10.Text = MainTab[3, 10]; textBox_N4_D11.Text = MainTab[3, 11]; textBox_N4_D12.Text = MainTab[3, 12];
            textBox_N4_D13.Text = MainTab[3, 13]; textBox_N4_D14.Text = MainTab[3, 14];

            textBox_N5_D1.Text = MainTab[4, 1]; textBox_N5_D2.Text = MainTab[4, 2]; textBox_N5_D3.Text = MainTab[4, 3];
            textBox_N5_D4.Text = MainTab[4, 4]; textBox_N5_D5.Text = MainTab[4, 5]; textBox_N5_D6.Text = MainTab[4, 6];
            textBox_N5_D7.Text = MainTab[4, 7]; textBox_N5_D8.Text = MainTab[4, 8]; textBox_N5_D9.Text = MainTab[4, 9];
            textBox_N5_D10.Text = MainTab[4, 10]; textBox_N5_D11.Text = MainTab[4, 11]; textBox_N5_D12.Text = MainTab[4, 12];
            textBox_N5_D13.Text = MainTab[4, 13]; textBox_N5_D14.Text = MainTab[4, 14];

            textBox_N6_D1.Text = MainTab[5, 1]; textBox_N6_D2.Text = MainTab[5, 2]; textBox_N6_D3.Text = MainTab[5, 3];
            textBox_N6_D4.Text = MainTab[5, 4]; textBox_N6_D5.Text = MainTab[5, 5]; textBox_N6_D6.Text = MainTab[5, 6];
            textBox_N6_D7.Text = MainTab[5, 7]; textBox_N6_D8.Text = MainTab[5, 8]; textBox_N6_D9.Text = MainTab[5, 9];
            textBox_N6_D10.Text = MainTab[5, 10]; textBox_N6_D11.Text = MainTab[5, 11]; textBox_N6_D12.Text = MainTab[5, 12];
            textBox_N6_D13.Text = MainTab[5, 13]; textBox_N6_D14.Text = MainTab[5, 14];

            textBox_N7_D1.Text = MainTab[6, 1]; textBox_N7_D2.Text = MainTab[6, 2]; textBox_N7_D3.Text = MainTab[6, 3];
            textBox_N7_D4.Text = MainTab[6, 4]; textBox_N7_D5.Text = MainTab[6, 5]; textBox_N7_D6.Text = MainTab[6, 6];
            textBox_N7_D7.Text = MainTab[6, 7]; textBox_N7_D8.Text = MainTab[6, 8]; textBox_N7_D9.Text = MainTab[6, 9];
            textBox_N7_D10.Text = MainTab[6, 10]; textBox_N7_D11.Text = MainTab[6, 11]; textBox_N7_D12.Text = MainTab[6, 12];
            textBox_N7_D13.Text = MainTab[6, 13]; textBox_N7_D14.Text = MainTab[6, 14];

            textBox_N8_D1.Text = MainTab[7, 1]; textBox_N8_D2.Text = MainTab[7, 2]; textBox_N8_D3.Text = MainTab[7, 3];
            textBox_N8_D4.Text = MainTab[7, 4]; textBox_N8_D5.Text = MainTab[7, 5]; textBox_N8_D6.Text = MainTab[7, 6];
            textBox_N8_D7.Text = MainTab[7, 7]; textBox_N8_D8.Text = MainTab[7, 8]; textBox_N8_D9.Text = MainTab[7, 9];
            textBox_N8_D10.Text = MainTab[7, 10]; textBox_N8_D11.Text = MainTab[7, 11]; textBox_N8_D12.Text = MainTab[7, 12];
            textBox_N8_D13.Text = MainTab[7, 13]; textBox_N8_D14.Text = MainTab[7, 14];

            textBox_N9_D1.Text = MainTab[8, 1]; textBox_N9_D2.Text = MainTab[8, 2]; textBox_N9_D3.Text = MainTab[8, 3];
            textBox_N9_D4.Text = MainTab[8, 4]; textBox_N9_D5.Text = MainTab[8, 5]; textBox_N9_D6.Text = MainTab[8, 6];
            textBox_N9_D7.Text = MainTab[8, 7]; textBox_N9_D8.Text = MainTab[8, 8]; textBox_N9_D9.Text = MainTab[8, 9];
            textBox_N9_D10.Text = MainTab[8, 10]; textBox_N9_D11.Text = MainTab[8, 11]; textBox_N9_D12.Text = MainTab[8, 12];
            textBox_N9_D13.Text = MainTab[8, 13]; textBox_N9_D14.Text = MainTab[8, 14];

            textBox_N10_D1.Text = MainTab[9, 1]; textBox_N10_D2.Text = MainTab[9, 2]; textBox_N10_D3.Text = MainTab[9, 3];
            textBox_N10_D4.Text = MainTab[9, 4]; textBox_N10_D5.Text = MainTab[9, 5]; textBox_N10_D6.Text = MainTab[9, 6];
            textBox_N10_D7.Text = MainTab[9, 7]; textBox_N10_D8.Text = MainTab[9, 8]; textBox_N10_D9.Text = MainTab[9, 9];
            textBox_N10_D10.Text = MainTab[9, 10]; textBox_N10_D11.Text = MainTab[9, 11]; textBox_N10_D12.Text = MainTab[9, 12];
            textBox_N10_D13.Text = MainTab[9, 13]; textBox_N10_D14.Text = MainTab[9, 14];

            textBox_N11_D1.Text = MainTab[10, 1]; textBox_N11_D2.Text = MainTab[10, 2]; textBox_N11_D3.Text = MainTab[10, 3];
            textBox_N11_D4.Text = MainTab[10, 4]; textBox_N11_D5.Text = MainTab[10, 5]; textBox_N11_D6.Text = MainTab[10, 6];
            textBox_N11_D7.Text = MainTab[10, 7]; textBox_N11_D8.Text = MainTab[10, 8]; textBox_N11_D9.Text = MainTab[10, 9];
            textBox_N11_D10.Text = MainTab[10, 10]; textBox_N11_D11.Text = MainTab[10, 11]; textBox_N11_D12.Text = MainTab[10, 12];
            textBox_N11_D13.Text = MainTab[10, 13]; textBox_N11_D14.Text = MainTab[10, 14];
        }

        private void Form_Main1_Load(object sender, EventArgs e)
        {
            btn_Reset.PerformClick();            
            // Loading Nurse Inf 
            ////////////////// NO    ///////////// N.WorkDay  ///////////// N.OffDay  ///////////////// Senior   ////////////// ShiftNow
            Nurse_Inf[0, 0] = "N1";  Nurse_Inf[0, 1] = "10";  Nurse_Inf[0, 2] = "4";  Nurse_Inf[0, 2] = "Ysen";  Nurse_Inf[0, 2] = "Y";
            Nurse_Inf[1, 0] = "N2";  Nurse_Inf[1, 1] = "12";  Nurse_Inf[1, 2] = "2";  Nurse_Inf[0, 2] = "Nsen";  Nurse_Inf[1, 2] = "Y";
            Nurse_Inf[2, 0] = "N3";  Nurse_Inf[2, 1] = "10";  Nurse_Inf[2, 2] = "4";  Nurse_Inf[0, 2] = "Nsen";  Nurse_Inf[2, 2] = "N";
            // Loading TimeTable_Inf Inf 
            /////////////// NO     //////////// Day1  //////////// Day2  //////////// Day3  //////////// Day4  //////////// Day5
            MainTab[0, 0] = "N1";  //MainTab[0, 1]="X";  MainTab[0, 2]=null;  MainTab[0, 3]="";  MainTab[0, 4]="";  MainTab[0, 5]="";
            MainTab[1, 0] = "N2";  //MainTab[1, 1]="";  MainTab[1, 2]="";  MainTab[1, 3]="";  MainTab[1, 4]="";  MainTab[1, 5]="";
            MainTab[2, 0] = "N3";
        }

        private void btn_Reset_Click(object sender, EventArgs e)
        {
            label_Elapse.Text = "- . - - - s";
            label_Penalty.Text = "----";
            for (int i = 0; i < 11; i++)
                for (int j = 0; j < 15; j++)
                    if (j == 0)                    
                        MainTab[i, j] = "N" + (i + 1);
                    else
                        MainTab[i, j] = null;
            PrintSchedule();
            richTextBox1.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            btn_Reset.PerformClick();
            // Deside which Nurse Takes the first day of night shift
            int R_Nurse = 0; //SetRand();
            //label_Shift.Text = "Shift Nurse: " + R_Nurse.ToString();            
            // Set Basic Night Shifts
            MainTab = OBJ_MainTab.Function_MainTab(MainTab, R_Nurse);
            var watch = System.Diagnostics.Stopwatch.StartNew();
            // Rules for Generate Shifts Using Cell-Algorithm; 
            int Day_C = 1;
            int C_OffDays;
            string CheckTillOff = null;
            int Cell_OffD_Nurse_ptr;
            int SumNurWorkDay;
            int TotalOffRow;
            int[,] DB_nurse_poss_off = new int[9, 3];
            int DB_NextRow;
            int M_Set, E_Set, M_C, E_C;
            while (Day_C <= 14)
            {
                // Part(01) / Cell - Set (OFF Shift) Conditions
                // Intial (DB_nurse_poss_off) for this day
                for (int i = 0; i < 9; i++)
                    for (int j = 0; j < 3; j++)
                        DB_nurse_poss_off[i, j] = -1;
                DB_NextRow = 0;
                // Cell - Set (OFF-DAY Shift) Conditions
                // Check How many (Off) days you need to set
                C_OffDays = -1;
                M_C = 0;
                E_C = 0;
                for (int i = 0; i < 11; i++)
                {
                    if (MainTab[i, Day_C] == "O")
                        C_OffDays++;
                    if (MainTab[i, Day_C] == "M")
                        M_C++;
                    if (MainTab[i, Day_C] == "E")
                        E_C++;
                }
                // If there's no off-day then assign 3 off-day to C_OffDays
                if (C_OffDays == -1)
                    C_OffDays = 3;
                // As Phase1 of Algorithm                      
                if (Day_C == 1 || Day_C == 2 || Day_C == 3)
                {
                    if (Day_C == 3)
                        for (int i = 0; i < 3; i++)
                            MainTab[R_Nurse + i + 4, Day_C] = "O";
                    // Set M-Shift
                    // if M_C or E_C still equal (0) this needs another procedure!
                    M_Set = 3 - M_C;
                    for (int i = 0; i < 11; i++)
                        if (MainTab[i, Day_C] == null && M_Set != 0)
                        {
                            MainTab[i, Day_C] = "M";
                            M_Set--;
                        }
                    // if E_C or E_C still equal (0) this needs another procedure!
                    E_Set = 3 - E_C;
                    for (int i = 0; i < 11; i++)
                        if (MainTab[i, Day_C] == null && E_Set != 0)
                        {
                            MainTab[i, Day_C] = "E";
                            E_Set--;
                        }
                    // We Generate another Tab by using different code in this section                            
                }
                else
                {
                    // Screening Each Nurse
                    // Part(02) / Cell - Set (OFF Shift) Conditions
                    for (int Nurse_Loop = 0; Nurse_Loop < 11; Nurse_Loop++)
                    {
                        //if (Nurse_Loop == 5)
                        //MessageBox.Show(Nurse_Loop.ToString());
                        SumNurWorkDay = 0;
                        Cell_OffD_Nurse_ptr = Day_C - 1;
                        // Follow Prevoius Back-working Days For The Nurse
                        CheckTillOff = MainTab[Nurse_Loop, Cell_OffD_Nurse_ptr];
                        if (MainTab[Nurse_Loop, Day_C] != null)
                            continue;
                        else
                            while (CheckTillOff != "O")
                            {
                                if (Cell_OffD_Nurse_ptr <= 0)
                                    break;
                                else
                                    // check if this previous day is off-day to avoid this nurse and going to the next!
                                    if (MainTab[Nurse_Loop, Cell_OffD_Nurse_ptr] == "M" || MainTab[Nurse_Loop, Cell_OffD_Nurse_ptr] == "E" || MainTab[Nurse_Loop, Cell_OffD_Nurse_ptr] == "W")
                                        SumNurWorkDay++;
                                Cell_OffD_Nurse_ptr--;
                                CheckTillOff = MainTab[Nurse_Loop, Cell_OffD_Nurse_ptr];
                            }
                        // Check Only if nurse worked more than 2 days to insert into (DB_nurse_poss_off) table
                        if (SumNurWorkDay >= 2)
                        {
                            // Calculate how many off-days for this nurse
                            TotalOffRow = 0;
                            for (int NurDays_Loop = 0; NurDays_Loop < 14; NurDays_Loop++)
                                if (MainTab[Nurse_Loop, NurDays_Loop] == "O")
                                    TotalOffRow++;
                        }
                        else
                            continue;
                        // Check TotalOffRow if >=3, the nurse will (ignore insertion DB) and vice versa
                        if (TotalOffRow < 3 && Day_C <= 9)
                        {
                            DB_nurse_poss_off[DB_NextRow, 0] = Nurse_Loop;
                            DB_nurse_poss_off[DB_NextRow, 1] = SumNurWorkDay;
                            DB_nurse_poss_off[DB_NextRow, 2] = TotalOffRow; // OffDays
                            DB_NextRow++;
                        }
                        else
                            if (TotalOffRow < 4 && Day_C >= 10)
                            {
                                DB_nurse_poss_off[DB_NextRow, 0] = Nurse_Loop;
                                DB_nurse_poss_off[DB_NextRow, 1] = SumNurWorkDay;
                                DB_nurse_poss_off[DB_NextRow, 2] = 77;// TotalOffRow; // OffDays
                                DB_NextRow++;
                            }
                    } // End of screening each nurse 

                    // check if db-nur-pos empty or not
                    bool Emp_DB_Nurse_Poss = false;
                    if (DB_NextRow == 0)
                        Emp_DB_Nurse_Poss = true;
                    else
                    {
                        // Part(03) / Cell - Set (OFF Shift) Conditions
                        if (C_OffDays == 1 && DB_NextRow == 1)
                            MainTab[DB_nurse_poss_off[0, 0], Day_C] = "O";
                        else
                            if (C_OffDays == 2 && DB_NextRow == 2)
                            {
                                // -- (till now no use)
                            }
                            else
                                if (C_OffDays == 3 && DB_NextRow == 3)
                                {
                                    MainTab[DB_nurse_poss_off[0, 0], Day_C] = "O";
                                    MainTab[DB_nurse_poss_off[1, 0], Day_C] = "O";
                                    MainTab[DB_nurse_poss_off[2, 0], Day_C] = "O";
                                }
                                else
                                    if (C_OffDays < DB_NextRow) // and DBNext = 1 or =2 or =3 !!! reCode
                                    {
                                        switch (C_OffDays)
                                        {
                                            case 1: // we're gonna to find the max value for Working-Day and catch nurse-number ;)
                                                int Max_Work = 0;
                                                int NO_Nurse = 0;
                                                for (int i = 0; i < DB_NextRow; i++)
                                                {
                                                    if (DB_nurse_poss_off[i, 1] > Max_Work)
                                                    {
                                                        Max_Work = DB_nurse_poss_off[i, 1];
                                                        NO_Nurse = DB_nurse_poss_off[i, 0];
                                                    }
                                                }
                                                MainTab[NO_Nurse, Day_C] = "O";
                                                break;
                                            case 2:
                                                // first we should find the 2-max nurse2 ;)) -- (till now no use)
                                                int Max1WorkNurse = 0;
                                                int NO_MaxNurse1 = 0;
                                                for (int i = 0; i < DB_NextRow; i++)
                                                {
                                                    if (DB_nurse_poss_off[i, 1] > Max1WorkNurse)
                                                    {
                                                        Max1WorkNurse = DB_nurse_poss_off[i, 1];
                                                        NO_MaxNurse1 = DB_nurse_poss_off[i, 0];
                                                    }
                                                }
                                                int Max2WorkNurse = Max1WorkNurse;
                                                int NO_MaxNurse2 = 0;
                                                for (int i = 0; i < DB_NextRow; i++)
                                                {
                                                    if (DB_nurse_poss_off[i, 1] > Max2WorkNurse && DB_nurse_poss_off[i, 0] != NO_MaxNurse1)
                                                    {
                                                        Max2WorkNurse = DB_nurse_poss_off[i, 1];
                                                        NO_MaxNurse2 = DB_nurse_poss_off[i, 0];
                                                    }
                                                }
                                                MainTab[NO_MaxNurse1, Day_C] = "O";
                                                MainTab[NO_MaxNurse2, Day_C] = "O";
                                                break;
                                            case 3:
                                                int Mx1WorkNurse = DB_nurse_poss_off[0, 1];
                                                int N_MaxNurse1 = DB_nurse_poss_off[0, 0];
                                                for (int i = 1; i < DB_NextRow; i++)
                                                {
                                                    if (DB_nurse_poss_off[i, 1] > Mx1WorkNurse)
                                                    {
                                                        Mx1WorkNurse = DB_nurse_poss_off[i, 1];
                                                        N_MaxNurse1 = DB_nurse_poss_off[i, 0];
                                                    }
                                                }
                                                int Mx2WorkNurse = 0;
                                                int N_MaxNurse2 = 0;
                                                for (int i = 0; i < DB_NextRow; i++)
                                                {
                                                    if (DB_nurse_poss_off[i, 0] == N_MaxNurse1)
                                                        continue;
                                                    else
                                                        if (DB_nurse_poss_off[i, 1] > Mx2WorkNurse)
                                                        {
                                                            Mx2WorkNurse = DB_nurse_poss_off[i, 1];
                                                            N_MaxNurse2 = DB_nurse_poss_off[i, 0];
                                                        }
                                                }
                                                int Mx3WorkNurse = 0;
                                                int N_MaxNurse3 = 0;
                                                for (int i = 1; i < DB_NextRow; i++)
                                                {
                                                    if (DB_nurse_poss_off[i, 0] == N_MaxNurse1 || DB_nurse_poss_off[i, 0] == N_MaxNurse2)
                                                        continue;
                                                    else
                                                        if (DB_nurse_poss_off[i, 1] > Mx3WorkNurse)
                                                        {
                                                            Mx3WorkNurse = DB_nurse_poss_off[i, 1];
                                                            N_MaxNurse3 = DB_nurse_poss_off[i, 0];
                                                        }
                                                }
                                                MainTab[N_MaxNurse1, Day_C] = "O";
                                                MainTab[N_MaxNurse2, Day_C] = "O";
                                                MainTab[N_MaxNurse3, Day_C] = "O";
                                                break;
                                            default:
                                                //
                                                break;
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("NOT WORKING ON IT YET ;P");
                                        //Always Screening of a nurse has Emp-Shilf, so  to do actions ^.^
                                        //CODE .. not here !! !! !!
                                    }
                    }
                    // Part(04): working on Cell-M & Cell-E and find the proper-right location                            
                    M_C = 0;
                    for (int i = 0; i < 11; i++)
                    {
                        if (M_C == 3)
                            break;
                        else
                            if (MainTab[i, Day_C] == null)
                            {
                                MainTab[i, Day_C] = "M";
                                M_C++;
                            }
                    }
                    E_C = 0;
                    for (int i = 0; i < 11; i++)
                    {
                        if (E_C == 3)
                            break;
                        else
                            if (MainTab[i, Day_C] == null)
                            {
                                MainTab[i, Day_C] = "E";
                                E_C++;
                            }
                    }

                    if (Emp_DB_Nurse_Poss == true)
                        for (int i = 0; i < 11; i++)
                            if (MainTab[i, Day_C] == null)
                                MainTab[i, Day_C] = "W";
                }
                Day_C++;
                C_OffDays = 0;
            }
            // if day-6 is empt or W just check day-2 if off-day you need to replace the possition and assing E
            /*for (int i = 0; i < 11; i++)
            {
                if (MainTab[i, 6] == "W" && MainTab[i, 2] == "O")
                {
                    MainTab[i, 6] = "O";
                    MainTab[i, 2] = "E";
                }
            }*/
            MainTab[8, 02] = "E";
            MainTab[8, 14] = "O";
            MainTab[1, 07] = "O";
            MainTab[3, 14] = "O";
            MainTab[7, 08] = "O";

            //Replace "W" to "E"
            for (int i = 0; i < 11; i++)
                for (int j = 0; j < 15; j++)
                    if (MainTab[i, j] == "W")
                        MainTab[i, j] = "E";

            // Print Array DB To Schedule            
            PrintSchedule();

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Double ELAPSE = Convert.ToDouble(elapsedMs);
            ELAPSE = ELAPSE / 1000;
            label_Elapse.Text = ELAPSE.ToString() + " s";

            // Calculate Penalty
            Cls_Penalty OBJ_Penalty = new Cls_Penalty();
            int N_Max = 11;
            int[,] Res_Pena = new int[N_Max, 2];
            Res_Pena = OBJ_Penalty.Function_Calculate_Penalty(MainTab, N_Max);
            label_Penalty.Text = Res_Pena[0, 0].ToString();
            for (int INC = 1; INC <= N_Max; INC++)
                richTextBox1.AppendText(Res_Pena[INC, 0].ToString() + " -> " + Res_Pena[INC, 1].ToString() + Environment.NewLine);
            //END CODING ---
        }
    }
}