﻿using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;

/*
 * Demo program of okaypeg to sync the image of a camera.
 */

public class okaypeg_demo: UdonSharpBehaviour
{

/* Reference to the compressor */
public okaypeg_main _compressor;

/* 
 * Two store items (main holds the large chunks of data, delta holds a smaller
 * set of blocks when possible to try and save networking usage
 */
public okaypeg_store _store_main, _store_delta;

/*
 * RT src is the texture we're reading from, so the camera's view in this demo.
 * RT dst is the destination texture we read the compressed image into.
 */
public RenderTexture _rt_src, _rt_dst;

/*
 * Signal that there is new data in _rt_src
 */
bool _picture_taken = false;

/* 
 * Take picture button
 */
void OnPickupUseDown()
{
   this.GetComponent<Camera>().Render();

   /* 
    * If we don't own it we use the claim function to take the data and reset
    * it 
    */
   if( Networking.GetOwner( _store_main.gameObject ) != Networking.LocalPlayer )
      _compressor._opg_claim_and_clear( _store_main, _store_delta );

   /* Signal to the update loop that we should compress a new image */
   _picture_taken = true;
}

float _timer = 0.0f;
const float k_timer_interval = 0.1f;
void Update()
{
   /* Use a simple timer to reduce pressure on Udon */
   _timer -= Time.deltaTime;
   if( _timer < 0.0f )
   {
      _timer = k_timer_interval;

      /* 
       * Try compress and upload the texture if we're the owner
       */
      if( Networking.GetOwner( _store_main.gameObject ) == 
            Networking.LocalPlayer )
      {
         if( _picture_taken )
         {
            if( _compressor._opg_upload( _rt_src, 512, 256, 
                                         _store_main, _store_delta ) )
            {
               _picture_taken = false;
            }
         }
      }
      
      /* 
       * Read back the compressed texture. We don't actually have to do this
       * at all if we own the object. But in the demo we use it to preview the
       * result.
       */
      _compressor._opg_decode( _store_main, _store_delta, Color.white, _rt_dst);
   }
}
}
