'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _index = require('../../index.js');

var _index2 = _interopRequireDefault(_index);

var _imagecanvas = require('../../imagecanvas.js');

var _imagecanvas2 = _interopRequireDefault(_imagecanvas);

var _layertype = require('../../layertype.js');

var _layertype2 = _interopRequireDefault(_layertype);

var _viewhint = require('../../viewhint.js');

var _viewhint2 = _interopRequireDefault(_viewhint);

var _array = require('../../array.js');

var _array2 = _interopRequireDefault(_array);

var _extent = require('../../extent.js');

var _extent2 = _interopRequireDefault(_extent);

var _vectorrendertype = require('../../layer/vectorrendertype.js');

var _vectorrendertype2 = _interopRequireDefault(_vectorrendertype);

var _obj = require('../../obj.js');

var _obj2 = _interopRequireDefault(_obj);

var _plugins = require('../../plugins.js');

var _plugins2 = _interopRequireDefault(_plugins);

var _type = require('../type.js');

var _type2 = _interopRequireDefault(_type);

var _intermediatecanvas = require('../canvas/intermediatecanvas.js');

var _intermediatecanvas2 = _interopRequireDefault(_intermediatecanvas);

var _transform = require('../../transform.js');

var _transform2 = _interopRequireDefault(_transform);

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

/**
 * @constructor
 * @extends {ol.renderer.canvas.IntermediateCanvas}
 * @param {ol.layer.Image} imageLayer Single image layer.
 * @api
 */
var _ol_renderer_canvas_ImageLayer_ = function (imageLayer) {

  _intermediatecanvas2.default.call(this, imageLayer);

  /**
   * @private
   * @type {?ol.ImageBase}
   */
  this.image_ = null;

  /**
   * @private
   * @type {ol.Transform}
   */
  this.imageTransform_ = _transform2.default.create();

  /**
   * @type {!Array.<string>}
   */
  this.skippedFeatures_ = [];

  /**
   * @private
   * @type {ol.renderer.canvas.VectorLayer}
   */
  this.vectorRenderer_ = null;
};

_index2.default.inherits(_ol_renderer_canvas_ImageLayer_, _intermediatecanvas2.default);

/**
 * Determine if this renderer handles the provided layer.
 * @param {ol.renderer.Type} type The renderer type.
 * @param {ol.layer.Layer} layer The candidate layer.
 * @return {boolean} The renderer can render the layer.
 */
_ol_renderer_canvas_ImageLayer_['handles'] = function (type, layer) {
  return type === _type2.default.CANVAS && (layer.getType() === _layertype2.default.IMAGE || layer.getType() === _layertype2.default.VECTOR &&
  /** @type {ol.layer.Vector} */layer.getRenderMode() === _vectorrendertype2.default.IMAGE);
};

/**
 * Create a layer renderer.
 * @param {ol.renderer.Map} mapRenderer The map renderer.
 * @param {ol.layer.Layer} layer The layer to be rendererd.
 * @return {ol.renderer.canvas.ImageLayer} The layer renderer.
 */
_ol_renderer_canvas_ImageLayer_['create'] = function (mapRenderer, layer) {
  var renderer = new _ol_renderer_canvas_ImageLayer_( /** @type {ol.layer.Image} */layer);
  if (layer.getType() === _layertype2.default.VECTOR) {
    var candidates = _plugins2.default.getLayerRendererPlugins();
    for (var i = 0, ii = candidates.length; i < ii; ++i) {
      var candidate = /** @type {Object.<string, Function>} */candidates[i];
      if (candidate !== _ol_renderer_canvas_ImageLayer_ && candidate['handles'](_type2.default.CANVAS, layer)) {
        renderer.setVectorRenderer(candidate['create'](mapRenderer, layer));
      }
    }
  }
  return renderer;
};

/**
 * @inheritDoc
 */
_ol_renderer_canvas_ImageLayer_.prototype.getImage = function () {
  return !this.image_ ? null : this.image_.getImage();
};

/**
 * @inheritDoc
 */
_ol_renderer_canvas_ImageLayer_.prototype.getImageTransform = function () {
  return this.imageTransform_;
};

/**
 * @inheritDoc
 */
_ol_renderer_canvas_ImageLayer_.prototype.prepareFrame = function (frameState, layerState) {

  var pixelRatio = frameState.pixelRatio;
  var size = frameState.size;
  var viewState = frameState.viewState;
  var viewCenter = viewState.center;
  var viewResolution = viewState.resolution;

  var image;
  var imageLayer = /** @type {ol.layer.Image} */this.getLayer();
  var imageSource = imageLayer.getSource();

  var hints = frameState.viewHints;

  var renderedExtent = frameState.extent;
  if (layerState.extent !== undefined) {
    renderedExtent = _extent2.default.getIntersection(renderedExtent, layerState.extent);
  }

  if (!hints[_viewhint2.default.ANIMATING] && !hints[_viewhint2.default.INTERACTING] && !_extent2.default.isEmpty(renderedExtent)) {
    var projection = viewState.projection;
    if (!_index2.default.ENABLE_RASTER_REPROJECTION) {
      var sourceProjection = imageSource.getProjection();
      if (sourceProjection) {
        projection = sourceProjection;
      }
    }
    var vectorRenderer = this.vectorRenderer_;
    if (vectorRenderer) {
      var context = vectorRenderer.context;
      var imageFrameState = /** @type {olx.FrameState} */_obj2.default.assign({}, frameState, {
        size: [_extent2.default.getWidth(renderedExtent) / viewResolution, _extent2.default.getHeight(renderedExtent) / viewResolution],
        viewState: /** @type {olx.ViewState} */_obj2.default.assign({}, frameState.viewState, {
          rotation: 0
        })
      });
      var skippedFeatures = Object.keys(imageFrameState.skippedFeatureUids).sort();
      if (vectorRenderer.prepareFrame(imageFrameState, layerState) && (vectorRenderer.replayGroupChanged || !_array2.default.equals(skippedFeatures, this.skippedFeatures_))) {
        context.canvas.width = imageFrameState.size[0] * pixelRatio;
        context.canvas.height = imageFrameState.size[1] * pixelRatio;
        vectorRenderer.composeFrame(imageFrameState, layerState, context);
        this.image_ = new _imagecanvas2.default(renderedExtent, viewResolution, pixelRatio, context.canvas);
        this.skippedFeatures_ = skippedFeatures;
      }
    } else {
      image = imageSource.getImage(renderedExtent, viewResolution, pixelRatio, projection);
      if (image) {
        var loaded = this.loadImage(image);
        if (loaded) {
          this.image_ = image;
        }
      }
    }
  }

  if (this.image_) {
    image = this.image_;
    var imageExtent = image.getExtent();
    var imageResolution = image.getResolution();
    var imagePixelRatio = image.getPixelRatio();
    var scale = pixelRatio * imageResolution / (viewResolution * imagePixelRatio);
    var transform = _transform2.default.compose(this.imageTransform_, pixelRatio * size[0] / 2, pixelRatio * size[1] / 2, scale, scale, 0, imagePixelRatio * (imageExtent[0] - viewCenter[0]) / imageResolution, imagePixelRatio * (viewCenter[1] - imageExtent[3]) / imageResolution);
    _transform2.default.compose(this.coordinateToCanvasPixelTransform, pixelRatio * size[0] / 2 - transform[4], pixelRatio * size[1] / 2 - transform[5], pixelRatio / viewResolution, -pixelRatio / viewResolution, 0, -viewCenter[0], -viewCenter[1]);

    this.updateLogos(frameState, imageSource);
    this.renderedResolution = imageResolution * pixelRatio / imagePixelRatio;
  }

  return !!this.image_;
};

/**
 * @inheritDoc
 */
_ol_renderer_canvas_ImageLayer_.prototype.forEachFeatureAtCoordinate = function (coordinate, frameState, hitTolerance, callback, thisArg) {
  if (this.vectorRenderer_) {
    return this.vectorRenderer_.forEachFeatureAtCoordinate(coordinate, frameState, hitTolerance, callback, thisArg);
  } else {
    return _intermediatecanvas2.default.prototype.forEachFeatureAtCoordinate.call(this, coordinate, frameState, hitTolerance, callback, thisArg);
  }
};

/**
 * @param {ol.renderer.canvas.VectorLayer} renderer Vector renderer.
 */
_ol_renderer_canvas_ImageLayer_.prototype.setVectorRenderer = function (renderer) {
  this.vectorRenderer_ = renderer;
};
exports.default = _ol_renderer_canvas_ImageLayer_;
module.exports = exports['default'];