'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 _size = require('../size.js');

var _size2 = _interopRequireDefault(_size);

var _tile3 = require('../source/tile.js');

var _tile4 = _interopRequireDefault(_tile3);

var _tilecoord = require('../tilecoord.js');

var _tilecoord2 = _interopRequireDefault(_tilecoord);

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

/**
 * @classdesc
 * A pseudo tile source, which does not fetch tiles from a server, but renders
 * a grid outline for the tile grid/projection along with the coordinates for
 * each tile. See examples/canvas-tiles for an example.
 *
 * Uses Canvas context2d, so requires Canvas support.
 *
 * @constructor
 * @extends {ol.source.Tile}
 * @param {olx.source.TileDebugOptions} options Debug tile options.
 * @api
 */
var _ol_source_TileDebug_ = function (options) {

  _tile4.default.call(this, {
    opaque: false,
    projection: options.projection,
    tileGrid: options.tileGrid,
    wrapX: options.wrapX !== undefined ? options.wrapX : true
  });
};

_index2.default.inherits(_ol_source_TileDebug_, _tile4.default);

/**
 * @inheritDoc
 */
_ol_source_TileDebug_.prototype.getTile = function (z, x, y) {
  var tileCoordKey = _tilecoord2.default.getKeyZXY(z, x, y);
  if (this.tileCache.containsKey(tileCoordKey)) {
    return (/** @type {!ol.source.TileDebug.Tile_} */this.tileCache.get(tileCoordKey)
    );
  } else {
    var tileSize = _size2.default.toSize(this.tileGrid.getTileSize(z));
    var tileCoord = [z, x, y];
    var textTileCoord = this.getTileCoordForTileUrlFunction(tileCoord);
    var text = !textTileCoord ? '' : this.getTileCoordForTileUrlFunction(textTileCoord).toString();
    var tile = new _ol_source_TileDebug_.Tile_(tileCoord, tileSize, text);
    this.tileCache.set(tileCoordKey, tile);
    return tile;
  }
};

/**
 * @constructor
 * @extends {ol.Tile}
 * @param {ol.TileCoord} tileCoord Tile coordinate.
 * @param {ol.Size} tileSize Tile size.
 * @param {string} text Text.
 * @private
 */
_ol_source_TileDebug_.Tile_ = function (tileCoord, tileSize, text) {

  _tile2.default.call(this, tileCoord, _tilestate2.default.LOADED);

  /**
   * @private
   * @type {ol.Size}
   */
  this.tileSize_ = tileSize;

  /**
   * @private
   * @type {string}
   */
  this.text_ = text;

  /**
   * @private
   * @type {HTMLCanvasElement}
   */
  this.canvas_ = null;
};
_index2.default.inherits(_ol_source_TileDebug_.Tile_, _tile2.default);

/**
 * Get the image element for this tile.
 * @return {HTMLCanvasElement} Image.
 */
_ol_source_TileDebug_.Tile_.prototype.getImage = function () {
  if (this.canvas_) {
    return this.canvas_;
  } else {
    var tileSize = this.tileSize_;
    var context = _dom2.default.createCanvasContext2D(tileSize[0], tileSize[1]);

    context.strokeStyle = 'black';
    context.strokeRect(0.5, 0.5, tileSize[0] + 0.5, tileSize[1] + 0.5);

    context.fillStyle = 'black';
    context.textAlign = 'center';
    context.textBaseline = 'middle';
    context.font = '24px sans-serif';
    context.fillText(this.text_, tileSize[0] / 2, tileSize[1] / 2);

    this.canvas_ = context.canvas;
    return context.canvas;
  }
};

/**
 * @override
 */
_ol_source_TileDebug_.Tile_.prototype.load = function () {};
exports.default = _ol_source_TileDebug_;
module.exports = exports['default'];