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 TitleScreenState : GameState
    {
        protected EntityList<Entity> EntityList { get; set; }

        public Font Font { get; private set; }

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

            EntityList = new EntityList<Entity>();
        }

        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("title_screen.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);
        }

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

        private void WindowOnKeyPressed(object sender, KeyEventArgs args)
        {
            ToSplashScreen();
        }

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

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

        private void ToSplashScreen()
        {
            Game.SwitchToGameState(new SplashScreenState(Game));
        }
    }
}
