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

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

        private TimeSpan lastMovementInput;
        private TimeSpan lastActionInput;


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

            Init();
        }


        public override void ActivateScreen()
        {
            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))
                {
                    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)
                    {
                        this.screenController.LastKeyboardInputTime = gameTime.TotalGameTime;
                        lastActionInput = gameTime.TotalGameTime;

                        this.Controls[this.Controls.IndexOfSelected()].Press(gameTime);
                    }
                }
                else if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                {
                    screenController.LastKeyboardInputTime = gameTime.TotalGameTime;
                    lastActionInput = gameTime.TotalGameTime;

                    ResumeGame(null, null);
                }
            }


            //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 = 92;
            int buttonSpace = 48;
            int screenWidth = screenController.Resolution.GameWidth;
            int screenHeight = screenController.Resolution.GameHeight;
            

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

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

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

            this.Controls.Add(new Button()
            {
                Text = "Main Menu",
                Location = new Rectangle((screenWidth - buttonWidth) / 2, 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 += MainMenu;

            this.Controls.Add(new Button()
            {
                Text = "Exit",
                Location = new Rectangle((screenWidth - buttonWidth) / 2, buttonStart + (buttonSpace * 3), 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 SaveGame(object sender, GameEventArgs e)
        {
            GameStateSaver saver = new GameStateSaver(this.screenController.GameData.GameState);
            Boolean result = saver.Save();

            Label label = new Label();
            label.Font = screenController.RawContentManager.FontRepository.GetContent(@"fonts\Small");
            if (result) label.Text = "Saved Successfully";
            else label.Text = "Save Failed";

            label.Location = new Rectangle(404, 360, 300, 24);

            saveResult = label;
        }


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


        private void ResumeGame(object sender, GameEventArgs e)
        {
            this.screenController.ChangeScreen(ScreenType.Game);
        }


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


    }
}
