
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/


/**
 * AUTO-GENERATED FILE. DO NOT MODIFY.
 */

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { __assign, __extends } from "tslib";
import * as zrUtil from 'tvrender/lib/core/util.js';
import Group from 'tvrender/lib/graphic/Group.js';
import Line from 'tvrender/lib/graphic/shape/Line.js';
import Text from 'tvrender/lib/graphic/Text.js';
import Rect from 'tvrender/lib/graphic/shape/Rect.js';
import requestAnimationFrame from 'tvrender/lib/animation/requestAnimationFrame.js';
import * as eventTool from 'tvrender/lib/core/event.js';
import { createSymbol } from '../../util/symbol.js';
import { getLeftTextX } from '../../util/axisLabel.js';
import { contrastColor, removeTransparency } from '../../util/color.js';
import ComponentView from '../../view/Component.js';
var deleteIcon = 'path://M896 224h-166.4L704 89.6c0-12.8-12.8-25.6-32-25.6H345.6c-12.8 0-32 12.8-32 25.6l-25.6 134.4H128c-19.2 0-32 12.8-32 32s12.8 32 32 32h76.8l19.2 640c0 19.2 12.8 32 32 32h512c19.2 0 32-12.8 32-32l19.2-640H896c19.2 0 32-12.8 32-32s-12.8-32-32-32zM371.2 128h281.6l19.2 96H358.4L371.2 128z m364.8 768h-448l-19.2-608h486.4l-19.2 608z';
var startIcon = 'path://M512 64c249.6 0 448 198.4 448 448s-198.4 448-448 448-448-198.4-448-448 198.4-448 448-448m0-64C230.4 0 0 230.4 0 512s230.4 512 512 512 512-230.4 512-512-230.4-512-512-512z M697.6 486.4L428.8 320c-19.2-12.8-44.8 0-44.8 25.6v332.8c0 25.6 25.6 38.4 44.8 25.6l262.4-166.4c25.6-12.8 25.6-38.4 6.4-51.2z';
var deleteWarpWidth = 26;
var edgeDistance = 50;
export var STATUS = ['停止-手动', '激活中', '触发', '停止-已触发', '停止-已过期'];
var AlarmView = /** @class */function (_super) {
  __extends(AlarmView, _super);
  function AlarmView() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.type = AlarmView.type;
    return _this;
    // private _onElClick(e: any, id: string): void {
    //     this.model.setSelectedId(id);
    // }
  }

  AlarmView.prototype.init = function (ecModel, api) {
    this.ecModel = ecModel;
    this.api = api;
    var zr = api.getZr();
    zr.on('mousemove', this._onMouseMove, this);
    zr.on('mouseup', this._onMouseUp, this);
    zr.on('click', this._onMouseUp, this);
    zr.on('mousedown', this._onMouseDown, this);
  };
  AlarmView.prototype.render = function (alarmModel, ecModel, api, payload) {
    this.model = alarmModel;
    // const isDataZoom = payload.type === 'datazoom';
    this._updateAlarms(alarmModel, ecModel, api, payload);
  };
  AlarmView.prototype.updateVisual = function (alarmModel, ecModel, api, payload) {
    this._updateAlarms(alarmModel, ecModel, api, payload);
  };
  // updateView(alarmModel: AlarmModel, ecModel: GlobalModel, api: ExtensionAPI, payload: Payload) {
  //     console.log('22222')
  //     this._updateAlarms(alarmModel, ecModel, api, payload);
  // }
  AlarmView.prototype._updateAlarms = function (alarmModel, ecModel, api, payload) {
    var alarms = alarmModel.get('alarms');
    var seriesName = alarmModel.get('seriesName');
    var group = this.group;
    group.removeAll();
    this.elMap = zrUtil.createHashMap();
    this.alarmMap = zrUtil.createHashMap();
    var seriesModel = ecModel.getSeriesByName(seriesName)[0];
    if (!seriesModel) {
      return;
    }
    var coordSys = seriesModel.coordinateSystem;
    if (!coordSys || !coordSys.master) {
      return;
    }
    var gridRect = coordSys.master.getRect();
    var yAxisModel = coordSys.getAxis('y');
    var that = this;
    zrUtil.each(alarms, function (alarm) {
      var type = alarm.type,
        value = alarm.value;
      that.alarmMap.set(alarm.id, alarm);
      if (type === 'range') {
        zrUtil.each(value, function (num, index) {
          var id = index === 0 ? alarm.id : alarm.id + ":" + index;
          var el = that.createAlarmGroupEl(num, id, alarm, gridRect, yAxisModel);
          group.add(el);
        });
      } else {
        var el = that.createAlarmGroupEl(value, alarm.id, alarm, gridRect, yAxisModel);
        group.add(el);
      }
    });
  };
  // updateLayout: updateController,
  // updateVisual: updateController,
  AlarmView.prototype.createAlarmGroupEl = function (value, id, alarm, gridRect, yAxisModel) {
    var _this = this;
    var selected = id === this.mouseOverId || id === this.model.get('selectedId');
    var y = Math.floor(yAxisModel.toGlobalCoord(yAxisModel.dataToCoord(value))) + 0.5;
    var groupEl = new Group({
      y: y,
      // onmousedown: (e) => this._onElMouseDown(e, id),
      onmouseover: function (e) {
        return _this._onElMouseOver(e, id);
      },
      onmouseout: function (e) {
        return _this._onElMouseOut(e, id);
      }
    });
    groupEl.on('mousedown', function (e) {
      return _this._onElMouseDown(e, id);
    });
    var precision = this.model.get('precision');
    var alarmModel = this.model;
    var alarmStyle = alarmModel.get('alarmStyle');
    var zlevel = alarmModel.get('zlevel');
    var showLine = alarmModel.get('showLine');
    var optionZ = alarmModel.get('z');
    var z = selected ? optionZ + 10 : optionZ;
    var offset = yAxisModel.model.get('offset') || 0;
    var isRight = yAxisModel.position === 'right';
    var x = isRight ? gridRect.x + gridRect.width + offset : gridRect.x - offset;
    var isActive = alarm.status === 2;
    var color = isActive ? alarmStyle.color : alarmStyle.disabledColor;
    var edgeX = isRight ? gridRect.x + gridRect.width - edgeDistance : gridRect.x + edgeDistance;
    if (showLine || selected) {
      var x1 = gridRect.x;
      var x2 = gridRect.x + gridRect.width;
      if (!showLine) {
        x1 = isRight ? edgeX : gridRect.x;
        x2 = isRight ? gridRect.x + gridRect.width : edgeX;
      }
      var lineEl = new Line({
        shape: {
          x1: x1,
          y1: 0,
          x2: x2,
          y2: 0
        },
        style: {
          lineWidth: 1,
          stroke: color,
          lineDash: [5, 5]
        },
        zlevel: zlevel,
        z: z,
        cursor: 'ns-resize'
      });
      groupEl.add(lineEl);
    }
    var backgroundColor = alarmStyle.backgroundColor;
    if (selected) {
      var textX = showLine ? gridRect.x + gridRect.width / 2 : edgeX;
      var maxWidth = Math.floor(gridRect.width * 0.8);
      var textEl = new Text({
        style: {
          text: isActive ? alarm.text : alarm.text + " \u00B7 " + STATUS[alarm.status],
          padding: [4, deleteWarpWidth + 8, 4, 8],
          fill: color,
          backgroundColor: backgroundColor,
          borderColor: color,
          borderWidth: 1,
          borderRadius: 4,
          align: showLine ? 'center' : isRight ? 'right' : 'left',
          verticalAlign: 'middle'
        },
        cursor: 'ns-resize',
        zlevel: zlevel,
        z: z,
        x: textX
      });
      var textRect = textEl.getBoundingRect();
      var textWidth = textRect.width;
      if (textRect.width > maxWidth) {
        textWidth = maxWidth + deleteWarpWidth + 16;
        textEl.useStyle(__assign(__assign({}, textEl.style), {
          width: maxWidth,
          overflow: 'truncate'
        }));
      }
      // console.log('%c [ textRect ]-173', 'font-size:13px; background:pink; color:#bf2c9f;', textRect)
      var deleteX = textX + textWidth / 2 - deleteWarpWidth - 1;
      if (!showLine) {
        deleteX = textX + (isRight ? 0 : textWidth) - deleteWarpWidth - 1;
      }
      var deleteWarpEl_1 = new Rect({
        shape: {
          r: 4,
          x: deleteX,
          y: -10,
          width: deleteWarpWidth,
          height: 20
        },
        style: {
          fill: this.selectIconId === id ? alarmStyle.splitColor : backgroundColor
        },
        z: z + 1,
        zlevel: zlevel
      });
      var deleteGroup = new Group();
      var splitLineEl = new Line({
        // x: deleteX,
        shape: {
          x1: deleteX,
          y1: 10,
          x2: deleteX,
          y2: -10
        },
        style: {
          stroke: alarmStyle.splitColor
        },
        z: z + 1,
        zlevel: zlevel
      });
      var deleteEl = createSymbol(isActive ? deleteIcon : startIcon, deleteX + 6, -6, 12, 12, isActive ? alarmStyle.color : alarmStyle.disabledIconColor);
      deleteEl.zlevel = zlevel;
      deleteEl.z = z + 1;
      deleteGroup.add(deleteWarpEl_1);
      deleteGroup.add(deleteEl);
      deleteGroup.add(splitLineEl);
      deleteGroup.on('mouseover', function (e) {
        deleteWarpEl_1.style.fill = alarmStyle.splitColor;
        _this.selectIconId = id;
      });
      deleteGroup.on('mouseout', function (e) {
        deleteWarpEl_1.style.fill = backgroundColor;
        _this.selectIconId = '';
      });
      deleteGroup.on('mousedown', function (e) {
        return _this._onDeleteMouseDown(e, id);
      });
      deleteGroup.on('click', function (e) {
        _this.api.trigger('alarmdelete', alarm);
      });
      var labelStyle = {
        padding: [4, 6, 2, 8],
        fill: contrastColor(color),
        backgroundColor: removeTransparency(color)
      };
      var labelText = (+value).toFixed(precision);
      var labelX = isRight ? x : getLeftTextX(x, labelText, labelStyle, this.ecModel);
      var labelEl = new Text({
        style: __assign(__assign({}, labelStyle), {
          text: labelText
        }),
        x: labelX,
        y: -10,
        zlevel: zlevel,
        z: z,
        name: 'label'
      });
      labelEl.on('mousedown', function (e) {
        e.cancelBubble = true;
        eventTool.stop(e.event);
        var selectId = _this.model.get('selectedId');
        if (selectId && selectId !== id) {
          _this._reCreate(selectId);
        }
        _this.model.setSelectedId(id);
      });
      groupEl.add(textEl);
      groupEl.add(labelEl);
      groupEl.add(deleteGroup);
    } else {
      var arrowX = isRight ? x + 8 : x - 8;
      var arrow = createSymbol(isRight ? 'triangleRight' : 'triangleLeft', arrowX, 0, 7, 11, color);
      arrow.zlevel = zlevel;
      arrow.z = z + 10;
      groupEl.add(arrow);
    }
    this.elMap.set(id, groupEl);
    return groupEl;
  };
  AlarmView.prototype.dispose = function () {
    var zr = this.api.getZr();
    zr.off('mousemove', this._onMouseMove);
    zr.off('mouseup', this._onMouseUp);
    zr.off('click', this._onMouseUp);
    zr.off('mousedown', this._onMouseDown);
    this.ecModel = null;
    this.api = null;
    this.group.removeAll();
  };
  AlarmView.prototype._reCreate = function (id) {
    var alarmId = getFormatId(id)[0];
    var group = this.group;
    var alarm = this.alarmMap.get(alarmId);
    if (!alarm) {
      return;
    }
    var type = alarm.type,
      value = alarm.value;
    var seriesName = this.model.get('seriesName');
    var seriesModel = this.ecModel.getSeriesByName(seriesName)[0];
    if (!seriesModel) {
      return;
    }
    var coordSys = seriesModel.coordinateSystem;
    var gridRect = coordSys.master.getRect();
    var yAxisModel = coordSys.getAxis('y');
    var that = this;
    if (type === 'range') {
      zrUtil.each(value, function (num, index) {
        var id = index === 0 ? alarm.id : alarm.id + ":" + index;
        var oldEl = that.elMap.get(id);
        group.remove(oldEl);
        var el = that.createAlarmGroupEl(num, id, alarm, gridRect, yAxisModel);
        group.add(el);
      });
    } else {
      var oldEl = that.elMap.get(id);
      group.remove(oldEl);
      var el = that.createAlarmGroupEl(value, alarm.id, alarm, gridRect, yAxisModel);
      group.add(el);
    }
  };
  AlarmView.prototype._onMouseMove = function (e) {
    var _this = this;
    if (!this.dragAlarmId) {
      return;
    }
    requestAnimationFrame(function () {
      var seriesName = _this.model.get('seriesName');
      var precision = _this.model.get('precision');
      var seriesModel = _this.ecModel.getSeriesByName(seriesName)[0];
      if (!seriesModel) {
        return;
      }
      var _a = getFormatId(_this.dragAlarmId),
        alarmId = _a[0],
        index = _a[1];
      var alarm = _this.alarmMap.get(alarmId);
      var el = _this.elMap.get(_this.dragAlarmId);
      if (!alarm || !el) {
        return;
      }
      var coordSys = seriesModel.coordinateSystem;
      var yAxisModel = coordSys.getAxis('y');
      var price = yAxisModel.coordToData(yAxisModel.toLocalCoord(e.offsetY));
      var formatPrice = price.toFixed(precision);
      if (alarm.type === 'range') {
        var valueIndex = index ? +index : 0;
        if (valueIndex === 0 && alarm.min && price < alarm.min) {
          return;
        } else if (alarm.max && price > alarm.max) {
          return;
        }
        alarm.value[valueIndex] = +formatPrice;
      } else {
        alarm.value = +formatPrice;
      }
      var labelEl = el.childOfName('label');
      if (!labelEl) {
        return;
      }
      labelEl.attr('style', {
        text: formatPrice
      });
      el.attr('y', Math.floor(e.offsetY) + 0.5);
      _this.isMove = true;
    });
  };
  AlarmView.prototype._onMouseUp = function () {
    if (this.dragAlarmId) {
      var id = getFormatId(this.dragAlarmId)[0];
      var alarm = this.alarmMap.get(id);
      this.dragAlarmId = '';
      if (this.isMove) {
        this.api.trigger('alarmchanged', {
          alarm: alarm
        });
        this.isMove = false;
      }
    }
  };
  AlarmView.prototype._onMouseDown = function (e) {
    // if (this.downStop) {
    //     this.downStop = false;
    //     return;
    // }
    var selectId = this.model.get('selectedId');
    if (selectId) {
      if (this.mouseOverId) {
        this.mouseOverId = '';
      }
      this.model.setSelectedId('');
      this._reCreate(selectId);
    }
  };
  AlarmView.prototype._onElMouseDown = function (e, id) {
    e.cancelBubble = true;
    // e.event.cancelBubble = true;
    eventTool.stop(e.event);
    this.dragAlarmId = id;
    var selectId = this.model.get('selectedId');
    if (selectId && selectId !== id || !this.mouseOverId) {
      !this.mouseOverId && (this.mouseOverId = id);
      this._reCreate(selectId || id);
    }
    this.model.setSelectedId(id);
    // this.downStop = true;
  };

  AlarmView.prototype._onElMouseOver = function (e, id) {
    var selectedId = this.model.get('selectedId');
    if (selectedId === id || this.dragAlarmId) {
      return;
    }
    this.mouseOverId = id;
    this._reCreate(id);
  };
  AlarmView.prototype._onElMouseOut = function (e, id) {
    if (this.mouseOverId) {
      this.mouseOverId = '';
    }
    var selectedId = this.model.get('selectedId');
    if (selectedId === id || this.dragAlarmId) {
      return;
    }
    this._reCreate(id);
  };
  AlarmView.prototype._onDeleteMouseDown = function (e, id) {
    e.cancelBubble = true;
  };
  AlarmView.type = 'alarm';
  return AlarmView;
}(ComponentView);
function getFormatId(id) {
  return typeof id === 'string' ? id.split(':') : [id];
}
export default AlarmView;