using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;

[ExecuteInEditMode]
public class VolumetricFogDensity : MonoBehaviour
{
    static readonly int sp_worldToLocal = Shader.PropertyToID("_density_worldToLocal");
    static readonly int sp_density_attenuates = Shader.PropertyToID("_density_attenuates");
    static readonly int sp_density_distance_attenuates = Shader.PropertyToID("_density_distance_attenuates");
    static readonly int sp_uv_scale = Shader.PropertyToID("_density_uv_scale");
    static readonly int sp_move_direction = Shader.PropertyToID("_density_move_direction");
    static readonly int sp_color = Shader.PropertyToID("_density_color");
    

    [Range(0, 10)]
    public float m_densityAttenuates = 1;
    [Range(0, 10)]
    public float m_distanceAttenuates = 1;
    public Vector3 m_uvScale = new Vector3(1, 1, 1);
    public Vector3 m_moveDirection = new Vector3(0.1f, 0, 0.1f);
    public Color m_color = new Color(1, 1, 1, 1);
    
    Material m_material = null;
    
    void Start()
    {
      
        Add();
    }

    private void OnDestroy()
    {
        Remove();
    }

    private void OnEnable()
    {
        Add();
    }

    private void OnDisable()
    {
        Remove();
    }

    public Material GetMaterial(Material material)
    {
        if(material == null)
        {
            m_material = null;
            return m_material;
        }

        if(m_material == null)
        {
            m_material = new Material(material);
        }
        return m_material;
    }
    public void SetMaterailProperty(Material material)
    {
        material.SetFloat(sp_density_attenuates, m_densityAttenuates);
        material.SetFloat(sp_density_distance_attenuates, m_distanceAttenuates);
        material.SetVector(sp_uv_scale, m_uvScale);
        material.SetVector(sp_move_direction, m_moveDirection);
        material.SetColor(sp_color, m_color);

        Matrix4x4 worldToLocal = transform.worldToLocalMatrix;

        // [-0.5, 0.5]
        //Vector4 pos = new Vector4(transform.position.x, transform.position.y, transform.position.z, 1.0f);
        //Vector4 pos2 = new Vector4(transform.position.x + (transform.right * transform.localScale.x / 2.0f).x, 
        //    transform.position.y + (transform.up * transform.localScale.y / 2.0f).y, 
        //    transform.position.z + (transform.forward * transform.localScale.z / 2.0f).z, 1.0f);
        //Vector4 pos3 = new Vector4(transform.position.x - (transform.right * transform.localScale.x / 2.0f).x,
        //  transform.position.y - (transform.up * transform.localScale.y / 2.0f).y,
        //  transform.position.z - (transform.forward * transform.localScale.z / 2.0f).z, 1.0f);

        //Vector4 pos4 = worldToLocal * pos;
        //Vector4 pos5 = worldToLocal * pos2;
        //Vector4 pos6 = worldToLocal * pos3;

        material.SetMatrix(sp_worldToLocal, worldToLocal);
   
    }

    void Add()
    {
        //var forwardRender = UniversalRenderPipeline.asset.scriptableRenderer as ForwardRenderer;

        if (X2Engine.EnvironmentSystem.instance == null ||
            X2Engine.EnvironmentSystem.instance.profile == null)
            return;

        X2Engine.EnvironmentSystem.instance.profile.volumetricFogSetting.AddDensity(this);
    }

    private void Remove()
    {
        if (X2Engine.EnvironmentSystem.instance == null ||
           X2Engine.EnvironmentSystem.instance.profile == null)
            return;

        X2Engine.EnvironmentSystem.instance.profile.volumetricFogSetting.RemoveDensity(this);
    }
    //Update is called once per frame
    void Update()
    {
        Add();
    }
}
