'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createSupplementaryPoints = exports.createSupplementaryMultiPoints = undefined;

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _constants = require('../constants');

var _constants2 = _interopRequireDefault(_constants);

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

// 创建Vertex节点对象
var createVertex = function createVertex(context, pid, coordinates, path, selected) {
  var vertex = context.newFeature({
    geometry: {
      type: _constants2['default'].geojsonTypes.POINT,
      coordinates: coordinates
    }
  });
  vertex.updateInternalProperty('meta', _constants2['default'].meta.VERTEX);
  vertex.updateInternalProperty('active', _constants2['default'].activeStates.ACTIVE);
  vertex.updateInternalProperty('pid', pid);
  vertex.updateInternalProperty('path', path);
  vertex.updateInternalProperty('selected', selected);
  return vertex;
};

// 创建MidPoint中点对象
/**
 * @文件说明: 定义选中要素扩展节点与线段中心点的函数
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-06-03 16:43:45
 */

var createMidpoint = function createMidpoint(context, pid, startVertex, endVertex) {
  var iMapApi = context.ctx.api.iMapApi;
  var startCoord = startVertex.coordinates;
  var endCoord = endVertex.coordinates;
  // 计算两点之间的中心点
  var ptStart = iMapApi.project([startCoord[0], startCoord[1]]);
  var ptEnd = iMapApi.project([endCoord[0], endCoord[1]]);
  var midCoordinates = iMapApi.unproject([(ptStart.x + ptEnd.x) / 2, (ptStart.y + ptEnd.y) / 2]).toArray();

  var midpoint = context.newFeature({
    geometry: {
      type: _constants2['default'].geojsonTypes.POINT,
      coordinates: midCoordinates
    }
  });
  midpoint.updateInternalProperty('meta', _constants2['default'].meta.MIDPOINT);
  midpoint.updateInternalProperty('active', _constants2['default'].activeStates.ACTIVE);
  midpoint.updateInternalProperty('pid', pid);
  midpoint.updateInternalProperty('path', endVertex.properties['draw:path']);
  return midpoint;
};

// 创建MultiVertex节点对象
var createMultiVertex = function createMultiVertex(context, pid) {
  var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '0';

  var vertex = context.newFeature({
    geometry: {
      type: _constants2['default'].geojsonTypes.MULTI_POINT,
      coordinates: []
    }
  });
  vertex.updateInternalProperty('meta', _constants2['default'].meta.VERTEX);
  vertex.updateInternalProperty('active', _constants2['default'].activeStates.ACTIVE);
  vertex.updateInternalProperty('pid', pid);
  path !== null && path !== undefined && vertex.updateInternalProperty('path', path);
  return vertex;
};

// 生成绘制要素的单一复合节点
var createSupplementaryMultiPoints = exports.createSupplementaryMultiPoints = function createSupplementaryMultiPoints(context, feature) {
  var basePath = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  var type = feature.type,
      coordinates = feature.coordinates,
      features = feature.features,
      properties = feature.properties;

  var featureId = feature && feature.id;
  var supplementaryPoints = [];

  // 根据线段生成节点对象
  var processLine = function processLine(line) {
    // 记录首个节点，排除Polygon的闭合节点
    var firstPointString = '';
    var points = [];
    // 循环线段的坐标创建节点
    line.map(function (point, idx) {
      // 判断当前节点坐标是否与首个节点坐标相同，则忽略
      var stringifiedPoint = JSON.stringify(point);
      if (firstPointString !== stringifiedPoint) {
        points.push(point);
      }
      idx === 0 && (firstPointString = stringifiedPoint);
    });
    return points;
  };

  // 判断是否为线要素类型
  if (type === _constants2['default'].geojsonTypes.LINE_STRING) {
    var vertex = createMultiVertex(context, featureId, basePath || null);
    vertex.setCoordinates(processLine(coordinates));
    supplementaryPoints.push(vertex);
  } else if (type === _constants2['default'].geojsonTypes.POLYGON) {
    // 判定是否为面要素类型
    var _vertex = createMultiVertex(context, featureId, basePath ? basePath + '.0' : '0');
    var points = [];
    coordinates.map(function (line) {
      // 判定多边形面的所属分类，根据不同特定分类进行节点筛选
      switch (properties['draw:polygon']) {
        // 圆形
        case 'circle':
          {
            // 添加半径点
            var index = Math.floor(line.length * 0.75);
            var radius = line[index] || line[0];
            radius && points.push(radius);
            break;
          }
        // 椭圆
        case 'ellipse':
          {
            // 添加长轴半径点
            line[0] && points.push(line[0]);
            var _index = Math.floor(line.length * 0.5);
            line[_index] && points.push(line[_index]);
            break;
          }
        default:
          {
            points.push.apply(points, (0, _toConsumableArray3['default'])(processLine(line)));
            break;
          }
      }
    });
    properties['draw:polygon'] && _vertex.updateInternalProperty('polygon', properties['draw:polygon']);
    _vertex.setCoordinates(points);
    supplementaryPoints.push(_vertex);
  } else if (type.indexOf(_constants2['default'].geojsonTypes.MULTI_PREFIX) === 0) {
    // 判定是否为复合要素类型
    features.map(function (subFeature, index) {
      subFeature.id = featureId;
      supplementaryPoints = supplementaryPoints.concat(createSupplementaryMultiPoints(context, subFeature, String(index)));
    });
  }

  return supplementaryPoints;
};

// 生成绘制要素的节点
var createSupplementaryPoints = exports.createSupplementaryPoints = function createSupplementaryPoints(context, feature) {
  var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var basePath = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
  var type = feature.type,
      coordinates = feature.coordinates,
      features = feature.features,
      properties = feature.properties;

  var featureId = feature && feature.id;
  var supplementaryPoints = [];

  // 判定是否为选中的节点路径
  var isSelectedPath = function isSelectedPath(pid, path) {
    if (!options.selectedPaths) {
      return false;
    }
    var selected = false;
    for (var i = 0, len = options.selectedPaths.length; i < len; i++) {
      if (options.selectedPaths[i].feature_id === pid && options.selectedPaths[i].coord_path === path) {
        selected = true;
        break;
      }
    }
    return selected;
  };
  // 根据线段生成节点对象
  var processLine = function processLine(line, lineBasePath) {
    // 记录首个节点，排除Polygon的闭合节点
    var firstPointString = '';
    var lastVertex = null;
    // 循环线段的坐标创建节点
    line.map(function (point, idx) {
      var pointPath = lineBasePath !== undefined && lineBasePath !== null ? lineBasePath + '.' + idx : String(idx);
      var vertex = createVertex(context, featureId, point, pointPath, isSelectedPath(featureId, pointPath));
      properties['draw:polygon'] && vertex.updateInternalProperty('polygon', properties['draw:polygon']);
      // 判定是否需创建中点
      if (options.midpoint && lastVertex) {
        var midpoint = createMidpoint(context, featureId, lastVertex, vertex);
        midpoint && supplementaryPoints.push(midpoint);
      }
      lastVertex = vertex;

      // 判断当前节点坐标是否与首个节点坐标相同，则忽略
      var stringifiedPoint = JSON.stringify(point);
      if (firstPointString !== stringifiedPoint) {
        supplementaryPoints.push(vertex);
      }
      idx === 0 && (firstPointString = stringifiedPoint);
    });
  };

  // 判断是否为线要素类型
  if (type === _constants2['default'].geojsonTypes.LINE_STRING) {
    processLine(coordinates, basePath);
  } else if (type === _constants2['default'].geojsonTypes.POLYGON) {
    coordinates.map(function (line, lineIndex) {
      // 判定多边形面的所属分类，根据不同特定分类进行节点筛选
      switch (properties['draw:polygon']) {
        // 圆形
        case 'circle':
          {
            // 添加半径点
            var index = Math.floor(line.length * 0.75);
            var radius = line[index] || line[0];
            if (radius) {
              var pointPath = basePath !== null ? basePath + '.0.' + index : '0.' + index;
              var vertex = createVertex(context, featureId, radius, pointPath, isSelectedPath(featureId, pointPath));
              vertex.updateInternalProperty('polygon', properties['draw:polygon']);
              supplementaryPoints.push(vertex);
            }
            break;
          }
        // 椭圆
        case 'ellipse':
          {
            // 添加长轴半径点
            if (line[0]) {
              var _pointPath = basePath !== null ? basePath + '.0.0' : '0.0';
              var _vertex2 = createVertex(context, featureId, line[0], _pointPath, isSelectedPath(featureId, _pointPath));
              _vertex2.updateInternalProperty('polygon', properties['draw:polygon']);
              supplementaryPoints.push(_vertex2);
            }
            var _index2 = Math.floor(line.length * 0.5);
            if (line[_index2]) {
              var _pointPath2 = basePath !== null ? basePath + '.0.' + _index2 : '0.' + _index2;
              var _vertex3 = createVertex(context, featureId, line[_index2], _pointPath2, isSelectedPath(featureId, _pointPath2));
              _vertex3.updateInternalProperty('polygon', properties['draw:polygon']);
              supplementaryPoints.push(_vertex3);
            }
            break;
          }
        default:
          {
            processLine([].concat((0, _toConsumableArray3['default'])(line), [line[0]]), basePath !== null ? basePath + '.' + lineIndex : String(lineIndex));
            break;
          }
      }
    });
  } else if (type.indexOf(_constants2['default'].geojsonTypes.MULTI_PREFIX) === 0) {
    features.map(function (subFeature, index) {
      subFeature.id = featureId;
      supplementaryPoints = supplementaryPoints.concat(createSupplementaryPoints(context, subFeature, options, index));
    });
  }

  return supplementaryPoints;
};