using System;
using System.Collections.Generic;
using Encompass;
using Kav;
using KavTest.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace KavTest.Renderers
{
    public class SceneRenderer : GeneralRenderer
    {
        private Kav.Renderer Renderer { get; }

        private IEnumerable<(Kav.Model, Matrix)> ModelTransforms
        {
            get
            {
                foreach (var entity in ReadEntitiesAsEnumerable<ModelComponent>())
                {
                    var transformComponent = GetComponent<Transform3DComponent>(entity);
                    var modelComponent = GetComponent<ModelComponent>(entity);
                    yield return (modelComponent.Model, transformComponent.Transform.TransformMatrix);
                }
            }
        }

        private IEnumerable<PointLight> PointLights
        {
            get
            {
                foreach (var entity in ReadEntitiesAsEnumerable<PointLightComponent>())
                {
                    var transformComponent = GetComponent<Transform3DComponent>(entity);
                    var pointLightComponent = GetComponent<PointLightComponent>(entity);

                    yield return new PointLight(
                        transformComponent.Transform.Position,
                        pointLightComponent.Color,
                        pointLightComponent.Intensity
                    );
                }
            }
        }

        private IEnumerable<Kav.DirectionalLight> DirectionalLights
        {
            get
            {
                foreach (var entity in ReadEntitiesAsEnumerable<DirectionalLightComponent>())
                {
                    var transformComponent = GetComponent<Transform3DComponent>(entity);
                    var directionalLightComponent = GetComponent<DirectionalLightComponent>(entity);

                    yield return new Kav.DirectionalLight(
                        transformComponent.Transform.Forward,
                        directionalLightComponent.Color,
                        directionalLightComponent.Intensity
                    );
                }
            }
        }

        public SceneRenderer(GraphicsDevice graphicsDevice)
        {
            Renderer = new Kav.Renderer(graphicsDevice, graphicsDevice.PresentationParameters.BackBufferWidth, graphicsDevice.PresentationParameters.BackBufferHeight);
        }

        public override void Render()
        {
            if (SomeComponent<CameraComponent>())
            {
                var cameraEntity = ReadEntity<CameraComponent>();
                var transformComponent = GetComponent<ArcballTransformComponent>(cameraEntity);
                var cameraComponent = GetComponent<CameraComponent>(cameraEntity);

                var camera = new Kav.Camera(
                    transformComponent.ArcballTransform.TransformMatrix,
                    cameraComponent.Projection
                );

                // if (SomeComponent<DirectionalLightComponent>())
                // {
                //     ref readonly var directionalLightEntity = ref ReadEntity<DirectionalLightComponent>();
                //     ref readonly var directionalLightTransformComponent = ref GetComponent<Transform3DComponent>(directionalLightEntity);
                //     ref readonly var directionalLightComponent = ref GetComponent<DirectionalLightComponent>(directionalLightEntity);

                //     Renderer.DepthRender(
                //         ModelTransforms,
                //         new Kav.DirectionalLight(
                //             directionalLightTransformComponent.Transform.Forward,
                //             directionalLightComponent.Color,
                //             directionalLightComponent.Intensity
                //         )
                //     );
                // }

                Renderer.Render(
                    camera,
                    ModelTransforms,
                    PointLights,
                    DirectionalLights
                );
            }
        }
    }
}
