using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SuperBallBrawlers
{
    using Protoinject;

    using Protogame;

    public class SuperBallBrawlersGame : CoreGame<SuperBallBrawlersWorld>
    {
        private readonly IRawLaunchArguments _arguments;

        public SuperBallBrawlersGame(IKernel kernel, IRawLaunchArguments arguments)
            : base(kernel)
        {
            _arguments = arguments;
        }

        protected override void ConfigureRenderPipeline(IRenderPipeline pipeline, IKernel kernel)
        {
            var debugPass = kernel.Get<IDebugRenderPass>();
            var renderPass = kernel.Get<I3DDeferredRenderPass>();
            renderPass.DebugGBuffer = false;
            renderPass.ClearDepthBuffer = true;
            renderPass.GBufferBlendState = BlendState.AlphaBlend;

            var skyRenderPass = kernel.Get<I2DBatchedRenderPass>();
            skyRenderPass.Name = "Sky";

            var uiRenderPass = kernel.Get<I2DBatchedRenderPass>();
            uiRenderPass.Name = "UI";

            //debugPass.EnabledLayers.Add(new PhysicsDebugLayer());

            pipeline.AddFixedRenderPass(skyRenderPass);
            pipeline.AddFixedRenderPass(renderPass);
            pipeline.AddFixedRenderPass(debugPass);
            pipeline.AddFixedRenderPass(uiRenderPass);
        }

        protected override void PrepareGraphicsDeviceManager(GraphicsDeviceManager graphicsDeviceManager)
        {
            graphicsDeviceManager.PreferredBackBufferWidth = 1280;
            graphicsDeviceManager.PreferredBackBufferHeight = 720;

            base.PrepareGraphicsDeviceManager(graphicsDeviceManager);
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            if (!_arguments.Arguments.Contains("--windowed"))
            {
                GraphicsDeviceManager.ToggleFullScreen();
            }
        }

        protected override void PrepareGameWindow(IGameWindow window)
        {
#if PLATFORM_WINDOWS
            Window.PlatformWindow.Position = new Point(
                GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / 2 - 1280 / 2,
                GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / 2 - 720 / 2);
#endif

            base.PrepareGameWindow(window);
        }
    }
}
