﻿using App.SpaceInvaders.Classes;
using App.SpaceInvaders.Game_Contexts;
using CodeCafeEngine;
using DeveloperConsole = CodeCafeEngine.DeveloperConsole;

namespace App.SpaceInvaders.Components
{
    class ControllerComponent : GameComponent
    {
        private AudioController _audioController;
        private float _invasionSpeed = 1f;

        public ControllerComponent()
        {
            Name = "Controller";
        }
        
        /// <summary>
        /// Add points to the player's score.
        /// </summary>
        /// <param name="score"></param>
        public void AddScore(int score)
        {
            GameSettings.Score += score;
        }

        /// <summary>
        /// Occurs when a game over condition has been triggered.
        /// </summary>
        public void GameOver()
        {
            DeveloperConsole.Write(ReportLevel.Feedback, "You have died!");

            // Go to game over screen
            GameContext.SwitchContext(new GameOverContext().Context);
        }
        
        /// <summary>
        /// Subtract a life from the player.
        /// </summary>
        public void SubtractLife()
        {
            if (GameSettings.Lives == 0) return;
            GameSettings.Lives--;

            // Play audio effect
            _audioController.PlayPlayerDie();
            GameContext.Find<PlayerController>()[0].Kill();
        }

        /// <summary>
        /// Occurs when there are no more space invaders.
        /// </summary>
        private void PlayerWon()
        {
            // Write feedback to the developer console in case something goes wrong
            DeveloperConsole.Write(ReportLevel.Feedback, "You beat the invaders!! Uh-oh, there are more incoming...");

            // Increase invasion speed
            _invasionSpeed += GameSettings.InvasionSpeedIncrement;
            
            // Destroy the current invader controller
            GameComponent go = GameContext.FindComponent("Invader Parent Controller")[0];
            go.DestroyGameObject();

            // Start a new, faster wave
            GameObject invaderParent = new GameObject();
            InvaderParentController controller = new InvaderParentController { InvasionSpeed = _invasionSpeed };
            invaderParent.AddComponent(controller);
            GameContext.AddGameObject(invaderParent);

            // Restore bunkers
            BunkerComponent[] bunkers = GameContext.Find<BunkerComponent>();
            foreach (BunkerComponent bunker in bunkers)
                bunker.RestoreBunker();
        }

        #region Overrides of GameComponent

        /// <summary>
        /// Occurs when the component is first started.
        /// </summary>
        public override void OnStart()
        {
            // Get the audio controller
            _audioController = GameContext.Find<AudioController>()[0];
        }

        /// <summary>
        /// Occurs when any game object or component has been destroyed.
        /// </summary>
        public override void OnAnyDestroyed(string name)
        {
            if (name == "Space Invader")
            {
                // A space invader was destroyed...
                // Was it the last one?
                int remaining = GameContext.Count<InvaderController>();
                if (remaining == 0)
                {
                    // Yes it was! The player darn-diddly-done went and did it!
                    PlayerWon();
                }
                else
                {
                    DeveloperConsole.Write(ReportLevel.Feedback, "A space invader died, but there are still " + remaining + " to go!");
                }
            }
        }

        #endregion
    }
}
