/**
 * 定义GML方法提供类
 */
export default class GMLProvider {
  constructor() { }

  /**
   * 获取点GML格式坐标串
   * @param {Array} coordinates -坐标数组
   * @param {Boolean} exact -坐标是否精确
   *
   * @returns {String}
   */
  static getPointString(coordinates, exact=true) {
    return (
      exact ? coordinates[0] + ',' + coordinates[1] + ''
            : coordinates[0].toFixed(6) + ',' + coordinates[1].toFixed(6) + ''
    );
  }

  /**
   * 获取折线(环)GML格式坐标串
   * @param {Array} coordinates -坐标数组
   * @param {Boolean} exact -坐标是否精确
   *
   * @returns {String}
   */
  static getLineStringString(coordinates, exact=true) {
    let str = '';
    coordinates.forEach(cs => {
      str += exact ? cs[0] + ',' + cs[1] + ''
                   : cs[0].toFixed(6) + ',' + cs[1].toFixed(6) + '';
      str += ' ';
    });

    return str.trimEnd();
  }

  /**
   * 获取折线相交查询的筛选器
   * @param {String} coordsStr -GML坐标串
   * @param {Boolean} geomFld -geom字段名
   *
   * @returns {String}
   */
  static getLineStringIntersectsFilter(coordsStr, geomFieldName='geom') {
    let gml =
      '<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">' +
        '<Intersects>' +
	        '<PropertyName>' + geomFieldName + '</PropertyName>' +
	        '<gml:LineString>' +
            '<gml:coordinates>' + coordsStr + '</gml:coordinates>' +
          '</gml:LineString>' +
        '</Intersects>' +
      '</Filter>';

    return gml;
  }

  /**
   * 获取多边形外边界相交查询的筛选器
   * @param {String} coordsStr -GML坐标串
   * @param {Boolean} geomFld -geom字段名
   *
   * @returns {String}
   */
  static getPolygonOuterRingIntersectsFilter(coordsStr, geomFieldName='geom') {
    let gml =
      '<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">' +
        '<Intersects>' +
	        '<PropertyName>' + geomFieldName + '</PropertyName>' +
	        '<gml:Polygon>' +
            '<gml:outerBoundaryIs>' +
	            '<gml:LinearRing>' +
                '<gml:coordinates>' + coordsStr + '</gml:coordinates>' +
              '</gml:LinearRing>' +
            '</gml:outerBoundaryIs>' +
          '</gml:Polygon>' +
        '</Intersects>' +
      '</Filter>';

    return gml;
  }

  /**
   * 获取属性相等且折线相交查询的筛选器
   * @param {String} fieldName -字段名
   * @param {String} fieldValue -字段值
   * @param {String} coordsStr -GML坐标串
   * @param {Boolean} geomFieldName -geom字段名
   *
   * @returns {String}
   */
  static getPropertyEqualAndLineStringIntersectsFilter(fieldName, fieldValue, coordsStr, geomFieldName='geom') {
    let gml =
      '<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">' +
        '<And>' +
          '<PropertyIsEqualTo>' +
            '<PropertyName>' + fieldName + '</PropertyName>' +
            '<Literal>' + fieldValue + '</Literal>' +
          '</PropertyIsEqualTo>' +
          '<Intersects>' +
	          '<PropertyName>' + geomFieldName + '</PropertyName>' +
	          '<gml:LineString>' +
              '<gml:coordinates>' + coordsStr + '</gml:coordinates>' +
            '</gml:LineString>' +
          '</Intersects>' +
        '</And>' +
      '</Filter>';

    return gml;
  }

  /**
   * 获取属性相等且多边形外边界相交查询的筛选器
   * @param {String} fieldName -字段名
   * @param {String} fieldValue -字段值
   * @param {String} coordsStr -GML坐标串
   * @param {Boolean} geomFieldName -geom字段名
   *
   * @returns {String}
   */
  static getPropertyEqualAndLinearRingIntersectsFilter(fieldName, fieldValue, coordsStr, geomFieldName='geom') {
    let gml =
      '<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">' +
        '<And>' +
          '<PropertyIsEqualTo>' +
            '<PropertyName>' + fieldName + '</PropertyName>' +
            '<Literal>' + fieldValue + '</Literal>' +
          '</PropertyIsEqualTo>' +
          '<Intersects>' +
	          '<PropertyName>' + geomFieldName + '</PropertyName>' +
	          '<gml:Polygon>' +
              '<gml:outerBoundaryIs>' +
	              '<gml:LinearRing>' +
                  '<gml:coordinates>' + coordsStr + '</gml:coordinates>' +
                '</gml:LinearRing>' +
              '</gml:outerBoundaryIs>' +
            '</gml:Polygon>' +
          '</Intersects>' +
        '</And>' +
      '</Filter>';

    return gml;
  }
}
