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;

namespace NurseRosterProb
{
    public partial class Form_Surgry : Form
    {
        public Form_Surgry()
        {
            InitializeComponent();
        }

        Cls_Surgery OBJ_MainTab = new Cls_Surgery();

        string[,] MainTab = new string[18, 15];

        int[] Ran_Nurse = { -1, -1, -1, -1, -1, -1, -1 };
        int[] Ran_Prev = { -1, -1, -1, -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, 7);
                stop = false;
                if (R == LastArrItem)
                    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";
                        break;
                    }
                }
                LastArrItem = Ran_Nurse[6];
                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";
                    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];

            textBox_N12_D1.Text = MainTab[11, 1]; textBox_N12_D2.Text = MainTab[11, 2]; textBox_N12_D3.Text = MainTab[11, 3];
            textBox_N12_D4.Text = MainTab[11, 4]; textBox_N12_D5.Text = MainTab[11, 5]; textBox_N12_D6.Text = MainTab[11, 6];
            textBox_N12_D7.Text = MainTab[11, 7]; textBox_N12_D8.Text = MainTab[11, 8]; textBox_N12_D9.Text = MainTab[11, 9];
            textBox_N12_D10.Text = MainTab[11, 10]; textBox_N12_D11.Text = MainTab[11, 11]; textBox_N12_D12.Text = MainTab[11, 12];
            textBox_N12_D13.Text = MainTab[11, 13]; textBox_N12_D14.Text = MainTab[11, 14];

            textBox_N13_D1.Text = MainTab[12, 1]; textBox_N13_D2.Text = MainTab[12, 2]; textBox_N13_D3.Text = MainTab[12, 3];
            textBox_N13_D4.Text = MainTab[12, 4]; textBox_N13_D5.Text = MainTab[12, 5]; textBox_N13_D6.Text = MainTab[12, 6];
            textBox_N13_D7.Text = MainTab[12, 7]; textBox_N13_D8.Text = MainTab[12, 8]; textBox_N13_D9.Text = MainTab[12, 9];
            textBox_N13_D10.Text = MainTab[12, 10]; textBox_N13_D11.Text = MainTab[12, 11]; textBox_N13_D12.Text = MainTab[12, 12];
            textBox_N13_D13.Text = MainTab[12, 13]; textBox_N13_D14.Text = MainTab[12, 14];

            textBox_N14_D1.Text = MainTab[13, 1]; textBox_N14_D2.Text = MainTab[13, 2]; textBox_N14_D3.Text = MainTab[13, 3];
            textBox_N14_D4.Text = MainTab[13, 4]; textBox_N14_D5.Text = MainTab[13, 5]; textBox_N14_D6.Text = MainTab[13, 6];
            textBox_N14_D7.Text = MainTab[13, 7]; textBox_N14_D8.Text = MainTab[13, 8]; textBox_N14_D9.Text = MainTab[13, 9];
            textBox_N14_D10.Text = MainTab[13, 10]; textBox_N14_D11.Text = MainTab[13, 11]; textBox_N14_D12.Text = MainTab[13, 12];
            textBox_N14_D13.Text = MainTab[13, 13]; textBox_N14_D14.Text = MainTab[13, 14];

            textBox_N15_D1.Text = MainTab[14, 1]; textBox_N15_D2.Text = MainTab[14, 2]; textBox_N15_D3.Text = MainTab[14, 3];
            textBox_N15_D4.Text = MainTab[14, 4]; textBox_N15_D5.Text = MainTab[14, 5]; textBox_N15_D6.Text = MainTab[14, 6];
            textBox_N15_D7.Text = MainTab[14, 7]; textBox_N15_D8.Text = MainTab[14, 8]; textBox_N15_D9.Text = MainTab[14, 9];
            textBox_N15_D10.Text = MainTab[14, 10]; textBox_N15_D11.Text = MainTab[14, 11]; textBox_N15_D12.Text = MainTab[14, 12];
            textBox_N15_D13.Text = MainTab[14, 13]; textBox_N15_D14.Text = MainTab[14, 14];

            textBox_N16_D1.Text = MainTab[15, 1]; textBox_N16_D2.Text = MainTab[15, 2]; textBox_N16_D3.Text = MainTab[15, 3];
            textBox_N16_D4.Text = MainTab[15, 4]; textBox_N16_D5.Text = MainTab[15, 5]; textBox_N16_D6.Text = MainTab[15, 6];
            textBox_N16_D7.Text = MainTab[15, 7]; textBox_N16_D8.Text = MainTab[15, 8]; textBox_N16_D9.Text = MainTab[15, 9];
            textBox_N16_D10.Text = MainTab[15, 10]; textBox_N16_D11.Text = MainTab[15, 11]; textBox_N16_D12.Text = MainTab[15, 12];
            textBox_N16_D13.Text = MainTab[15, 13]; textBox_N16_D14.Text = MainTab[15, 14];

            textBox_N17_D1.Text = MainTab[16, 1]; textBox_N17_D2.Text = MainTab[16, 2]; textBox_N17_D3.Text = MainTab[16, 3];
            textBox_N17_D4.Text = MainTab[16, 4]; textBox_N17_D5.Text = MainTab[16, 5]; textBox_N17_D6.Text = MainTab[16, 6];
            textBox_N17_D7.Text = MainTab[16, 7]; textBox_N17_D8.Text = MainTab[16, 8]; textBox_N17_D9.Text = MainTab[16, 9];
            textBox_N17_D10.Text = MainTab[16, 10]; textBox_N17_D11.Text = MainTab[16, 11]; textBox_N17_D12.Text = MainTab[16, 12];
            textBox_N17_D13.Text = MainTab[16, 13]; textBox_N17_D14.Text = MainTab[16, 14];

            textBox_N18_D1.Text = MainTab[17, 1]; textBox_N18_D2.Text = MainTab[17, 2]; textBox_N18_D3.Text = MainTab[17, 3];
            textBox_N18_D4.Text = MainTab[17, 4]; textBox_N18_D5.Text = MainTab[17, 5]; textBox_N18_D6.Text = MainTab[17, 6];
            textBox_N18_D7.Text = MainTab[17, 7]; textBox_N18_D8.Text = MainTab[17, 8]; textBox_N18_D9.Text = MainTab[17, 9];
            textBox_N18_D10.Text = MainTab[17, 10]; textBox_N18_D11.Text = MainTab[17, 11]; textBox_N18_D12.Text = MainTab[17, 12];
            textBox_N18_D13.Text = MainTab[17, 13]; textBox_N18_D14.Text = MainTab[17, 14];
        }

        private void Form_Surgry_Load(object sender, EventArgs e)
        {
            btn_Reset.PerformClick();
        }

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

        public void d()
        {
        }


        private void btn_Generate_Click(object sender, EventArgs e)
        {
            btn_Reset.PerformClick();
            MainTab = OBJ_MainTab.Function_MainTab(MainTab, 0);
            
            var watch = System.Diagnostics.Stopwatch.StartNew();
            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[20, 3];
            int DB_NextRow;
            int M_C, E_C;
            int C_OffDays_NOW;
            while (Day_C <= 14)
            {
                for (int i = 0; i < 20; i++)
                    for (int j = 0; j < 3; j++)
                        DB_nurse_poss_off[i, j] = -1;
                DB_NextRow = 0;

                C_OffDays_NOW = 0;
                M_C = 0;
                E_C = 0;
                for (int i = 0; i < 18; i++) ///////
                {
                    if (MainTab[i, Day_C] == "O")
                        C_OffDays_NOW++;
                    if (MainTab[i, Day_C] == "M")
                        M_C++;
                    if (MainTab[i, Day_C] == "E")
                        E_C++;
                }

                if (C_OffDays_NOW == 0)
                    C_OffDays = 6;
                else
                    C_OffDays = 6 - C_OffDays_NOW;

                for (int Nurse_Loop = 0; Nurse_Loop < 18; Nurse_Loop++)
                {
                    SumNurWorkDay = 0;
                    Cell_OffD_Nurse_ptr = Day_C - 1;
                    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
                                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];
                        }

                    if (SumNurWorkDay >= 2)
                    {
                        TotalOffRow = 0;
                        for (int NurDays_Loop = 0; NurDays_Loop < 14; NurDays_Loop++)
                            if (MainTab[Nurse_Loop, NurDays_Loop] == "O")
                                TotalOffRow++;
                    }
                    else
                        continue;

                    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;
                        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] = TotalOffRow;
                            DB_NextRow++;
                        }
                }

                if (DB_NextRow != 0)
                {
                    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)
                                {
                                    switch (C_OffDays)
                                    {
                                        case 1:
                                            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:
                                            // Finding 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:
                                            // Finding the 3-max nurse2 (till now no use)
                                            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;
                                        case 4:
                                            // Finding the 4-max nurse2 (till now no use)
                                            int Mx1WrkNurse = DB_nurse_poss_off[0, 1];
                                            int N_MxNurse1 = DB_nurse_poss_off[0, 0];
                                            for (int i = 1; i < DB_NextRow; i++)
                                            {
                                                if (DB_nurse_poss_off[i, 1] > Mx1WrkNurse)
                                                {
                                                    Mx1WrkNurse = DB_nurse_poss_off[i, 1];
                                                    N_MxNurse1 = DB_nurse_poss_off[i, 0];
                                                }
                                            }
                                            int Mx2WrkNurse = 0;
                                            int N_MxNurse2 = 0;
                                            for (int i = 0; i < DB_NextRow; i++)
                                            {
                                                if (DB_nurse_poss_off[i, 0] == N_MxNurse1)
                                                    continue;
                                                else
                                                    if (DB_nurse_poss_off[i, 1] > Mx2WrkNurse)
                                                    {
                                                        Mx2WrkNurse = DB_nurse_poss_off[i, 1];
                                                        N_MxNurse2 = DB_nurse_poss_off[i, 0];
                                                    }
                                            }
                                            int Mx3WrkNurse = 0;
                                            int N_MxNurse3 = 0;
                                            for (int i = 0; i < DB_NextRow; i++)
                                            {
                                                if (DB_nurse_poss_off[i, 0] == N_MxNurse1 || DB_nurse_poss_off[i, 0] == N_MxNurse2)
                                                    continue;
                                                else
                                                    if (DB_nurse_poss_off[i, 1] > Mx3WrkNurse)
                                                    {
                                                        Mx3WrkNurse = DB_nurse_poss_off[i, 1];
                                                        N_MxNurse3 = DB_nurse_poss_off[i, 0];
                                                    }
                                            }
                                            int Mx4WrkNurse = 0;
                                            int N_MxNurse4 = 0;
                                            for (int i = 0; i < DB_NextRow; i++)
                                            {
                                                if (DB_nurse_poss_off[i, 0] == N_MxNurse1 || DB_nurse_poss_off[i, 0] == N_MxNurse2 || DB_nurse_poss_off[i, 0] == N_MxNurse3)
                                                    continue;
                                                else
                                                    if (DB_nurse_poss_off[i, 1] > Mx4WrkNurse)
                                                    {
                                                        Mx4WrkNurse = DB_nurse_poss_off[i, 1];
                                                        N_MxNurse4 = DB_nurse_poss_off[i, 0];
                                                    }
                                            }
                                            MainTab[N_MxNurse1, Day_C] = "O";
                                            MainTab[N_MxNurse2, Day_C] = "O";
                                            MainTab[N_MxNurse3, Day_C] = "O";
                                            MainTab[N_MxNurse4, Day_C] = "O";
                                            break;
                                        default:
                                            MessageBox.Show("You need to add another case ;)");
                                            break;
                                    }
                                }
                                else
                                {
                                    // this when have less poss-nurse than what we need from/of Off-Days
                                    for (int i = 0; i < DB_NextRow; i++)
                                        MainTab[DB_nurse_poss_off[i, 0], Day_C] = "O";
                                }
                }

                M_C = 0;
                for (int i = 0; i < 18; i++)
                {
                    if (M_C == 4)
                        break;
                    else
                        if (MainTab[i, Day_C] == null)
                        {
                            MainTab[i, Day_C] = "M";
                            M_C++;
                        }
                }
                E_C = 0;
                for (int i = 0; i < 18; i++)
                {
                    if (E_C == 4)
                        break;
                    else
                        if (MainTab[i, Day_C] == null)
                        {
                            MainTab[i, Day_C] = "E";
                            E_C++;
                        }
                }

                for (int i = 0; i < 18; i++)
                    if (MainTab[i, Day_C] == null)
                        MainTab[i, Day_C] = "W";
                Day_C++;
                C_OffDays = 0;
            }

            // Nnext is enhacement the produced timetable and looking for weekend issue ;)
            for (int i = 0; i < 18; i++)
            {
                if (MainTab[i, 6] == "W" && MainTab[i, 2] == "O")
                {
                    MainTab[i, 6] = "O";
                    MainTab[i, 2] = "E";
                }
            }
            for (int i = 0; i < 18; i++)
                for (int j = 0; j < 15; j++)
                    if (MainTab[i, j] == "W")
                        MainTab[i, j] = "E";
            MainTab[12, 14] = "O";
            MainTab[13, 14] = "O";
            MainTab[12, 02] = "E";
            MainTab[13, 02] = "E";
            PrintSchedule();
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Double ELAPSE = Convert.ToDouble(elapsedMs);
            ELAPSE = ELAPSE / 1000;
            label_Elapse.Text = ELAPSE.ToString() + " s";
            int Penalty = 0;
            int pena = 0;
            int N_C = 0;
            int wd_i;
            bool Shift_Similar;
            string[] WD_Arr = { null, null, null, null };
            while (N_C < 18)
            {
                // NNNNOO
                for (int Days_C = 1; Days_C <= 14; Days_C++)
                    if (MainTab[N_C, Days_C] == "N" && Days_C < 6)
                        if (MainTab[N_C, Days_C + 6] == "M")
                        { Penalty++; pena++; break; }
                        else
                            continue;
                // EEEE-or-MMMMM
                wd_i = 0;
                Shift_Similar = true;
                for (int Days_C = 1; Days_C <= 14; Days_C++)

                    if (Days_C == 14 && wd_i > 0 && MainTab[N_C, Days_C] != "O")
                    {
                        for (int i = 0; i < wd_i - 1; i++)
                            if (WD_Arr[i] != WD_Arr[i + 1])
                            { Shift_Similar = false; break; }
                        if (Shift_Similar == false)
                        { Penalty += 10; pena += 10; }
                    }
                    else
                        if (MainTab[N_C, Days_C] != "O")
                        { WD_Arr[wd_i] = MainTab[N_C, Days_C]; wd_i++; }
                        else
                            if (wd_i > 0)
                                if (wd_i < 4)
                                { Penalty += 10; wd_i = 0; pena += 10; }
                                else
                                {
                                    for (int i = 0; i < 3; i++)
                                        if (WD_Arr[i] != WD_Arr[i + 1])
                                        { Shift_Similar = false; break; }
                                    if (Shift_Similar == false)
                                    { Penalty += 10; wd_i = 0; Shift_Similar = true; pena += 10; }
                                    else
                                        wd_i = 0;
                                }
                N_C++;
                richTextBox1.AppendText(N_C.ToString() + " -> " + pena.ToString() + Environment.NewLine);
                pena = 0;
            }
            label_Penalty.Text = Penalty.ToString();
        }
    }
}
