import {EffectComposer as EffectComposer2} from "../../../../modules/three/examples/jsm/postprocessing/EffectComposer";
import {RenderPass as RenderPass2} from "../../../../modules/three/examples/jsm/postprocessing/RenderPass";
import {Vector2 as Vector22} from "three/src/math/Vector2";
import {NodeParamsConfig, ParamConfig} from "../../utils/params/ParamsConfig";
import {RGBFormat} from "three/src/constants";
import {Poly as Poly2} from "../../../Poly";
import {
  MAG_FILTER_DEFAULT_VALUE,
  MAG_FILTER_MENU_ENTRIES,
  MIN_FILTER_DEFAULT_VALUE,
  MIN_FILTER_MENU_ENTRIES
} from "../../../../core/cop/ConstantFilter";
export class PostProcessNetworkParamsConfig extends NodeParamsConfig {
  constructor() {
    super(...arguments);
    this.prepend_render_pass = ParamConfig.BOOLEAN(1);
    this.useRenderTarget = ParamConfig.BOOLEAN(1);
    this.tmagFilter = ParamConfig.BOOLEAN(0, {
      visibleIf: {useRenderTarget: 1}
    });
    this.magFilter = ParamConfig.INTEGER(MAG_FILTER_DEFAULT_VALUE, {
      visibleIf: {useRenderTarget: 1, tmagFilter: 1},
      menu: {
        entries: MAG_FILTER_MENU_ENTRIES
      }
    });
    this.tminFilter = ParamConfig.BOOLEAN(0, {
      visibleIf: {useRenderTarget: 1}
    });
    this.minFilter = ParamConfig.INTEGER(MIN_FILTER_DEFAULT_VALUE, {
      visibleIf: {useRenderTarget: 1, tminFilter: 1},
      menu: {
        entries: MIN_FILTER_MENU_ENTRIES
      }
    });
    this.stencilBuffer = ParamConfig.BOOLEAN(0, {
      visibleIf: {useRenderTarget: 1}
    });
    this.sampling = ParamConfig.INTEGER(1, {
      range: [1, 4],
      rangeLocked: [true, false]
    });
  }
}
export class EffectsComposerController {
  constructor(node) {
    this.node = node;
    this._renderer_size = new Vector22();
  }
  display_node_controller_callbacks() {
    return {
      on_display_node_remove: () => {
      },
      on_display_node_set: () => {
        this.node.setDirty();
      },
      on_display_node_update: () => {
        this.node.setDirty();
      }
    };
  }
  create_effects_composer(options) {
    const renderer = options.renderer;
    let composer;
    if (this.node.pv.useRenderTarget) {
      const render_target = this._create_render_target(renderer);
      composer = new EffectComposer2(renderer, render_target);
    } else {
      composer = new EffectComposer2(renderer);
    }
    composer.setPixelRatio(window.devicePixelRatio * this.node.pv.sampling);
    this._build_passes(composer, options);
    return composer;
  }
  _create_render_target(renderer) {
    let render_target;
    renderer.autoClear = false;
    const parameters = {
      format: RGBFormat,
      stencilBuffer: this.node.pv.stencilBuffer
    };
    if (this.node.pv.tminfilter) {
      parameters.minFilter = this.node.pv.minFilter;
    }
    if (this.node.pv.tminfilter) {
      parameters.magFilter = this.node.pv.magFilter;
    }
    renderer.getDrawingBufferSize(this._renderer_size);
    render_target = Poly2.renderersController.renderTarget(this._renderer_size.x, this._renderer_size.y, parameters);
    return render_target;
  }
  _build_passes(composer, options) {
    if (this.node.pv.prepend_render_pass == true) {
      const render_pass = new RenderPass2(options.scene, options.camera);
      composer.addPass(render_pass);
    }
    const post_node = this.node.display_node_controller.display_node;
    if (post_node) {
      post_node.setup_composer({
        composer,
        camera: options.camera,
        resolution: options.resolution,
        camera_node: options.camera_node,
        scene: options.scene,
        requester: options.requester
      });
    }
  }
}
