"use strict";

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

/**
 * Draws dash line
 * @param graphic
 * @param [x1]
 * @param [y1]
 * @param [x2]
 * @param [y2]
 * @param [dash]
 * @param [gap]
 */
function drawDashLine(graphic) {
  var x1 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  var y1 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var x2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
  var y2 = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
  var dash = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 16;
  var gap = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 8;
  var angle = Math.atan((y2 - y1) / (x2 - x1));
  var dashGapSum = dash + gap;
  var xMovement = dashGapSum * Math.cos(angle);
  var yMovement = dashGapSum * Math.sin(angle);
  var gapPercent = gap / dashGapSum;
  var dashXLength = xMovement * (1 - gapPercent);
  var dashYLength = yMovement * (1 - gapPercent);
  var gapXLength = xMovement * gapPercent;
  var gapYLength = yMovement * gapPercent;
  var xDir = xMovement / Math.abs(xMovement);
  var yDir = yMovement / Math.abs(yMovement);
  var currentPoint = {
    x: x1,
    y: y1
  };
  graphic.moveTo(currentPoint.x, currentPoint.y);

  while (currentPoint.x !== x2 || currentPoint.y !== y2) {
    currentPoint.x += dashXLength;
    currentPoint.y += dashYLength;
    clamp();
    graphic.lineTo(currentPoint.x, currentPoint.y);
    currentPoint.x += gapXLength;
    currentPoint.y += gapYLength;
    clamp();
    graphic.moveTo(currentPoint.x, currentPoint.y);
  }

  function clamp() {
    if (x2 <= currentPoint.x * xDir) {
      currentPoint.x = x2;
    }

    if (y2 <= currentPoint.y * yDir) {
      currentPoint.y = y2;
    }
  }
}

/**
 * @example
 *  polygons.push({x:-50, y:-50});
 *  polygons.push({x:-50, y:50});
 *  polygons.push({x:50, y:50});
 *  polygons.push({x:50, y:-50});
 * @param {*} instance
 * @param {*} polygons
 * @param {*} x
 * @param {*} y
 * @param {*} rotation
 * @param {*} dash
 * @param {*} gap
 * @param {*} offsetPercentage
 */
// https://codepen.io/unrealnl/pen/aYaxBW  issue https://github.com/pixijs/pixi.js/issues/1333
function drawDashedPolygon(instance, polygons, x, y, rotation, dash, gap, offsetPercentage) {
  var i;
  var p1;
  var p2;
  var dashLeft = 0;
  var gapLeft = 0;

  if (offsetPercentage > 0) {
    var progressOffset = (dash + gap) * offsetPercentage;

    if (progressOffset < dash) {
      dashLeft = dash - progressOffset;
    } else {
      gapLeft = gap - (progressOffset - dash);
    }
  }

  var rotatedPolygons = [];

  for (i = 0; i < polygons.length; i += 1) {
    var p = {
      x: polygons[i].x,
      y: polygons[i].y
    };
    var cosAngle = Math.cos(rotation);
    var sinAngle = Math.sin(rotation);
    var dx = p.x;
    var dy = p.y;
    p.x = dx * cosAngle - dy * sinAngle;
    p.y = dx * sinAngle + dy * cosAngle;
    rotatedPolygons.push(p);
  }

  for (i = 0; i < rotatedPolygons.length; i += 1) {
    p1 = rotatedPolygons[i];

    if (i === rotatedPolygons.length - 1) {
      p2 = rotatedPolygons[0];
    } else {
      p2 = rotatedPolygons[i + 1];
    }

    var _dx = p2.x - p1.x;

    var _dy = p2.y - p1.y;

    var len = Math.sqrt(_dx * _dx + _dy * _dy);
    var normal = {
      x: _dx / len,
      y: _dy / len
    };
    var progressOnLine = 0;
    instance.moveTo(x + p1.x + gapLeft * normal.x, y + p1.y + gapLeft * normal.y);

    while (progressOnLine <= len) {
      progressOnLine += gapLeft;

      if (dashLeft > 0) {
        progressOnLine += dashLeft;
      } else {
        progressOnLine += dash;
      }

      if (progressOnLine > len) {
        dashLeft = progressOnLine - len;
        progressOnLine = len;
      } else {
        dashLeft = 0;
      }

      instance.lineTo(x + p1.x + progressOnLine * normal.x, y + p1.y + progressOnLine * normal.y);
      progressOnLine += gap;

      if (progressOnLine > len && dashLeft === 0) {
        gapLeft = progressOnLine - len;
      } else {
        gapLeft = 0;
        instance.moveTo(x + p1.x + progressOnLine * normal.x, y + p1.y + progressOnLine * normal.y);
      }
    }
  }
}