using ActionGame.RawResources.Manager;
using ActionGame.UserInterface.Control;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;

namespace ActionGame.UserInterface.Screen
{
    public class GameOverScreen : Screen
    {
        public override ControlCollection Controls { get; }
        private IScreenController screenController;
        private Label saveResult;

        private TimeSpan lastMovementInput;
        private TimeSpan lastActionInput;


        public GameOverScreen(IScreenController screenController)
        {
            this.Controls = new ControlCollection();
            this.screenController = screenController;
            this.saveResult = null;

            Init();
        }


        public override void ActivateScreen()
        {
            Song song = this.screenController.RawContentManager.SongRepository.GetContent(@"music\gameover");

            if (MediaPlayer.State == MediaState.Playing)
            {
                MediaPlayer.Stop();
            }

            this.screenController.BackgroundMusic = song;
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(song);

            lastMovementInput = screenController.LastKeyboardInputTime;
            lastActionInput = screenController.LastKeyboardInputTime;
        }


        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            foreach (IControl control in this.Controls)
            {
                control.Draw(spriteBatch, gameTime);
            }

            if (saveResult != null) saveResult.Draw(spriteBatch, gameTime);
        }


        public override void Update(GameTime gameTime)
        {
            //keyboard
            if ((gameTime.TotalGameTime - lastMovementInput).TotalSeconds >= 0.12)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                {
                    lastMovementInput = gameTime.TotalGameTime;
                    this.Controls.SelectPrev();
                }
                else if (Keyboard.GetState().IsKeyDown(Keys.Down))
                {
                    lastMovementInput = gameTime.TotalGameTime;
                    this.Controls.SelectNext();
                }
            }

            if ((gameTime.TotalGameTime - lastActionInput).TotalSeconds >= 0.12)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    if (this.Controls.IndexOfSelected() != -1)
                    {
                        this.screenController.LastKeyboardInputTime = gameTime.TotalGameTime;
                        lastActionInput = gameTime.TotalGameTime;
                        this.Controls[this.Controls.IndexOfSelected()].Press(gameTime);
                    }
                }
            }


            //controls
            foreach (IControl control in this.Controls)
            {
                control.Update(gameTime);
            }
        }


        private void Init()
        {
            IRawContentManager rawContentManager = screenController.RawContentManager;

            int buttonWidth = 220;
            int buttonHeight = 32;
            int buttonStart = 8;
            int buttonSpace = 48;
            int screenWidth = screenController.Resolution.GameWidth;
            int screenHeight = screenController.Resolution.GameHeight;
            int screenHeightHalf = screenHeight / 2;

            this.Controls.Add(new Panel()
            {
                Location = new Rectangle(0, 0, screenWidth, screenHeight),
                Texture = rawContentManager.TextureRepository.GetContent(@"textures\ui\gameover-bg")
            });

            this.Controls.Add(new Label()
            {
                Text = "Game Over",
                Location = new Rectangle((screenWidth - 370) / 2, 50, 460, 100),
                Font = rawContentManager.FontRepository.GetContent(@"fonts\Title")
            });

            this.Controls.Add(new Button()
            {
                Text = "Main Menu",
                Location = new Rectangle((screenWidth - buttonWidth) / 2, screenHeightHalf - buttonStart - (buttonSpace * 0), buttonWidth, buttonHeight),
                TextureNormal = rawContentManager.TextureRepository.GetContent(@"textures\ui\button"),
                TextureSelected = rawContentManager.TextureRepository.GetContent(@"textures\ui\button-select"),
                TexturePressed = rawContentManager.TextureRepository.GetContent(@"textures\ui\button-press"),
                Font = rawContentManager.FontRepository.GetContent(@"fonts\Normal"),
                PressSound = rawContentManager.SoundEffectRepository.GetContent(@"soundeffects\ui -button"),
                IsSelected = true
            });
            ((Button)this.Controls[this.Controls.Count - 1]).OnClick += MainMenu;

            this.Controls.Add(new Button()
            {
                Text = "Exit",
                Location = new Rectangle((screenWidth - buttonWidth) / 2, screenHeightHalf - buttonStart + (buttonSpace * 1), buttonWidth, buttonHeight),
                TextureNormal = rawContentManager.TextureRepository.GetContent(@"textures\ui\button"),
                TextureSelected = rawContentManager.TextureRepository.GetContent(@"textures\ui\button-select"),
                TexturePressed = rawContentManager.TextureRepository.GetContent(@"textures\ui\button-press"),
                PressSound = rawContentManager.SoundEffectRepository.GetContent(@"soundeffects\ui-button"),
                Font = rawContentManager.FontRepository.GetContent(@"fonts\Normal")
            });
            ((Button)this.Controls[this.Controls.Count - 1]).OnClick += CloseGame;
        }


        private void CloseGame(object sender, GameEventArgs e)
        {
            this.screenController.Exit();
        }


        private void MainMenu(object sender, GameEventArgs e)
        {
            this.screenController.GameData = null;
            this.screenController.ChangeScreen(ScreenType.MainMenu);
        }

    }
}
