/// <summary>
 /// This method initializes the different classes and data
 /// </summary>
 private void InitializesClassesData()
 {
     ServoMotor.GetInstance().Lock();
     RFIDReader.GetInstance();
     DisplayLoad();
     ListOfCards.GetInstance().CardsList = SDCard.GetInstance().LoadCards();
     if (ListOfCards.GetInstance().CardsList == null)
     {
         ListOfCards.GetInstance().CardsList = new ArrayList();
     }
     RestoreInitialState();
 }
        /// <summary>
        /// When the menu to delete a card is selected
        /// </summary>
        private void DeleteCard()
        {
            switch (_deleteCardState)
            {
            case DELETE_CARD_STATE.listIsEmpty:
                LCD.GetInstance().Clear();
                if (ListOfCards.GetInstance().IsEmpty())
                {
                    LCD.GetInstance().DisplayText(GT.Color.Red, "/!\\ Aucun badge n'est enregistre /!\\", 10, LCD.GetInstance().LcdHeight / 2);
                    Thread.Sleep(2000);
                    RestoreInitialState();
                }
                else
                {
                    _deleteCardState = DELETE_CARD_STATE.selectCard;
                }
                break;

            case DELETE_CARD_STATE.selectCard:
                int positionY = 10;     // The Y position on the LCD
                int i         = 0;
                foreach (Card card in ListOfCards.GetInstance().CardsList)
                {
                    if (LCDTextFields.CursorPosition == i)     // If the cursorposition is at this char
                    {
                        LCD.GetInstance().DisplayText(GT.Color.Blue, card.Name, 10, positionY);
                    }
                    else
                    {
                        LCD.GetInstance().DisplayText(GT.Color.Black, card.Name, 10, positionY);
                    }
                    positionY += 15;     // Increment the Y position
                    i++;
                }
                LCD.GetInstance().DisplayText(GT.Color.Gray, "Pour selectionner le badge, appuyer sur le joystick", 10, LCD.GetInstance().LcdHeight - 30);

                if (_joystickX.Read() > JOYSTICK_DOWN_LEFT)                                           // If joystick is down
                {
                    LCDTextFields.CursorPosition++;                                                   // Move the cursor to the next name
                    if (LCDTextFields.CursorPosition > ListOfCards.GetInstance().CardsList.Count - 1) // If the cursor get out of the range of the list of cards array
                    {
                        LCDTextFields.CursorPosition = 0;                                             // Move to the first position of the list of cards array
                    }
                    Thread.Sleep(100);                                                                // Wait 0.1 second to prevent the cursor from moving too fast
                }
                else if (_joystickX.Read() < JOYSTICK_UP_RIGHT)                                       // If joystick is up
                {
                    LCDTextFields.CursorPosition--;                                                   // Move the cursor to the previous name
                    if (LCDTextFields.CursorPosition < 0)                                             // If the cursor get out of the range of the list array
                    {
                        LCDTextFields.CursorPosition = ListOfCards.GetInstance().CardsList.Count - 1; // Move to the last position of the list of cards array
                    }
                    Thread.Sleep(100);                                                                // Wait 0.1 second to prevent the cursor from moving too fast
                }
                if (!_joystickButton.Read())                                                          // If joystick button is press
                {
                    _deleteCardState = DELETE_CARD_STATE.save;
                }
                break;

            case DELETE_CARD_STATE.save:
                try
                {
                    ListOfCards.GetInstance().DeleteCardFromList(LCDTextFields.CursorPosition);
                    SDCard.GetInstance().SaveCards(ListOfCards.GetInstance().CardsList);
                    _deleteCardState = DELETE_CARD_STATE.success;
                }
                catch (Exception e)
                {
                    _deleteCardState = DELETE_CARD_STATE.errorMSG;
                }
                break;

            case DELETE_CARD_STATE.errorMSG:
                DisplayError();
                Thread.Sleep(2000);
                RestoreInitialState();
                break;

            case DELETE_CARD_STATE.success:
                DisplaySave();
                LCD.GetInstance().DisplayText(GT.Color.Green, "Le badge a bien ete supprime", 10, LCD.GetInstance().LcdHeight / 2);
                Thread.Sleep(2000);
                RestoreInitialState();
                break;

            default:
                _deleteCardState = DELETE_CARD_STATE.listIsEmpty;
                break;
            }
        }
        /// <summary>
        /// When the menu to add a card is selected
        /// </summary>
        private void AddCard()
        {
            switch (_addCardState)
            {
            case ADD_CARD_STATE.waitRFID:
                LCD.GetInstance().Clear();
                LCD.GetInstance().DisplayText(GT.Color.Gray, "Veuillez approcher un badge du lecteur");
                if (RFIDReader.GetInstance().IsBadgeScan)
                {
                    _addCardState = ADD_CARD_STATE.RFIDDetected;
                    LCD.GetInstance().DisplayText(GT.Color.Green, "Votre badge a ete correctement scanne", 10, LCD.GetInstance().LcdHeight / 2);
                    Thread.Sleep(2000);     // Wait 2 seconds to see the message
                }
                break;

            case ADD_CARD_STATE.RFIDDetected:
                LCD.GetInstance().Clear();
                string uid = RFIDReader.GetInstance().CurrentUid;

                if (ListOfCards.GetInstance().FindCardInlist(uid))     // If the badge scanned already exist
                {
                    _addCardState = ADD_CARD_STATE.badgeExist;
                }
                else
                {
                    _addCardState = ADD_CARD_STATE.bageDontExist;
                }
                break;

            case ADD_CARD_STATE.badgeExist:
                LCD.GetInstance().DisplayText(GT.Color.Red, "/!\\ Erreur : Ce badge est deja sauvegarde /!\\", 10, LCD.GetInstance().LcdHeight / 2);
                Thread.Sleep(2000);     // Wait 2 seconds to see the message
                RestoreInitialState();
                break;

            case ADD_CARD_STATE.bageDontExist:
                string name      = LCDTextFields.Content; // The content value of the LCD field, it's the name of the badge
                char[] charArray = name.ToCharArray();    // We split the name in a char array to make it easier to modify char by char
                int    x         = 110;                   // The position index where we're gonna write the first char

                if (LCDTextFields.ShouldBeRefresh)        // If we need to refresh because we have modify a char or the position of the cursor
                {
                    LCD.GetInstance().Clear();
                    LCD.GetInstance().DisplayText(GT.Color.Gray, "Votre badge :", 10, LCD.GetInstance().LcdHeight / 2);
                    LCD.GetInstance().DisplayText(GT.Color.Gray, "Pour valider le nom, appuyer sur le joystick", 10, LCD.GetInstance().LcdHeight - 20);
                    for (int i = 0; i < charArray.Length; i++)
                    {
                        if (i == LCDTextFields.CursorPosition)     // If the cursorposition is at this char
                        {
                            LCD.GetInstance().DisplayText(GT.Color.Blue, charArray[i].ToString(), x, LCD.GetInstance().LcdHeight / 2);
                        }
                        else
                        {
                            LCD.GetInstance().DisplayText(GT.Color.Black, charArray[i].ToString(), x, LCD.GetInstance().LcdHeight / 2);
                        }
                        x += 10;     // Increment the X position on the LCD
                    }
                    LCDTextFields.ShouldBeRefresh = false;
                }

                if (_joystickX.Read() < JOYSTICK_UP_RIGHT)       // If the joystick is up
                {
                    charArray[LCDTextFields.CursorPosition]++;   // Increment the char ex : A -> B
                    LCDTextFields.ShouldBeRefresh = true;
                    Thread.Sleep(100);                           // Wait 0.1 second to prevent the letter from scrolling too fast
                }
                else if (_joystickX.Read() > JOYSTICK_DOWN_LEFT) // If the joystick is down
                {
                    charArray[LCDTextFields.CursorPosition]--;   // ecrement the char ex : B -> A
                    LCDTextFields.ShouldBeRefresh = true;
                    Thread.Sleep(100);                           // Wait 0.1 second to prevent the letter from scrolling too fast
                }


                if (_joystickY.Read() < JOYSTICK_UP_RIGHT)                   // If the joystick is right
                {
                    LCDTextFields.CursorPosition++;                          // Move the cursor to the next char
                    if (LCDTextFields.CursorPosition > charArray.Length - 1) // If the cursor get out of the range of the char array
                    {
                        LCDTextFields.CursorPosition = 0;                    // Move to the first position of the char array
                    }
                    LCDTextFields.ShouldBeRefresh = true;
                    Thread.Sleep(200);                                       // Wait 0.2 seconds to prevent the cursor from moving too fast
                }
                else if (_joystickY.Read() > JOYSTICK_DOWN_LEFT)             // If the joystick is left
                {
                    LCDTextFields.CursorPosition--;                          // Move the cursor to the previous char
                    if (LCDTextFields.CursorPosition < 0)                    // If the cursor get out of the range of the char array
                    {
                        LCDTextFields.CursorPosition = charArray.Length - 1; // Move to the last position of the char array
                    }
                    LCDTextFields.ShouldBeRefresh = true;
                    Thread.Sleep(200);     // Wait 0.2 seconds to prevent the cursor from moving too fast
                }

                LCDTextFields.Content = new string(charArray); // Set the LCD text field with the value of the modify char array

                if (!_joystickButton.Read())                   // If joystick button is press
                {
                    _addCardState = ADD_CARD_STATE.save;
                }
                break;

            case ADD_CARD_STATE.save:
                try
                {
                    uid  = RFIDReader.GetInstance().CurrentUid;    // Get the uid of the badge that was scanned
                    name = LCDTextFields.Content;
                    ListOfCards.GetInstance().AddCardToList(name, uid);
                    SDCard.GetInstance().SaveCards(ListOfCards.GetInstance().CardsList);
                    _addCardState = ADD_CARD_STATE.successMSG;
                }
                catch (Exception e)
                {
                    _addCardState = ADD_CARD_STATE.errorMSG;
                }
                break;

            case ADD_CARD_STATE.errorMSG:
                DisplayError();
                Thread.Sleep(2000);
                RestoreInitialState();
                break;

            case ADD_CARD_STATE.successMSG:
                DisplaySave();
                LCD.GetInstance().DisplayText(GT.Color.Green, "Le badge a bien ete ajoute", 10, LCD.GetInstance().LcdHeight / 2);
                Thread.Sleep(2000);
                RestoreInitialState();
                break;

            default:
                _addCardState = ADD_CARD_STATE.waitRFID;
                break;
            }
        }