/// <summary>
 /// Updates the image of a field
 /// </summary>
 /// <param name="field">The field to update</param>
 /// <param name="image">The image to set as background</param>
 public void UpdateField(Field field, ImageBrush image)
 {
     LogControl.Log(field.ToString(), LogControl.LogLevel.Debug);
     if (field.GetPieces.Count <= 1)
     {
         field.Background = image;
     }
 }
        /// <summary>
        /// Resets the specified field back to it's default values
        /// </summary>
        /// <param name="field">The field to reset</param>
        public void ResetField(Field field)
        {
            LogControl.Log(field.ToString(), LogControl.LogLevel.Debug);

            field.RemovePiece(field.GetPieces.First()); // Removes every piece from the field
            if (field.GetPieces.Count < 1)
            {
                field.SetDefaultImage(); // First resets the background image
            }
            switch (field)
            {
            case White fie1:
                fie1.SetColor = GameColor.White;
                break;

            case Star fie2:
                fie2.SetColor = GameColor.White;
                break;

            case Globe fie3:
                fie3.Color = GameColor.White;
                break;
            }
        }
        // TODO Optimize movement methods
        /// <summary>
        /// Moves the piece and cleans it's path
        /// </summary>
        /// <param name="field">The field that was clicked</param>
        /// <param name="fields">the list of fields to use as board</param>
        /// <param name="player">the turn player</param>
        /// <param name="dieValue">the value of the die</param>
        public void Move(ref Field field, ref List <Field> fields, ref Player player, int dieValue)
        {
            Fields = fields;

            // Checks if there is any pieces to move
            if (field.GetPieces.Count != 0)
            {
                // The piece to move (Always chooses the first in the stack)
                Piece piece = (Piece)field.GetPieces.First();

                homeField = player.GetPlayerFields[(piece.BasePosition)];

                if (piece.GetPosition() + dieValue > 51 && piece.State != PieceState.Safe)
                {
                    piece.SetPosition((piece.GetPosition() - 52));
                }

                // The field to move to
                Field fieldToMove = fields[(piece.GetPosition() + dieValue)];

                // TODO change from exception
                if (!piece.CanMove && player.Color != piece.Color)
                {
                    LogControl.Log("The selected piece can't move", LogControl.LogLevel.Error);
                    LogControl.Log(piece.CanMove.ToString() + ", COLOR: " + player.Color, LogControl.LogLevel.Error);
                    throw new Exception("ERROR: Piece cannot move.");
                }
                else if (piece.Counter + dieValue > 55)
                {
                    UpdateField(field, field.GetDefaultImage());
                    ResetField(field);
                    piece.State = PieceState.Finished;
                    OnMove?.Invoke(this, piece); // Fires the event only if there is any listeners
                }

                else if (piece.Counter + dieValue >= 50)
                {
                    int tmpMath = piece.GetSafePosition + dieValue - 53;
                    //int tmpz =
                    fieldToMove = fields[(piece.GetSafePosition + tmpMath)];
                    PlacePiece(piece, fieldToMove, PieceState.Safe, dieValue);
                    ResetField(field);
                }

                else if (piece.State == PieceState.Home)
                {
                    fieldToMove = fields[(piece.StartPosition)];

                    UpdateField(fieldToMove, GetImage(piece.Color, fieldToMove));
                    PlacePiece(piece, fieldToMove, PieceState.InPlay, 0);
                    ResetField(field);
                }

                else if (IsPiecePlaced(fieldToMove))
                {
                    if (fieldToMove.GetPieces.Count > 1 && fieldToMove.Color != piece.Color)
                    {
                        KillPiece(ref piece);
                        return;
                    }
                }
                else if (IsSpecialField(fieldToMove) != FieldType.BaseField)
                {
                    MovementController dis = this;

                    switch (fieldToMove)
                    {
                    case Globe glo:
                        glo.Protec(ref piece, ref field, ref glo, ref dis, dieValue);
                        break;

                    case Start sta:
                        sta.Protec(ref piece, ref field, ref sta, ref dis, dieValue);
                        break;

                    case Star star:
                        star.Protec(ref piece, ref field, ref dis);
                        break;
                    }
                }
                else
                {
                    ResetField(field);
                    PlacePiece(piece, fieldToMove, PieceState.InPlay, dieValue);
                }
            }
            else
            {
                LogControl.Log("Player tried to move a token from an empty field: " + field, LogControl.LogLevel.Warning);
            }


            foreach (Piece piece in player.GetPieces())
            {
                piece.CanMove = false;
            }
        }