import { Filter, Renderer, Texture, WRAP_MODES } from "pixi.js";
import RTTShader from "../utils/RTTShader";

//@ts-expect-error Frag file
import filter_src from "./frag/InGameSpace.frag";

export interface SpaceTextures {
  noise_sparse;
  noise_fine;
}

export default class InGameSpaceFilter extends RTTShader {
  private elapsed: number;
  private filter: Filter;

  constructor(renderer: Renderer, scale: number, textures: SpaceTextures) {
    const noise_sparse = textures.noise_sparse.texture as Texture;
    const noise_fine = textures.noise_fine.texture as Texture;

    noise_sparse.baseTexture.wrapMode = WRAP_MODES.REPEAT;
    noise_fine.baseTexture.wrapMode = WRAP_MODES.REPEAT;

    const filter = new Filter(null, filter_src, {
      time: 0,
      ires: [renderer.width / scale, renderer.height / scale],
      noise_sparse,
      noise_fine,
      scroll_speed: 0,
      warp_boost: 0,
      warp_opacity: 0,
    });
    super(renderer, filter, scale);
    this.elapsed = 0;
    this.filter = filter;
  }

  rescale(scale: number) {
    super.rescale(scale);
    this.filter.uniforms.ires = [
      this.renderer.width / scale,
      this.renderer.height / scale,
    ];
  }

  update(delta: number) {
    this.filter.uniforms.time = this.elapsed;
    this.elapsed += delta / 60;
  }
}
