using ActionGame.GameArtifacts.Models.Items;
using ActionGame.GameArtifacts.Operators;
using ActionGame.GameArtifacts.Operators.Items;
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 GameScreen : Screen
    {
        public override ControlCollection Controls { get; }
        private IScreenController screenController;
        private GameViewer gameViewer;

        private TimeSpan lastMoveInputTime;
        private TimeSpan lastActionInputTime;


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

            Init();
        }


        public override void ActivateScreen()
        {
            gameViewer.GameData = screenController.GameData;
        }


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

        public override void Update(GameTime gameTime)
        {
            if (lastMoveInputTime == default(TimeSpan)) lastMoveInputTime = gameTime.TotalGameTime;
            if (lastActionInputTime == default(TimeSpan)) lastActionInputTime = gameTime.TotalGameTime;

            //check for player death / gameover
            if(gameViewer.GameData.GameState.Player.CharacterStats.Health <= 0)
            {
                screenController.ChangeScreen(ScreenType.GameOver);
            }

            //keyboard
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                //action
                if ((gameTime.TotalGameTime - screenController.LastKeyboardInputTime).TotalSeconds > 0.25)
                {
                    if (this.screenController.GameData.GameState.IsPlotActive())
                    {
                        screenController.LastKeyboardInputTime = gameTime.TotalGameTime;

                        PlotRunner runner = new PlotRunner(this.screenController.GameData);
                        runner.GoToNextItem();
                    }
                    else
                    {
                        screenController.LastKeyboardInputTime = gameTime.TotalGameTime;

                        NonPlayerCharacterActivator activator = new NonPlayerCharacterActivator(this.screenController.GameData);
                        activator.ActivateNonPlayerCharacter(GameResources.Models.NonPlayerCharacterDefinition.Activation.Command);

                        PlotRunner runner = new PlotRunner(this.screenController.GameData);
                        runner.GoToNextItem();
                    }
                }
            }
            else if (this.screenController.GameData.GameState.IsPlotActive() == false) {
                if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                {
                    //escape
                    if ((gameTime.TotalGameTime - screenController.LastKeyboardInputTime).TotalSeconds > 0.25)
                    {
                        screenController.LastKeyboardInputTime = gameTime.TotalGameTime;

                        this.screenController.ChangeScreen(ScreenType.GameMenu);
                    }
                }
                else if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    //inventory screen
                    if ((gameTime.TotalGameTime - screenController.LastKeyboardInputTime).TotalSeconds > 0.25)
                    {
                        screenController.LastKeyboardInputTime = gameTime.TotalGameTime;

                        this.screenController.ChangeScreen(ScreenType.Inventory);
                    }
                }
                else
                {
                    //direction
                    if ((gameTime.TotalGameTime - lastMoveInputTime).TotalSeconds > 0.01)
                    {
                        if (Keyboard.GetState().IsKeyDown(Keys.Up))
                        {
                            ObjectMover mover = new ObjectMover(this.screenController.GameData.GameState.Player);
                            mover.Move(ObjectMover.Direction.Up);

                            NonPlayerCharacterActivator activator = new NonPlayerCharacterActivator(this.screenController.GameData);
                            Boolean result = activator.ActivateNonPlayerCharacter(GameResources.Models.NonPlayerCharacterDefinition.Activation.Touch);

                            lastMoveInputTime = gameTime.TotalGameTime;
                        }
                        else if (Keyboard.GetState().IsKeyDown(Keys.Down))
                        {
                            ObjectMover mover = new ObjectMover(this.screenController.GameData.GameState.Player);
                            mover.Move(ObjectMover.Direction.Down);

                            NonPlayerCharacterActivator activator = new NonPlayerCharacterActivator(this.screenController.GameData);
                            Boolean result = activator.ActivateNonPlayerCharacter(GameResources.Models.NonPlayerCharacterDefinition.Activation.Touch);

                            lastMoveInputTime = gameTime.TotalGameTime;
                        }

                        if (Keyboard.GetState().IsKeyDown(Keys.Left))
                        {
                            ObjectMover mover = new ObjectMover(this.screenController.GameData.GameState.Player);
                            mover.Move(ObjectMover.Direction.Left);

                            NonPlayerCharacterActivator activator = new NonPlayerCharacterActivator(this.screenController.GameData);
                            Boolean result = activator.ActivateNonPlayerCharacter(GameResources.Models.NonPlayerCharacterDefinition.Activation.Touch);

                            lastMoveInputTime = gameTime.TotalGameTime;
                        }
                        else if (Keyboard.GetState().IsKeyDown(Keys.Right))
                        {
                            ObjectMover mover = new ObjectMover(this.screenController.GameData.GameState.Player);
                            mover.Move(ObjectMover.Direction.Right);

                            NonPlayerCharacterActivator activator = new NonPlayerCharacterActivator(this.screenController.GameData);
                            Boolean result = activator.ActivateNonPlayerCharacter(GameResources.Models.NonPlayerCharacterDefinition.Activation.Touch);

                            lastMoveInputTime = gameTime.TotalGameTime;
                        }
                    }

                    //action buttons
                    if ((gameTime.TotalGameTime - lastActionInputTime).TotalSeconds > 0.2)
                    {
                        //weapon 1
                        if (Keyboard.GetState().IsKeyDown(Keys.X))
                        {
                            screenController.LastKeyboardInputTime = gameTime.TotalGameTime;
                            lastActionInputTime = gameTime.TotalGameTime;

                            InventorySlot item = this.screenController.GameData?.GameState?.Player?.Inventory?.Slot1;
                            if ((item != null) && (item.Item != null) && (item.Quantity > 0 || item.Quantity == null))
                            {
                                InventoryItemUser weaponUser = new InventoryItemUser(this.screenController.RawContentManager, this.screenController.GameData, this.screenController.GameData.GameState.Player);
                                weaponUser.UseItem(item.Item);

                                item.Quantity--;
                            }
                        }

                        //weapon 2
                        if (Keyboard.GetState().IsKeyDown(Keys.W))
                        {
                            screenController.LastKeyboardInputTime = gameTime.TotalGameTime;
                            lastActionInputTime = gameTime.TotalGameTime;

                            InventorySlot item = this.screenController.GameData?.GameState?.Player?.Inventory?.Slot2;
                            if ((item != null) && (item.Item != null) && (item.Quantity > 0 || item.Quantity == null))
                            {
                                InventoryItemUser weaponUser = new InventoryItemUser(this.screenController.RawContentManager, this.screenController.GameData, this.screenController.GameData.GameState.Player);
                                weaponUser.UseItem(item.Item);

                                item.Quantity--;
                            }
                        }
                    }
                }
            }

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


        private void Init()
        {
            this.gameViewer = new GameViewer(this.screenController.Resolution, this.screenController.RawContentManager, this.screenController)
            {
                Location = new Rectangle(0, 0, screenController.Resolution.GameWidth, screenController.Resolution.GameHeight),
            };
            Controls.Add(gameViewer);
        }


    }
}
