/**
 * <b><code>charts</code></b>
 * <p/>
 * @Description: 图表charts的公共方法
 * <p/>
 * @Creation Time: 2021/06/07 16:58.
 *
 * @Author: tangjieping
 * @Since: nile-cmgd-lc-fe 0.1.0
 */

/**
 * ts-vue-echarts 的x、y轴等字体样式
 */
export const labelStyle = {
  fontSize: 17,
  color: '#fff',
  fontFamily: 'alibabaPuHuiTiRegular'
}

/**
 * ts-vue-echarts 的tooltip字体样式
 */
export const tooltipStyle = {
  backgroundColor: 'rgba(0,0,0,0.5)',
  borderWidth: 0,
  textStyle: {
    ...labelStyle
  }
}
/**
 * ts-vue-echarts 的横向滚动条
 */
export const horizontalDataZoom = [{
  type: 'slider',
  show: false,
  start: 0,
  end: 100,
  height: 8,
  bottom: '0%',
  zoomLock: true,
  showDetail: false,
  handleSize: 0
}, {
  type: 'inside',
  borderColor: 'transparent',
  zoomOnMouseWheel: false,
  moveOnMouseMove: true,
  moveOnMouseWheel: true
}]

/**
 * ts-vue-echarts 的纵向滚动条
 */
export const verticalDataZoom = [{
  type: 'slider',
  show: false,
  start: 0,
  end: 100,
  width: 8,
  right: '0%',
  orient: 'vertical',
  zoomLock: true,
  showDetail: false,
  handleSize: 0
}, {
  type: 'inside',
  borderColor: 'transparent',
  zoomOnMouseWheel: false,
  moveOnMouseMove: true,
  moveOnMouseWheel: true
}]

export const setDataZoom = (option, total, size = 15) => {
  if (total > size) {
    option.dataZoom[0].show = true
    option.dataZoom[0].start = 100 - (size / total * 100)
  } else {
    option.dataZoom[0].show = false
    option.dataZoom[0].start = 0
  }
}

// 设置渐变色
export const linearGradientStyle = (location, colorStart, colorEnd) => {
  return {
    type: 'linear',
    x: location[0],
    y: location[1],
    x2: location[2],
    y2: location[3],
    colorStops: [{
      offset: 0,
      color: colorStart
    }, {
      offset: 1,
      color: colorEnd
    }],
    globalCoord: false
  }
}

// util.js
// 射线判断函数
export function rayCasting (p, poly) {
  let px = p[0]
  let py = p[1]
  let flag = false

  for (let i = 0, l = poly.length, j = l - 1; i < l; j = i, i++) {
    let sx = poly[i][0]
    let sy = poly[i][1]
    let tx = poly[j][0]
    let ty = poly[j][1]

    // 点与多边形顶点重合
    if ((sx === px && sy === py) || (tx === px && ty === py)) {
      return true
    }

    // 点的射线和多边形的一条边重合，并且点在边上
    if ((sy === ty && sy === py) && ((sx > px && tx < px) || (sx < px && tx > px))) {
      return true
    }

    // 判断线段两端点是否在射线两侧
    if ((sy < py && ty >= py) || (sy >= py && ty < py)) {
      // 线段上与射线 Y 坐标相同的点的 X 坐标
      let x = sx + (py - sy) * (tx - sx) / (ty - sy)

      // 点在多边形的边上
      if (x === px) {
        return true
      }

      // 射线穿过多边形的边界
      if (x > px) {
        flag = !flag
      }
    }
  }

  // 射线穿过多边形边界的次数为奇数时点在多边形内
  return !!flag
}

// 判断点有没有在某个行政区
export function isPointInAreas (p, mapJson) {
  const areas = mapJson.features
  let flag = false
  for (let i = 0; i < areas.length; i++) {
    if (rayCasting(p, areas[i].geometry.coordinates[0])) {
      flag = true
      break
    }
  }
  return flag
}

// 获取元素在页面中的绝对位置
export function getElementFixed (element) {
  var actualLeft = element.offsetLeft
  var actualTop = element.offsetTop
  var current = element.offsetParent
  while (current !== null) {
    actualLeft += (current.offsetLeft)
    actualTop += (current.offsetTop)
    current = current.offsetParent
  }
  return {
    currentX: actualLeft,
    currentY: actualTop
  }
}
