using MoonWorks;
using MoonWorks.Audio;
using MoonWorks.Graphics;
using MoonWorks.Input;
using System.IO;
using System.Threading.Tasks;

namespace MoonWorksTest
{
    public class TestGame : Game
    {
        ShaderModule passthroughVertexShaderModule;
        ShaderModule raymarchFragmentShaderModule;
        RaymarchUniforms raymarchUniforms;

        Texture woodTexture;
        Texture noiseTexture;
        Sampler sampler;

        Buffer vertexBuffer;

        Color clearColor;

        GraphicsPipeline mainGraphicsPipeline;

        StaticSound music;
        StaticSoundInstance musicInstance;
        StreamingSoundOgg musicStream;

        public TestGame(WindowCreateInfo windowCreateInfo, FrameLimiterSettings limiterSettings, int targetTimestep = 60, bool debugMode = false) : base(windowCreateInfo, limiterSettings, targetTimestep, debugMode)
        {
            var windowWidth = windowCreateInfo.WindowWidth;
            var windowHeight = windowCreateInfo.WindowHeight;

            //passthroughVertexShaderModule = new ShaderModule(GraphicsDevice, Path.Combine("Content", "passthrough_vert.spv"));
            //raymarchFragmentShaderModule = new ShaderModule(GraphicsDevice, Path.Combine("Content", "hexagon_grid.spv"));

            raymarchUniforms.time = 0;
            raymarchUniforms.padding = 0;
            raymarchUniforms.resolutionX = windowWidth;
            raymarchUniforms.resolutionY = windowHeight;

            var uploadCommandBuffer = GraphicsDevice.AcquireCommandBuffer();

            woodTexture = Texture.FromImageFile(GraphicsDevice, uploadCommandBuffer, "Content/woodgrain.png");
            noiseTexture = Texture.FromImageFile(GraphicsDevice, uploadCommandBuffer, "Content/noise.png");

            sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.LinearWrap);

            /* Load Vertex Data */

            var vertices = new PositionTexture[3];
            vertices[0].Position.X = -1;
            vertices[0].Position.Y = -1;
            vertices[0].Position.Z = 0;
            vertices[0].Texture.X = 0;
            vertices[0].Texture.Y = 1;

            vertices[1].Position.X = 3;
            vertices[1].Position.Y = -1;
            vertices[1].Position.Z = 0;
            vertices[1].Texture.X = 1;
            vertices[1].Texture.Y = 1;

            vertices[2].Position.X = -1;
            vertices[2].Position.Y = 3;
            vertices[2].Position.Z = 0;
            vertices[2].Texture.X = 0;
            vertices[2].Texture.Y = 0;

            vertexBuffer = Buffer.Create<PositionTexture>(GraphicsDevice, BufferUsageFlags.Vertex, 3);
            uploadCommandBuffer.SetBufferData(vertexBuffer, vertices);

            GraphicsDevice.Submit(uploadCommandBuffer);

            /* Pipeline */

			/*
            mainGraphicsPipeline = new GraphicsPipeline(
                GraphicsDevice,
                new GraphicsPipelineCreateInfo
                {
                    AttachmentInfo = new GraphicsPipelineAttachmentInfo(
                        new ColorAttachmentDescription(
                            MainWindow.SwapchainFormat,
                            ColorAttachmentBlendState.None
                        )
                    ),
                    DepthStencilState = DepthStencilState.Disable,
                    VertexShaderInfo = GraphicsShaderInfo.Create(passthroughVertexShaderModule, "main", 0),
					VertexInputState = VertexInputState.CreateSingleBinding<PositionTexture>(),
                    PrimitiveType = PrimitiveType.TriangleList,
                    FragmentShaderInfo = GraphicsShaderInfo.Create<RaymarchUniforms>(raymarchFragmentShaderModule, "main", 2),
                    RasterizerState = RasterizerState.CW_CullBack,
                    MultisampleState = MultisampleState.None
                }
            );
			*/

            // music = StaticSound.LoadOgg(AudioDevice, Path.Combine("Content", "title_screen.ogg"));
            // musicInstance = music.CreateInstance();
            // musicInstance.Play();

            musicStream = StreamingSoundOgg.Load(AudioDevice, Path.Combine("Content", "housecleaning_herbal_mix.ogg"));
            musicStream.QueueSyncPlay();

			var musicQoaStream = StreamingSoundQoa.Load(AudioDevice, Path.Combine("Content", "housecleaning_herbal_mix.qoa"));
			musicQoaStream.QueueSyncPlay();

			AudioDevice.SyncPlay();
		}

        protected override void Update(System.TimeSpan dt)
        {
            raymarchUniforms.time += (float)dt.TotalSeconds;
        }

        protected override void Draw(double alpha)
        {
            var commandBuffer = GraphicsDevice.AcquireCommandBuffer();

            var swapchainTexture = commandBuffer.AcquireSwapchainTexture(MainWindow);

            if (swapchainTexture != null)
            {
                commandBuffer.BeginRenderPass(
                    new ColorAttachmentInfo(swapchainTexture, clearColor)
                );

				/*
                commandBuffer.BindGraphicsPipeline(mainGraphicsPipeline);

                commandBuffer.BindVertexBuffers(vertexBuffer);
                commandBuffer.BindFragmentSamplers(
                    new TextureSamplerBinding(woodTexture, sampler),
                    new TextureSamplerBinding(noiseTexture, sampler)
                );

                var fragmentParamOffset = commandBuffer.PushFragmentShaderUniforms(raymarchUniforms);
                commandBuffer.DrawPrimitives(0, 1, 0, fragmentParamOffset);
				*/

                commandBuffer.EndRenderPass();

            }

            GraphicsDevice.Submit(commandBuffer);
        }

        protected override void Destroy()
        {

        }
    }
}
