using System;
using System.Collections.Generic;
using SFML.Graphics;
using SFML.Window;
using SlimDX.XInput;
using YourBeatingHeart.Main.Effects;
using YourBeatingHeart.Main.Entities;
using YourBeatingHeart.Main.States.Play;
using YourBeatingHeart.Main.States.Play.Collision;
using YourBeatingHeart.Main.States.Play.Entities;
using YourBeatingHeart.Main.Managers;
using System.Diagnostics;

namespace YourBeatingHeart.Main.States
{
    public class SplashScreenState : GameState
    {
        protected EntityList<Entity> EntityList { get; set; }

        public Font Font { get; private set; }

        private int amountOfJoystickPlayers;

        private int maxAmountOfPlayers { get { return Math.Min(4, amountOfJoystickPlayers + 2); } }

        private Sprite chooseAmount;
        private Sprite players2;
        private Sprite players3;
        private Sprite players4;
        private Sprite loadingSprite;
        private bool loading;

        public SplashScreenState(Game game) : base(game)
        {
            Font = Game.LoadFont("cambria.ttf");

            EntityList = new EntityList<Entity>();

            amountOfJoystickPlayers = 0;

            chooseAmount = new Sprite(Game.LoadTexture("set_number_of_players.png"));
            players2 = new Sprite(Game.LoadTexture("2_players.png"));
            players3 = new Sprite(Game.LoadTexture("3_players.png"));
            players4 = new Sprite(Game.LoadTexture("4_players.png"));
            loadingSprite = new Sprite(Game.LoadTexture("loading.png"));
            loadingSprite.Origin = new Vector2f(loadingSprite.Texture.Size.X / 2, loadingSprite.Texture.Size.Y);

            var offset = 15;

            chooseAmount.Position = new Vector2f(500, Constants.TARGET_HEIGHT - chooseAmount.Texture.Size.Y - 20);
            players2.Position = chooseAmount.Position + new Vector2f(chooseAmount.Texture.Size.X + offset + 5, 0);
            players3.Position = players2.Position + new Vector2f(players2.Texture.Size.X + offset, 0);
            players4.Position = players3.Position + new Vector2f(players3.Texture.Size.X + offset, 0);

            loadingSprite.Position = new Vector2f(Constants.TARGET_WIDTH / 2, Constants.TARGET_HEIGHT);

            loading = false;
        }

        public void AddEntity(Entity entity)
        {
            EntityList.Add(entity);
            entity.Added();
        }

        public void RemoveEntity(Entity entity)
        {
            EntityList.Remove(entity);
            entity.Removed();
        }

        public override void SetUp()
        {
            var backgroundTexture = Game.LoadTexture("game_tutorial.jpg");
            AddEntity(new DrawableEntity(new Sprite(backgroundTexture)));

            Window.JoystickButtonPressed += WindowOnJoystickButtonPressed;
            Window.KeyPressed += WindowOnKeyPressed;
            Window.MouseButtonPressed += WindowOnMouseButtonPressed;
        }

        public override void TearDown()
        {
            Window.JoystickButtonPressed -= WindowOnJoystickButtonPressed;
            Window.KeyPressed -= WindowOnKeyPressed;
            Window.MouseButtonPressed -= WindowOnMouseButtonPressed;
        }

        public override void Update(float elapsedTime)
        {
            EntityList.Update(elapsedTime);

            amountOfJoystickPlayers = 0;
            for (uint i = 0; i < 4; i++)
            {
                if (new Controller(ControllerVibration.IdToUserIndex(i)).IsConnected)
                {
                    amountOfJoystickPlayers++;
                }
                else
                {
                    break;
                }
            }
        }

        public override void Draw()
        {
            Window.Clear(new Color(210, 210, 210));
            EntityList.Draw(Window);

            if (loading)
            {
                Window.Draw(loadingSprite);
            }
            else
            {
                Window.Draw(chooseAmount);
                Window.Draw(players2);

                if (maxAmountOfPlayers >= 3)
                {
                    Window.Draw(players3);
                }

                if (maxAmountOfPlayers >= 4)
                {
                    Window.Draw(players4);
                }
            }
        }

        private void WindowOnKeyPressed(object sender, KeyEventArgs args)
        {
            if (args.Code == Keyboard.Key.Escape)
            {
                Window.Close();
                return;
            }

            int amountOfPlayers = 0;
            switch (args.Code)
            {
                case Keyboard.Key.Num2:
                case Keyboard.Key.Numpad2:
                    amountOfPlayers = 2;
                    break;

                case Keyboard.Key.Num3:
                case Keyboard.Key.Numpad3:
                    amountOfPlayers = 3;
                    break;

                case Keyboard.Key.Num4:
                case Keyboard.Key.Numpad4:
                    amountOfPlayers = 4;
                    break;
            }

            if (amountOfPlayers == 0)
                return;

            if (amountOfPlayers > maxAmountOfPlayers)
                return;

            StartGame(amountOfPlayers);
        }

        private void WindowOnJoystickButtonPressed(object sender, JoystickButtonEventArgs args)
        {
            //StartGame();
        }

        private void WindowOnMouseButtonPressed(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            //StartGame();
        }

        private void StartGame(int numberOfPlayers)
        {
            if (loading)
                return;

            Game.SwitchToGameState(new PlayState(Game, numberOfPlayers));
            loading = true;
        }
    }
}
