'use strict';

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

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

var _index2 = _interopRequireDefault(_index);

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

var _extent2 = _interopRequireDefault(_extent);

var _geometrytype = require('../../geom/geometrytype.js');

var _geometrytype2 = _interopRequireDefault(_geometrytype);

var _replaytype = require('../replaytype.js');

var _replaytype2 = _interopRequireDefault(_replaytype);

var _vectorcontext = require('../vectorcontext.js');

var _vectorcontext2 = _interopRequireDefault(_vectorcontext);

var _replaygroup = require('../webgl/replaygroup.js');

var _replaygroup2 = _interopRequireDefault(_replaygroup);

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

/**
 * @constructor
 * @extends {ol.render.VectorContext}
 * @param {ol.webgl.Context} context Context.
 * @param {ol.Coordinate} center Center.
 * @param {number} resolution Resolution.
 * @param {number} rotation Rotation.
 * @param {ol.Size} size Size.
 * @param {ol.Extent} extent Extent.
 * @param {number} pixelRatio Pixel ratio.
 * @struct
 */
var _ol_render_webgl_Immediate_ = function (context, center, resolution, rotation, size, extent, pixelRatio) {
  _vectorcontext2.default.call(this);

  /**
   * @private
   */
  this.context_ = context;

  /**
   * @private
   */
  this.center_ = center;

  /**
   * @private
   */
  this.extent_ = extent;

  /**
   * @private
   */
  this.pixelRatio_ = pixelRatio;

  /**
   * @private
   */
  this.size_ = size;

  /**
   * @private
   */
  this.rotation_ = rotation;

  /**
   * @private
   */
  this.resolution_ = resolution;

  /**
   * @private
   * @type {ol.style.Image}
   */
  this.imageStyle_ = null;

  /**
   * @private
   * @type {ol.style.Fill}
   */
  this.fillStyle_ = null;

  /**
   * @private
   * @type {ol.style.Stroke}
   */
  this.strokeStyle_ = null;

  /**
   * @private
   * @type {ol.style.Text}
   */
  this.textStyle_ = null;
};

_index2.default.inherits(_ol_render_webgl_Immediate_, _vectorcontext2.default);

/**
 * @param {ol.render.webgl.ReplayGroup} replayGroup Replay group.
 * @param {ol.geom.Geometry|ol.render.Feature} geometry Geometry.
 * @private
 */
_ol_render_webgl_Immediate_.prototype.drawText_ = function (replayGroup, geometry) {
  var context = this.context_;
  var replay = /** @type {ol.render.webgl.TextReplay} */replayGroup.getReplay(0, _replaytype2.default.TEXT);
  replay.setTextStyle(this.textStyle_);
  replay.drawText(geometry, null);
  replay.finish(context);
  // default colors
  var opacity = 1;
  var skippedFeatures = {};
  var featureCallback;
  var oneByOne = false;
  replay.replay(this.context_, this.center_, this.resolution_, this.rotation_, this.size_, this.pixelRatio_, opacity, skippedFeatures, featureCallback, oneByOne);
  replay.getDeleteResourcesFunction(context)();
};

/**
 * Set the rendering style.  Note that since this is an immediate rendering API,
 * any `zIndex` on the provided style will be ignored.
 *
 * @param {ol.style.Style} style The rendering style.
 * @override
 * @api
 */
_ol_render_webgl_Immediate_.prototype.setStyle = function (style) {
  this.setFillStrokeStyle(style.getFill(), style.getStroke());
  this.setImageStyle(style.getImage());
  this.setTextStyle(style.getText());
};

/**
 * Render a geometry into the canvas.  Call
 * {@link ol.render.webgl.Immediate#setStyle} first to set the rendering style.
 *
 * @param {ol.geom.Geometry|ol.render.Feature} geometry The geometry to render.
 * @override
 * @api
 */
_ol_render_webgl_Immediate_.prototype.drawGeometry = function (geometry) {
  var type = geometry.getType();
  switch (type) {
    case _geometrytype2.default.POINT:
      this.drawPoint( /** @type {ol.geom.Point} */geometry, null);
      break;
    case _geometrytype2.default.LINE_STRING:
      this.drawLineString( /** @type {ol.geom.LineString} */geometry, null);
      break;
    case _geometrytype2.default.POLYGON:
      this.drawPolygon( /** @type {ol.geom.Polygon} */geometry, null);
      break;
    case _geometrytype2.default.MULTI_POINT:
      this.drawMultiPoint( /** @type {ol.geom.MultiPoint} */geometry, null);
      break;
    case _geometrytype2.default.MULTI_LINE_STRING:
      this.drawMultiLineString( /** @type {ol.geom.MultiLineString} */geometry, null);
      break;
    case _geometrytype2.default.MULTI_POLYGON:
      this.drawMultiPolygon( /** @type {ol.geom.MultiPolygon} */geometry, null);
      break;
    case _geometrytype2.default.GEOMETRY_COLLECTION:
      this.drawGeometryCollection( /** @type {ol.geom.GeometryCollection} */geometry, null);
      break;
    case _geometrytype2.default.CIRCLE:
      this.drawCircle( /** @type {ol.geom.Circle} */geometry, null);
      break;
    default:
    // pass
  }
};

/**
 * @inheritDoc
 * @api
 */
_ol_render_webgl_Immediate_.prototype.drawFeature = function (feature, style) {
  var geometry = style.getGeometryFunction()(feature);
  if (!geometry || !_extent2.default.intersects(this.extent_, geometry.getExtent())) {
    return;
  }
  this.setStyle(style);
  this.drawGeometry(geometry);
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.drawGeometryCollection = function (geometry, data) {
  var geometries = geometry.getGeometriesArray();
  var i, ii;
  for (i = 0, ii = geometries.length; i < ii; ++i) {
    this.drawGeometry(geometries[i]);
  }
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.drawPoint = function (geometry, data) {
  var context = this.context_;
  var replayGroup = new _replaygroup2.default(1, this.extent_);
  var replay = /** @type {ol.render.webgl.ImageReplay} */replayGroup.getReplay(0, _replaytype2.default.IMAGE);
  replay.setImageStyle(this.imageStyle_);
  replay.drawPoint(geometry, data);
  replay.finish(context);
  // default colors
  var opacity = 1;
  var skippedFeatures = {};
  var featureCallback;
  var oneByOne = false;
  replay.replay(this.context_, this.center_, this.resolution_, this.rotation_, this.size_, this.pixelRatio_, opacity, skippedFeatures, featureCallback, oneByOne);
  replay.getDeleteResourcesFunction(context)();

  if (this.textStyle_) {
    this.drawText_(replayGroup, geometry);
  }
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.drawMultiPoint = function (geometry, data) {
  var context = this.context_;
  var replayGroup = new _replaygroup2.default(1, this.extent_);
  var replay = /** @type {ol.render.webgl.ImageReplay} */replayGroup.getReplay(0, _replaytype2.default.IMAGE);
  replay.setImageStyle(this.imageStyle_);
  replay.drawMultiPoint(geometry, data);
  replay.finish(context);
  var opacity = 1;
  var skippedFeatures = {};
  var featureCallback;
  var oneByOne = false;
  replay.replay(this.context_, this.center_, this.resolution_, this.rotation_, this.size_, this.pixelRatio_, opacity, skippedFeatures, featureCallback, oneByOne);
  replay.getDeleteResourcesFunction(context)();

  if (this.textStyle_) {
    this.drawText_(replayGroup, geometry);
  }
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.drawLineString = function (geometry, data) {
  var context = this.context_;
  var replayGroup = new _replaygroup2.default(1, this.extent_);
  var replay = /** @type {ol.render.webgl.LineStringReplay} */replayGroup.getReplay(0, _replaytype2.default.LINE_STRING);
  replay.setFillStrokeStyle(null, this.strokeStyle_);
  replay.drawLineString(geometry, data);
  replay.finish(context);
  var opacity = 1;
  var skippedFeatures = {};
  var featureCallback;
  var oneByOne = false;
  replay.replay(this.context_, this.center_, this.resolution_, this.rotation_, this.size_, this.pixelRatio_, opacity, skippedFeatures, featureCallback, oneByOne);
  replay.getDeleteResourcesFunction(context)();

  if (this.textStyle_) {
    this.drawText_(replayGroup, geometry);
  }
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.drawMultiLineString = function (geometry, data) {
  var context = this.context_;
  var replayGroup = new _replaygroup2.default(1, this.extent_);
  var replay = /** @type {ol.render.webgl.LineStringReplay} */replayGroup.getReplay(0, _replaytype2.default.LINE_STRING);
  replay.setFillStrokeStyle(null, this.strokeStyle_);
  replay.drawMultiLineString(geometry, data);
  replay.finish(context);
  var opacity = 1;
  var skippedFeatures = {};
  var featureCallback;
  var oneByOne = false;
  replay.replay(this.context_, this.center_, this.resolution_, this.rotation_, this.size_, this.pixelRatio_, opacity, skippedFeatures, featureCallback, oneByOne);
  replay.getDeleteResourcesFunction(context)();

  if (this.textStyle_) {
    this.drawText_(replayGroup, geometry);
  }
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.drawPolygon = function (geometry, data) {
  var context = this.context_;
  var replayGroup = new _replaygroup2.default(1, this.extent_);
  var replay = /** @type {ol.render.webgl.PolygonReplay} */replayGroup.getReplay(0, _replaytype2.default.POLYGON);
  replay.setFillStrokeStyle(this.fillStyle_, this.strokeStyle_);
  replay.drawPolygon(geometry, data);
  replay.finish(context);
  var opacity = 1;
  var skippedFeatures = {};
  var featureCallback;
  var oneByOne = false;
  replay.replay(this.context_, this.center_, this.resolution_, this.rotation_, this.size_, this.pixelRatio_, opacity, skippedFeatures, featureCallback, oneByOne);
  replay.getDeleteResourcesFunction(context)();

  if (this.textStyle_) {
    this.drawText_(replayGroup, geometry);
  }
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.drawMultiPolygon = function (geometry, data) {
  var context = this.context_;
  var replayGroup = new _replaygroup2.default(1, this.extent_);
  var replay = /** @type {ol.render.webgl.PolygonReplay} */replayGroup.getReplay(0, _replaytype2.default.POLYGON);
  replay.setFillStrokeStyle(this.fillStyle_, this.strokeStyle_);
  replay.drawMultiPolygon(geometry, data);
  replay.finish(context);
  var opacity = 1;
  var skippedFeatures = {};
  var featureCallback;
  var oneByOne = false;
  replay.replay(this.context_, this.center_, this.resolution_, this.rotation_, this.size_, this.pixelRatio_, opacity, skippedFeatures, featureCallback, oneByOne);
  replay.getDeleteResourcesFunction(context)();

  if (this.textStyle_) {
    this.drawText_(replayGroup, geometry);
  }
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.drawCircle = function (geometry, data) {
  var context = this.context_;
  var replayGroup = new _replaygroup2.default(1, this.extent_);
  var replay = /** @type {ol.render.webgl.CircleReplay} */replayGroup.getReplay(0, _replaytype2.default.CIRCLE);
  replay.setFillStrokeStyle(this.fillStyle_, this.strokeStyle_);
  replay.drawCircle(geometry, data);
  replay.finish(context);
  var opacity = 1;
  var skippedFeatures = {};
  var featureCallback;
  var oneByOne = false;
  replay.replay(this.context_, this.center_, this.resolution_, this.rotation_, this.size_, this.pixelRatio_, opacity, skippedFeatures, featureCallback, oneByOne);
  replay.getDeleteResourcesFunction(context)();

  if (this.textStyle_) {
    this.drawText_(replayGroup, geometry);
  }
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.setImageStyle = function (imageStyle) {
  this.imageStyle_ = imageStyle;
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.setFillStrokeStyle = function (fillStyle, strokeStyle) {
  this.fillStyle_ = fillStyle;
  this.strokeStyle_ = strokeStyle;
};

/**
 * @inheritDoc
 */
_ol_render_webgl_Immediate_.prototype.setTextStyle = function (textStyle) {
  this.textStyle_ = textStyle;
};
exports.default = _ol_render_webgl_Immediate_;
module.exports = exports['default'];