/// <summary>Sets up the game state ready for the <see cref="AnimalQueryMenuStage.Main"/> stage when transitionaing from a different stage (not initially loading up the menu).</summary>
 private void SetupMainStage()
 {
     Game1.currentLocation = Game1.player.currentLocation;
     Game1.currentLocation.resetForPlayerEntry();
     Game1.globalFadeToClear(() => CurrentStage = AnimalQueryMenuStage.Main);
     Game1.displayHUD     = true;
     Game1.viewportFreeze = false;
     Game1.displayFarmer  = true;
     this.snapToDefaultClickableComponent();
 }
        /// <summary>Invoked when the left mouse button is clicked.</summary>
        /// <param name="x">The X position of the mouse.</param>
        /// <param name="y">The Y position of the mouse.</param>
        /// <param name="playSound">Whether sound should be played.</param>
        public override void receiveLeftClick(int x, int y, bool playSound = true)
        {
            if (CurrentStage == AnimalQueryMenuStage.Main)
            {
                // move home button
                if (MoveHomeButton.containsPoint(x, y))
                {
                    CurrentStage = AnimalQueryMenuStage.Rehoming;
                    Game1.playSound("smallSelect");
                    Game1.globalFadeToBlack(SetupRehomingStage);
                }

                // sell button
                if (SellButton.containsPoint(x, y))
                {
                    CurrentStage = AnimalQueryMenuStage.ConfirmingSell;
                    Game1.playSound("smallSelect");
                    this.snapToDefaultClickableComponent();
                }

                // allow reproduction button
                if (AllowReproductionButton != null && AllowReproductionButton.containsPoint(x, y))
                {
                    Game1.playSound("drumkit6");
                    Animal.allowReproduction.Value = !Animal.allowReproduction;

                    if (Animal.allowReproduction)
                    {
                        AllowReproductionButton.sourceRect.X = 128;
                    }
                    else
                    {
                        AllowReproductionButton.sourceRect.X = 137;
                    }
                }

                // ok button
                if (OkButton.containsPoint(x, y))
                {
                    NameTextBox.Selected = false;
                    Game1.exitActiveMenu();
                    if (NameTextBox.Text.Length > 0 && !Utility.areThereAnyOtherAnimalsWithThisName(NameTextBox.Text))
                    {
                        Animal.displayName = NameTextBox.Text;
                        Animal.Name        = NameTextBox.Text;
                    }
                    Game1.playSound("smallSelect");
                }

                // name text box
                NameTextBox.Update();
            }
            else if (CurrentStage == AnimalQueryMenuStage.Rehoming)
            {
                // back button
                if (BackButton.containsPoint(x, y))
                {
                    Game1.globalFadeToBlack(SetupMainStage);
                    Game1.playSound("smallSelect");
                    return;
                }

                // check for clicked building
                var clickTile = new Vector2(
                    x: (int)((Utility.ModifyCoordinateFromUIScale(x) + Game1.viewport.X) / 64f),
                    y: (int)((Utility.ModifyCoordinateFromUIScale(y) + Game1.viewport.Y) / 64f)
                    );

                var selectedBuilding = Game1.getFarm().getBuildingAt(clickTile);

                // ensure a building was actually clicked
                if (selectedBuilding == null)
                {
                    return;
                }

                // ensure building is valid
                var isBuildingValid = false;
                var customAnimal    = ModEntry.Instance.Api.GetAnimalByInternalSubtypeName(Animal.type);
                if (customAnimal != null)
                {
                    foreach (var building in customAnimal.Buildings)
                    {
                        if (selectedBuilding.buildingType.Value.ToLower() == building.ToLower())
                        {
                            isBuildingValid = true;
                        }
                    }
                }

                // get whether building is valid
                if (isBuildingValid)
                {
                    // ensure building has space for animal
                    if ((selectedBuilding.indoors.Value as AnimalHouse).isFull())
                    {
                        // show 'That Building Is Full' message
                        Game1.showRedMessage(Game1.content.LoadString("Strings\\UI:AnimalQuery_Moving_BuildingFull"));
                        return;
                    }

                    // ensure building isn't it's current home
                    if (selectedBuilding == Animal.home)
                    {
                        // show 'That Is My Home' message
                        Game1.showRedMessage(Game1.content.LoadString("Strings\\UI:AnimalQuery_Moving_AlreadyHome"));
                        return;
                    }

                    // change animals home
                    (Animal.home.indoors.Value as AnimalHouse).animalsThatLiveHere.Remove(Animal.myID);
                    if ((Animal.home.indoors.Value as AnimalHouse).animals.ContainsKey(Animal.myID))
                    {
                        (Animal.home.indoors.Value as AnimalHouse).animals.Remove(Animal.myID);
                        (selectedBuilding.indoors.Value as AnimalHouse).animals.Add(Animal.myID, Animal);
                    }

                    Animal.home = selectedBuilding;
                    Animal.homeLocation.Value = new Vector2(selectedBuilding.tileX, selectedBuilding.tileY);
                    (selectedBuilding.indoors.Value as AnimalHouse).animalsThatLiveHere.Add(Animal.myID);
                    Animal.makeSound();
                    Game1.globalFadeToBlack(FinishRehomingStage);
                }
                else
                {
                    // show '{0}s Can't Live There.'
                    Game1.showRedMessage(Game1.content.LoadString("Strings\\UI:AnimalQuery_Moving_CantLiveThere", Animal.shortDisplayType()));
                }
            }
            else if (CurrentStage == AnimalQueryMenuStage.ConfirmingSell)
            {
                // confirm sell button
                if (ConfirmSellButton.containsPoint(x, y))
                {
                    // give player money
                    Game1.player.Money += Animal.getSellPrice();

                    // remove animal from home
                    (Animal.home.indoors.Value as AnimalHouse).animalsThatLiveHere.Remove(Animal.myID);
                    Animal.health.Value = -1;

                    // unreserve grass if it had reserved some
                    if (Animal.foundGrass != null && FarmAnimal.reservedGrass.Contains(Animal.foundGrass))
                    {
                        FarmAnimal.reservedGrass.Remove(Animal.foundGrass);
                    }

                    // create green smoke particles
                    var numberOfClouds = Animal.frontBackSourceRect.Width / 2;
                    for (int i = 0; i < numberOfClouds; i++)
                    {
                        var greenness   = Game1.random.Next(25, 200); // this is be taken away from the red and blue channels to vary the greenness
                        var multiplayer = (Multiplayer)typeof(Game1).GetField("multiplayer", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
                        multiplayer.broadcastSprites(Game1.currentLocation, new TemporaryAnimatedSprite(
                                                         rowInAnimationTexture: 5,
                                                         position: Animal.Position + new Vector2(Game1.random.Next(-32, Animal.frontBackSourceRect.Width * 3), Game1.random.Next(-32, Animal.frontBackSourceRect.Height * 3)),
                                                         color: new Color(255 - greenness, 255, 255 - greenness),
                                                         animationInterval: (Game1.random.NextDouble() < 0.5) ? 50 : Game1.random.Next(30, 200),
                                                         sourceRectWidth: 64,
                                                         sourceRectHeight: 64,
                                                         delay: (Game1.random.NextDouble() < 0.5) ? Game1.random.Next(0, 600) : 0
                                                         )
                        {
                            scale  = Game1.random.Next(2, 5) * .25f,
                            alpha  = Game1.random.Next(2, 5) * .25f,
                            motion = new Vector2(0, (0 - (float)Game1.random.NextDouble()))
                        });
                    }

                    Game1.playSound("newRecipe");
                    Game1.playSound("money");
                    Game1.exitActiveMenu();
                }

                // deny sell button
                if (DenySellButton.containsPoint(x, y))
                {
                    CurrentStage = AnimalQueryMenuStage.Main;
                    Game1.playSound("smallSelect");
                    this.snapToDefaultClickableComponent();
                }
            }
        }