using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Camera))]
public class DepthPostprocessor : MonoBehaviour
{

    public float timeBetweenRequests = 0.2f;
    public string topicname = "rt/depth_image/image_raw/compressed";
    public string frame_id = null;
    //private ImagePublisher ImagePub;
    //private Image_ myimage = new Image_(); // format 32 bits floating point for depth camera

    //material that's applied when doing postprocessing
    [SerializeField]
    private Material mater;

    private RenderTexture m_RenderTexture;
    private ComputeBuffer buffer;
    private float[] output;
    
    private float timer = 0.0f;


    private void Start()
    {
        Camera camera = GetComponent<Camera>();
        camera.depthTextureMode = camera.depthTextureMode | DepthTextureMode.Depth;

        m_RenderTexture = camera.targetTexture;
        m_RenderTexture.depth = 32;
        m_RenderTexture.enableRandomWrite = true; // ik wijzig texture niet dus niet nodig?

        buffer = new ComputeBuffer(m_RenderTexture.width * m_RenderTexture.height, sizeof(float));
        output = new float[m_RenderTexture.width * m_RenderTexture.height];
        output[0] = 0.2f;
        output[30000] = 0.212f;
        buffer.SetData(output); // nodig?
        Graphics.SetRandomWriteTarget(1, buffer, true); // 1?

        mater.SetInt("_width", m_RenderTexture.width);
        mater.SetInt("_height", m_RenderTexture.height);
        mater.SetBuffer("_myBuffer", buffer);

        //InitPub((uint) m_RenderTexture.height, (uint) m_RenderTexture.width);
    }

    private void InitPub(uint height, uint width)
    {
        //ImagePub = new ImagePublisher(topicname);

        //myimage.encoding_ = "32FC1";
        //myimage.height_ = height;
        //myimage.width_ = width;
        //myimage.is_bigendian_ = 0;
        //myimage.step_ = myimage.width_ * 4; //4 want float is 4 bytes
    }

    private byte[] GetBytes(float[] input)
    {
        byte[] messageinput = new byte[input.Length * 4];
        for (int i = 0; i < input.Length; i++)
        {
            byte[] newbytes = System.BitConverter.GetBytes(input[i]);
            for (int j = 0; j < newbytes.Length; j++)
            {
                messageinput[i * 4 + j] = newbytes[j];
            }
        }
        return messageinput;
    }
    //method which is automatically called by unity after the camera is done rendering
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        Graphics.Blit(source, destination, mater);

        timer += Time.deltaTime;
        if (timer > timeBetweenRequests)
        {
            //draws the pixels from the source texture to the destination texture

            buffer.GetData(output);
            //myimage.data_ = GetBytes(output);
            //myimage.header_.stamp_ = ros_helper.RosHelper.GetUnixTimestamp();
            //myimage.header_.frame_id_ = frame_id;
            //ImagePub.Publish(myimage);

            timer = timer - timeBetweenRequests;
        }
    }
    private void OnDestroy()
    {
        buffer.Dispose();
    }
}