'use strict';

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

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

var _index2 = _interopRequireDefault(_index);

var _tile = require('./tile.js');

var _tile2 = _interopRequireDefault(_tile);

var _tilestate = require('./tilestate.js');

var _tilestate2 = _interopRequireDefault(_tilestate);

var _dom = require('./dom.js');

var _dom2 = _interopRequireDefault(_dom);

var _events = require('./events.js');

var _events2 = _interopRequireDefault(_events);

var _eventtype = require('./events/eventtype.js');

var _eventtype2 = _interopRequireDefault(_eventtype);

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

/**
 * @constructor
 * @extends {ol.Tile}
 * @param {ol.TileCoord} tileCoord Tile coordinate.
 * @param {ol.TileState} state State.
 * @param {string} src Image source URI.
 * @param {?string} crossOrigin Cross origin.
 * @param {ol.TileLoadFunctionType} tileLoadFunction Tile load function.
 * @param {olx.TileOptions=} opt_options Tile options.
 */
var _ol_ImageTile_ = function (tileCoord, state, src, crossOrigin, tileLoadFunction, opt_options) {

  _tile2.default.call(this, tileCoord, state, opt_options);

  /**
   * @private
   * @type {?string}
   */
  this.crossOrigin_ = crossOrigin;

  /**
   * Image URI
   *
   * @private
   * @type {string}
   */
  this.src_ = src;

  /**
   * @private
   * @type {Image|HTMLCanvasElement}
   */
  this.image_ = new Image();
  if (crossOrigin !== null) {
    this.image_.crossOrigin = crossOrigin;
  }

  /**
   * @private
   * @type {Array.<ol.EventsKey>}
   */
  this.imageListenerKeys_ = null;

  /**
   * @private
   * @type {ol.TileLoadFunctionType}
   */
  this.tileLoadFunction_ = tileLoadFunction;
};

_index2.default.inherits(_ol_ImageTile_, _tile2.default);

/**
 * @inheritDoc
 */
_ol_ImageTile_.prototype.disposeInternal = function () {
  if (this.state == _tilestate2.default.LOADING) {
    this.unlistenImage_();
    this.image_ = _ol_ImageTile_.getBlankImage();
  }
  if (this.interimTile) {
    this.interimTile.dispose();
  }
  this.state = _tilestate2.default.ABORT;
  this.changed();
  _tile2.default.prototype.disposeInternal.call(this);
};

/**
 * Get the HTML image element for this tile (may be a Canvas, Image, or Video).
 * @return {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement} Image.
 * @api
 */
_ol_ImageTile_.prototype.getImage = function () {
  return this.image_;
};

/**
 * @inheritDoc
 */
_ol_ImageTile_.prototype.getKey = function () {
  return this.src_;
};

/**
 * Tracks loading or read errors.
 *
 * @private
 */
_ol_ImageTile_.prototype.handleImageError_ = function () {
  this.state = _tilestate2.default.ERROR;
  this.unlistenImage_();
  this.image_ = _ol_ImageTile_.getBlankImage();
  this.changed();
};

/**
 * Tracks successful image load.
 *
 * @private
 */
_ol_ImageTile_.prototype.handleImageLoad_ = function () {
  if (this.image_.naturalWidth && this.image_.naturalHeight) {
    this.state = _tilestate2.default.LOADED;
  } else {
    this.state = _tilestate2.default.EMPTY;
  }
  this.unlistenImage_();
  this.changed();
};

/**
 * @inheritDoc
 * @api
 */
_ol_ImageTile_.prototype.load = function () {
  if (this.state == _tilestate2.default.ERROR) {
    this.state = _tilestate2.default.IDLE;
    this.image_ = new Image();
    if (this.crossOrigin_ !== null) {
      this.image_.crossOrigin = this.crossOrigin_;
    }
  }
  if (this.state == _tilestate2.default.IDLE) {
    this.state = _tilestate2.default.LOADING;
    this.changed();
    this.imageListenerKeys_ = [_events2.default.listenOnce(this.image_, _eventtype2.default.ERROR, this.handleImageError_, this), _events2.default.listenOnce(this.image_, _eventtype2.default.LOAD, this.handleImageLoad_, this)];
    this.tileLoadFunction_(this, this.src_);
  }
};

/**
 * Discards event handlers which listen for load completion or errors.
 *
 * @private
 */
_ol_ImageTile_.prototype.unlistenImage_ = function () {
  this.imageListenerKeys_.forEach(_events2.default.unlistenByKey);
  this.imageListenerKeys_ = null;
};

/**
 * Get a 1-pixel blank image.
 * @return {HTMLCanvasElement} Blank image.
 */
_ol_ImageTile_.getBlankImage = function () {
  var ctx = _dom2.default.createCanvasContext2D(1, 1);
  ctx.fillStyle = 'rgba(0,0,0,0)';
  ctx.fillRect(0, 0, 1, 1);
  return ctx.canvas;
};
exports.default = _ol_ImageTile_;
module.exports = exports['default'];