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

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

        private TimeSpan lastMovementInput;
        private TimeSpan lastActionInput;


        public MainMenuScreen(IScreenController screenController)
        {
            this.Controls = new ControlCollection();
            this.screenController = screenController;

            Init();
        }


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


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

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

                        this.Controls[this.Controls.IndexOfSelected()].Press(gameTime);
                    }
                }
            }


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


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

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

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

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


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

            int buttonWidth = 220;
            int buttonHeight = 32;
            int buttonStart = 32;
            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\title")
            });

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

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

            this.Controls.Add(new Button()
            {
                Text = "New Game",
                Location = new Rectangle((screenWidth - buttonWidth) / 2, screenHeightHalf + buttonStart, 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 += NewGame;

            this.Controls.Add(new Button()
            {
                Text = "Load Game",
                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 += LoadGame;

            this.Controls.Add(new Button()
            {
                Text = "Close",
                Location = new Rectangle((screenWidth - buttonWidth) / 2, screenHeightHalf + buttonStart + (buttonSpace * 2), 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;

            this.Controls.Add(new Label()
            {
                Font = rawContentManager.FontRepository.GetContent(2),
                Text = "Controls\nWeapons = X, W\nInventory = Enter\nAccept = Space\nMenu = Escape\nToggle Fullscreen = ~",
                Location = new Rectangle(10, 300, 200, 200)
            });
        }


        private void NewGame(object sender, GameEventArgs e)
        {
            GameDataFactory factory = new GameDataFactory(screenController.GameContentManager, screenController.RawContentManager);
            this.screenController.GameData = factory.CreateNewGame();

            this.screenController.ChangeScreen(ScreenType.Game);
        }


        private void LoadGame(object sender, GameEventArgs e)
        {
            GameDataFactory factory = new GameDataFactory(screenController.GameContentManager, screenController.RawContentManager);
            this.screenController.GameData = factory.CreateNewGame(false);

            GameStateLoader loader = new GameStateLoader(this.screenController.GameData);
            Boolean result = loader.Load();

            if(result == false)
            {
                this.screenController.GameData = factory.CreateNewGame();
            }

            this.screenController.ChangeScreen(ScreenType.Game);
        }


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


    }
}
