//
//  KRLight.h
//  Kraken Engine
//
//  Copyright 2026 Kearwood Gilbert. All rights reserved.
//  
//  Redistribution and use in source and binary forms, with or without modification, are
//  permitted provided that the following conditions are met:
//  
//  1. Redistributions of source code must retain the above copyright notice, this list of
//  conditions and the following disclaimer.
//  
//  2. Redistributions in binary form must reproduce the above copyright notice, this list
//  of conditions and the following disclaimer in the documentation and/or other materials
//  provided with the distribution.
//  
//  THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
//  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
//  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//  
//  The views and conclusions contained in the software and documentation are those of the
//  authors and should not be interpreted as representing official policies, either expressed
//  or implied, of Kearwood Gilbert.
//
#pragma once

#include "resources/KRResource.h"
#include "KRNode.h"
#include "resources/texture/KRTexture.h"
#include "resources/texture/KRTextureBinding.h"

static const float KRLIGHT_MIN_INFLUENCE = 0.15f; // 0.05f
static const float KRLIGHT_PARTICLE_RANGE = 600.0f;

// KRENGINE_MAX_SHADOW_BUFFERS must be at least 6 to allow omni-directional lights to render cube maps

#define KRENGINE_MAX_SHADOW_BUFFERS 6
#define KRENGINE_SHADOW_MAP_WIDTH 1024
#define KRENGINE_SHADOW_MAP_HEIGHT 1024

class KRLight : public KRNode
{
public:
  static void InitNodeInfo(KrNodeInfo* nodeInfo);

  virtual ~KRLight();
  virtual std::string getElementName() override = 0;
  virtual tinyxml2::XMLElement* saveXML(tinyxml2::XMLNode* parent) override;
  virtual void loadXML(tinyxml2::XMLElement* e) override;

  void setIntensity(float intensity);
  float getIntensity() const;
  void setDecayStart(float decayStart);
  float getDecayStart() const;
  const hydra::Vector3& getColor();
  void setColor(const hydra::Vector3& color);

  void setFlareTexture(std::string flare_texture);
  void setFlareSize(float flare_size);
  void setFlareOcclusionSize(float occlusion_size);
  void deleteBuffers();

  virtual void getResourceBindings(std::list<KRResourceBinding*>& bindings) override;
  virtual void render(RenderInfo& ri) override;

  int getShadowBufferCount();
  int* getShadowTextures();
  KRViewport* getShadowViewports();


protected:
  KRLight(KRScene& scene, std::string name);

  bool getShaderValue(const KRCamera* camera, ShaderValue value, float* output) const override;
  bool getShaderValue(const KRCamera* camera, ShaderValue value, hydra::Vector2* output) const override;
  bool getShaderValue(const KRCamera* camera, ShaderValue value, hydra::Vector3* output) const override;
  bool getImageBinding(const std::string& name, const KRTextureBinding** binding, KRSampler** sample) const final;

  // Properties
  KRNODE_PROPERTY(float, m_decayStart, 0.f, "decay_start");
  KRNODE_PROPERTY(hydra::Vector3, m_color, hydra::Vector3({1.f, 1.f, 1.f}), "decay_start");
  KRNODE_PROPERTY(float, m_intensity, 1.f, "intensity");
  KRNODE_PROPERTY(float, m_flareSize, 0.f, "flare_size");
  KRNODE_PROPERTY(float, m_flareOcclusionSize, 0.05f, "flare_occlusion_size");
  KRNODE_PROPERTY(bool, m_casts_shadow, true, "casts_shadow");
  KRNODE_PROPERTY(bool, m_light_shafts, true, "light_shafts");
  KRNODE_PROPERTY(float, m_dust_particle_density, 0.1f, "dust_particle_density");
  KRNODE_PROPERTY(float, m_dust_particle_size, 1.f, "dust_particle_size");
  KRNODE_PROPERTY(float, m_dust_particle_intensity, 1.f, "dust_particle_intensity");
  KRNODE_PROPERTY(KRTextureBinding, m_flareTexture, KRTexture::TEXTURE_USAGE_LIGHT_FLARE, "flare_texture");

  int m_occlusionQuery; // Occlusion query for attenuating occluded flares


  // Shadow Maps
  int m_cShadowBuffers;
  int shadowFramebuffer[KRENGINE_MAX_SHADOW_BUFFERS], shadowDepthTexture[KRENGINE_MAX_SHADOW_BUFFERS];
  bool shadowValid[KRENGINE_MAX_SHADOW_BUFFERS];
  KRViewport m_shadowViewports[KRENGINE_MAX_SHADOW_BUFFERS];

  void allocateShadowBuffers(int cBuffers);
  void invalidateShadowBuffers();

  virtual int configureShadowBufferViewports(const KRViewport& viewport);
  void renderShadowBuffers(RenderInfo& ri);

private:
  hydra::Matrix4 getParticleModelMatrix(const KRViewport& viewport) const;
  int getParticleCount() const;
  int getSliceCount(const KRCamera* camera) const;
};
