goog.provide('ol.renderer.webgl.VectorLayer');

goog.require('ol');
goog.require('ol.ViewHint');
goog.require('ol.extent');
goog.require('ol.render.webgl.ReplayGroup');
goog.require('ol.renderer.vector');
goog.require('ol.renderer.webgl.Layer');
goog.require('ol.transform');


if (ol.ENABLE_WEBGL) {

  /**
   * @constructor
   * @extends {ol.renderer.webgl.Layer}
   * @param {ol.renderer.webgl.Map} mapRenderer Map renderer.
   * @param {ol.layer.Vector} vectorLayer Vector layer.
   */
  ol.renderer.webgl.VectorLayer = function(mapRenderer, vectorLayer) {

    ol.renderer.webgl.Layer.call(this, mapRenderer, vectorLayer);

    /**
     * @private
     * @type {boolean}
     */
    this.dirty_ = false;

    /**
     * @private
     * @type {number}
     */
    this.renderedRevision_ = -1;

    /**
     * @private
     * @type {number}
     */
    this.renderedResolution_ = NaN;

    /**
     * @private
     * @type {ol.Extent}
     */
    this.renderedExtent_ = ol.extent.createEmpty();

    /**
     * @private
     * @type {function(ol.Feature, ol.Feature): number|null}
     */
    this.renderedRenderOrder_ = null;

    /**
     * @private
     * @type {ol.render.webgl.ReplayGroup}
     */
    this.replayGroup_ = null;

    /**
     * The last layer state.
     * @private
     * @type {?ol.LayerState}
     */
    this.layerState_ = null;

  };
  ol.inherits(ol.renderer.webgl.VectorLayer, ol.renderer.webgl.Layer);


  /**
   * @inheritDoc
   */
  ol.renderer.webgl.VectorLayer.prototype.composeFrame = function(frameState, layerState, context) {
    this.layerState_ = layerState;
    var viewState = frameState.viewState;
    var replayGroup = this.replayGroup_;
    var size = frameState.size;
    var pixelRatio = frameState.pixelRatio;
    var gl = this.mapRenderer.getGL();
    if (replayGroup && !replayGroup.isEmpty()) {
      gl.enable(gl.SCISSOR_TEST);
      gl.scissor(0, 0, size[0] * pixelRatio, size[1] * pixelRatio);
      replayGroup.replay(context,
          viewState.center, viewState.resolution, viewState.rotation,
          size, pixelRatio, layerState.opacity,
          layerState.managed ? frameState.skippedFeatureUids : {});
      gl.disable(gl.SCISSOR_TEST);
    }

  };


  /**
   * @inheritDoc
   */
  ol.renderer.webgl.VectorLayer.prototype.disposeInternal = function() {
    var replayGroup = this.replayGroup_;
    if (replayGroup) {
      var context = this.mapRenderer.getContext();
      replayGroup.getDeleteResourcesFunction(context)();
      this.replayGroup_ = null;
    }
    ol.renderer.webgl.Layer.prototype.disposeInternal.call(this);
  };


  /**
   * @inheritDoc
   */
  ol.renderer.webgl.VectorLayer.prototype.forEachFeatureAtCoordinate = function(coordinate, frameState, hitTolerance, callback, thisArg) {
    if (!this.replayGroup_ || !this.layerState_) {
      return undefined;
    } else {
      var context = this.mapRenderer.getContext();
      var viewState = frameState.viewState;
      var layer = this.getLayer();
      var layerState = this.layerState_;
      /** @type {Object.<string, boolean>} */
      var features = {};
      return this.replayGroup_.forEachFeatureAtCoordinate(coordinate,
          context, viewState.center, viewState.resolution, viewState.rotation,
          frameState.size, frameState.pixelRatio, layerState.opacity,
          {},
          /**
           * @param {ol.Feature|ol.render.Feature} feature Feature.
           * @return {?} Callback result.
           */
          function(feature) {
            var key = ol.getUid(feature).toString();
            if (!(key in features)) {
              features[key] = true;
              return callback.call(thisArg, feature, layer);
            }
          });
    }
  };


  /**
   * @inheritDoc
   */
  ol.renderer.webgl.VectorLayer.prototype.hasFeatureAtCoordinate = function(coordinate, frameState) {
    if (!this.replayGroup_ || !this.layerState_) {
      return false;
    } else {
      var context = this.mapRenderer.getContext();
      var viewState = frameState.viewState;
      var layerState = this.layerState_;
      return this.replayGroup_.hasFeatureAtCoordinate(coordinate,
          context, viewState.center, viewState.resolution, viewState.rotation,
          frameState.size, frameState.pixelRatio, layerState.opacity,
          frameState.skippedFeatureUids);
    }
  };


  /**
   * @inheritDoc
   */
  ol.renderer.webgl.VectorLayer.prototype.forEachLayerAtPixel = function(pixel, frameState, callback, thisArg) {
    var coordinate = ol.transform.apply(
        frameState.pixelToCoordinateTransform, pixel.slice());
    var hasFeature = this.hasFeatureAtCoordinate(coordinate, frameState);

    if (hasFeature) {
      return callback.call(thisArg, this.getLayer(), null);
    } else {
      return undefined;
    }
  };


  /**
   * Handle changes in image style state.
   * @param {ol.events.Event} event Image style change event.
   * @private
   */
  ol.renderer.webgl.VectorLayer.prototype.handleStyleImageChange_ = function(event) {
    this.renderIfReadyAndVisible();
  };


  /**
   * @inheritDoc
   */
  ol.renderer.webgl.VectorLayer.prototype.prepareFrame = function(frameState, layerState, context) {

    var vectorLayer = /** @type {ol.layer.Vector} */ (this.getLayer());
    var vectorSource = vectorLayer.getSource();

    this.updateAttributions(
        frameState.attributions, vectorSource.getAttributions());
    this.updateLogos(frameState, vectorSource);

    var animating = frameState.viewHints[ol.ViewHint.ANIMATING];
    var interacting = frameState.viewHints[ol.ViewHint.INTERACTING];
    var updateWhileAnimating = vectorLayer.getUpdateWhileAnimating();
    var updateWhileInteracting = vectorLayer.getUpdateWhileInteracting();

    if (!this.dirty_ && (!updateWhileAnimating && animating) ||
        (!updateWhileInteracting && interacting)) {
      return true;
    }

    var frameStateExtent = frameState.extent;
    var viewState = frameState.viewState;
    var projection = viewState.projection;
    var resolution = viewState.resolution;
    var pixelRatio = frameState.pixelRatio;
    var vectorLayerRevision = vectorLayer.getRevision();
    var vectorLayerRenderBuffer = vectorLayer.getRenderBuffer();
    var vectorLayerRenderOrder = vectorLayer.getRenderOrder();

    if (vectorLayerRenderOrder === undefined) {
      vectorLayerRenderOrder = ol.renderer.vector.defaultOrder;
    }

    var extent = ol.extent.buffer(frameStateExtent,
        vectorLayerRenderBuffer * resolution);

    if (!this.dirty_ &&
        this.renderedResolution_ == resolution &&
        this.renderedRevision_ == vectorLayerRevision &&
        this.renderedRenderOrder_ == vectorLayerRenderOrder &&
        ol.extent.containsExtent(this.renderedExtent_, extent)) {
      return true;
    }

    if (this.replayGroup_) {
      frameState.postRenderFunctions.push(
          this.replayGroup_.getDeleteResourcesFunction(context));
    }

    this.dirty_ = false;

    var replayGroup = new ol.render.webgl.ReplayGroup(
        ol.renderer.vector.getTolerance(resolution, pixelRatio),
        extent, vectorLayer.getRenderBuffer());
    vectorSource.loadFeatures(extent, resolution, projection);
    /**
     * @param {ol.Feature} feature Feature.
     * @this {ol.renderer.webgl.VectorLayer}
     */
    var renderFeature = function(feature) {
      var styles;
      var styleFunction = feature.getStyleFunction();
      if (styleFunction) {
        styles = styleFunction.call(feature, resolution);
      } else {
        styleFunction = vectorLayer.getStyleFunction();
        if (styleFunction) {
          styles = styleFunction(feature, resolution);
        }
      }
      if (styles) {
        var dirty = this.renderFeature(
            feature, resolution, pixelRatio, styles, replayGroup);
        this.dirty_ = this.dirty_ || dirty;
      }
    };
    if (vectorLayerRenderOrder) {
      /** @type {Array.<ol.Feature>} */
      var features = [];
      vectorSource.forEachFeatureInExtent(extent,
          /**
           * @param {ol.Feature} feature Feature.
           */
          function(feature) {
            features.push(feature);
          }, this);
      features.sort(vectorLayerRenderOrder);
      features.forEach(renderFeature, this);
    } else {
      vectorSource.forEachFeatureInExtent(extent, renderFeature, this);
    }
    replayGroup.finish(context);

    this.renderedResolution_ = resolution;
    this.renderedRevision_ = vectorLayerRevision;
    this.renderedRenderOrder_ = vectorLayerRenderOrder;
    this.renderedExtent_ = extent;
    this.replayGroup_ = replayGroup;

    return true;
  };


  /**
   * @param {ol.Feature} feature Feature.
   * @param {number} resolution Resolution.
   * @param {number} pixelRatio Pixel ratio.
   * @param {(ol.style.Style|Array.<ol.style.Style>)} styles The style or array of
   *     styles.
   * @param {ol.render.webgl.ReplayGroup} replayGroup Replay group.
   * @return {boolean} `true` if an image is loading.
   */
  ol.renderer.webgl.VectorLayer.prototype.renderFeature = function(feature, resolution, pixelRatio, styles, replayGroup) {
    if (!styles) {
      return false;
    }
    var loading = false;
    if (Array.isArray(styles)) {
      for (var i = styles.length - 1, ii = 0; i >= ii; --i) {
        loading = ol.renderer.vector.renderFeature(
            replayGroup, feature, styles[i],
            ol.renderer.vector.getSquaredTolerance(resolution, pixelRatio),
            this.handleStyleImageChange_, this) || loading;
      }
    } else {
      loading = ol.renderer.vector.renderFeature(
          replayGroup, feature, styles,
          ol.renderer.vector.getSquaredTolerance(resolution, pixelRatio),
          this.handleStyleImageChange_, this) || loading;
    }
    return loading;
  };

}
