"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArcAnnotationBase = void 0;
var Deleter_1 = require("../../../Core/Deleter");
var Point_1 = require("../../../Core/Point");
var pointUtil_1 = require("../../../utils/pointUtil");
var translate_1 = require("../../../utils/translate");
var BrushCache_1 = require("../../Drawing/BrushCache");
var Pen2DCache_1 = require("../../Drawing/Pen2DCache");
var labelHelper_1 = require("../Helpers/labelHelper");
var SciChartSurfaceBase_1 = require("../SciChartSurfaceBase");
var DpiHelper_1 = require("../TextureManager/DpiHelper");
var AnnotationBase_1 = require("./AnnotationBase");
var ECoordinateMode_1 = require("./types/ECoordinateMode");
var annotationHelpers_1 = require("./annotationHelpers");
var constants_1 = require("./constants");
var RenderContextAnnotationBase_1 = require("./RenderContextAnnotationBase");
var ArcAnnotationBase = /** @class */ (function (_super) {
    __extends(ArcAnnotationBase, _super);
    function ArcAnnotationBase(options) {
        var _this = this;
        var _a, _b, _c, _d, _e;
        _this = _super.call(this, options) || this;
        _this.dragPointsProperty = [
            AnnotationBase_1.EDraggingGripPoint.Body,
            AnnotationBase_1.EDraggingGripPoint.x1y1,
            AnnotationBase_1.EDraggingGripPoint.x2y2,
            AnnotationBase_1.EDraggingGripPoint.x3y3
        ];
        _this.arcAnnotationBorders = {
            x1: 0,
            x2: 0,
            y1: 0,
            y2: 0,
            xCenter: 0,
            yCenter: 0
        };
        _this.strokeThicknessProperty = 1;
        _this.strokeDashArrayProperty = [];
        _this.strokeProperty = "#FFFFFF";
        _this.isLineModeProperty = false;
        _this.strokeProperty = (_a = options === null || options === void 0 ? void 0 : options.stroke) !== null && _a !== void 0 ? _a : _this.strokeProperty;
        _this.strokeThicknessProperty = (_b = options === null || options === void 0 ? void 0 : options.strokeThickness) !== null && _b !== void 0 ? _b : _this.strokeThicknessProperty;
        _this.strokeDashArrayProperty = (_c = options === null || options === void 0 ? void 0 : options.strokeDashArray) !== null && _c !== void 0 ? _c : _this.strokeDashArrayProperty;
        _this.isLineModeProperty = (_d = options === null || options === void 0 ? void 0 : options.isLineMode) !== null && _d !== void 0 ? _d : _this.isLineModeProperty;
        _this.fillProperty = (_e = options === null || options === void 0 ? void 0 : options.fill) !== null && _e !== void 0 ? _e : _this.fillProperty;
        return _this;
    }
    Object.defineProperty(ArcAnnotationBase.prototype, "stroke", {
        get: function () {
            return this.strokeProperty;
        },
        set: function (htmlColorCode) {
            if (htmlColorCode !== this.strokeProperty) {
                this.strokeProperty = htmlColorCode;
                this.notifyPropertyChanged(constants_1.PROPERTY.STROKE);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(ArcAnnotationBase.prototype, "strokeThickness", {
        get: function () {
            return this.strokeThicknessProperty;
        },
        set: function (value) {
            if (value !== this.strokeThicknessProperty) {
                this.strokeThicknessProperty = value;
                this.notifyPropertyChanged(constants_1.PROPERTY.STROKE_THICKNESS);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(ArcAnnotationBase.prototype, "strokeDashArray", {
        get: function () {
            return this.strokeDashArrayProperty;
        },
        set: function (value) {
            if (value !== this.strokeDashArrayProperty) {
                this.strokeDashArrayProperty = value;
                this.notifyPropertyChanged(constants_1.PROPERTY.STROKE_DASH_ARRAY);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(ArcAnnotationBase.prototype, "isLineMode", {
        get: function () {
            return this.isLineModeProperty;
        },
        set: function (value) {
            if (value !== this.isLineModeProperty) {
                this.isLineModeProperty = value;
                this.notifyPropertyChanged(constants_1.PROPERTY.IS_LINE_MODE);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(ArcAnnotationBase.prototype, "fill", {
        get: function () {
            return this.fillProperty;
        },
        set: function (colorCode) {
            if (colorCode !== this.fillProperty) {
                this.fillProperty = colorCode;
                this.notifyPropertyChanged(constants_1.PROPERTY.FILL);
            }
        },
        enumerable: false,
        configurable: true
    });
    ArcAnnotationBase.prototype.delete = function () {
        this.strokePenCache = (0, Deleter_1.deleteSafe)(this.strokePenCache);
        this.fillBrushCache = (0, Deleter_1.deleteSafe)(this.fillBrushCache);
    };
    ArcAnnotationBase.prototype.onAttach = function (scs) {
        _super.prototype.onAttach.call(this, scs);
        if (!this.strokePenCache) {
            this.strokePenCache = new Pen2DCache_1.Pen2DCache(scs.webAssemblyContext2D);
        }
        (0, Pen2DCache_1.createPenInCache)(this.strokePenCache, this.stroke, this.strokeThickness > 0 ? this.strokeThickness + 1 : 0, this.opacity, this.strokeDashArray);
        if (!this.fillBrushCache) {
            this.fillBrushCache = new BrushCache_1.BrushCache(scs.webAssemblyContext2D);
        }
        (0, BrushCache_1.createBrushInCache)(this.fillBrushCache, this.fill, this.opacity);
    };
    ArcAnnotationBase.prototype.onDpiChanged = function (args) {
        _super.prototype.onDpiChanged.call(this, args);
        this.notifyPropertyChanged(constants_1.PROPERTY.STROKE);
    };
    ArcAnnotationBase.prototype.toJSON = function () {
        var json = _super.prototype.toJSON.call(this);
        var options = {
            stroke: this.stroke,
            strokeDashArray: this.strokeDashArray,
            strokeThickness: this.strokeThickness,
            isLineMode: this.isLineMode,
            fill: this.fill
        };
        Object.assign(json.options, options);
        return json;
    };
    ArcAnnotationBase.prototype.onDragStarted = function (args) {
        _super.prototype.onDragStarted.call(this, args);
        var _a = this.getArcAnnotationBorders(true), x1 = _a.x1, x2 = _a.x2, xCenter = _a.xCenter, y1 = _a.y1, y2 = _a.y2, yCenter = _a.yCenter;
        var canvasMouseX = args.mousePoint.x;
        var canvasMouseY = args.mousePoint.y;
        if (this.canDragPoint(AnnotationBase_1.EDraggingGripPoint.x1y1)) {
            var dist = (0, pointUtil_1.calcDistance)(x1, y1, canvasMouseX, canvasMouseY);
            if (dist < this.annotationsGripsRadius * DpiHelper_1.DpiHelper.PIXEL_RATIO) {
                this.adornerDraggingPoint = AnnotationBase_1.EDraggingGripPoint.x1y1;
                return true;
            }
        }
        if (this.canDragPoint(AnnotationBase_1.EDraggingGripPoint.x2y2)) {
            var dist = (0, pointUtil_1.calcDistance)(x2, y2, canvasMouseX, canvasMouseY);
            if (dist < this.annotationsGripsRadius * DpiHelper_1.DpiHelper.PIXEL_RATIO) {
                this.adornerDraggingPoint = AnnotationBase_1.EDraggingGripPoint.x2y2;
                return true;
            }
        }
        if (this.canDragPoint(AnnotationBase_1.EDraggingGripPoint.x3y3)) {
            var dist = (0, pointUtil_1.calcDistance)(xCenter, yCenter, canvasMouseX, canvasMouseY);
            if (dist < this.annotationsGripsRadius * DpiHelper_1.DpiHelper.PIXEL_RATIO) {
                this.adornerDraggingPoint = AnnotationBase_1.EDraggingGripPoint.x3y3;
                return true;
            }
        }
        if (this.canDragPoint(AnnotationBase_1.EDraggingGripPoint.Body)) {
            if (this.clickToSelect(args)) {
                this.adornerDraggingPoint = AnnotationBase_1.EDraggingGripPoint.Body;
                return true;
            }
        }
        return false;
    };
    ArcAnnotationBase.prototype.notifyPropertyChanged = function (propertyName) {
        _super.prototype.notifyPropertyChanged.call(this, propertyName);
        var strokePenRelatedProperties = [
            constants_1.PROPERTY.STROKE,
            constants_1.PROPERTY.STROKE_THICKNESS,
            constants_1.PROPERTY.STROKE_DASH_ARRAY,
            constants_1.PROPERTY.OPACITY
        ];
        if (strokePenRelatedProperties.includes(propertyName) && this.strokePenCache) {
            (0, Pen2DCache_1.createPenInCache)(this.strokePenCache, this.stroke, this.strokeThickness, this.opacity, this.strokeDashArray);
        }
        var fillBrushRelatedProperties = [constants_1.PROPERTY.FILL, constants_1.PROPERTY.OPACITY];
        if (fillBrushRelatedProperties.includes(propertyName) && this.fillBrushCache) {
            (0, BrushCache_1.createBrushInCache)(this.fillBrushCache, this.fill, this.opacity);
        }
    };
    ArcAnnotationBase.prototype.getBrush = function () {
        if (this.isLineMode) {
            return undefined;
        }
        else {
            var fillBrush = this.fill ? (0, BrushCache_1.getWebGlBrushFromCache)(this.fillBrushCache) : undefined;
            return fillBrush ? fillBrush.scrtBrush : undefined;
        }
    };
    ArcAnnotationBase.prototype.getDrawMode = function () {
        // 0 - draw mode for sectors, 1 - draw mode for arcs
        if (this.isLineMode) {
            // draw mode for thin arc lines with anti-aliasing, is used for grid lines
            return 1;
        }
        else {
            // standard draw mode, draw sectors and arcs, but does not have anti-aliasing there for has jagged edges
            return 0;
        }
    };
    ArcAnnotationBase.prototype.setArcAnnotationBorders = function (x1, x2, xCenter, y1, y2, yCenter) {
        this.arcAnnotationBorders = {
            x1: x1,
            x2: x2,
            xCenter: xCenter,
            y1: y1,
            y2: y2,
            yCenter: yCenter
        };
    };
    ArcAnnotationBase.prototype.getArcAnnotationBorders = function (translateToCanvas, multiplier) {
        if (translateToCanvas === void 0) { translateToCanvas = false; }
        if (multiplier === void 0) { multiplier = 1; }
        if (translateToCanvas) {
            var seriesViewRect = this.parentSurface.seriesViewRect;
            var _a = this.arcAnnotationBorders, x1 = _a.x1, x2 = _a.x2, xCenter = _a.xCenter, y1 = _a.y1, y2 = _a.y2, yCenter = _a.yCenter;
            var p1 = (0, translate_1.translateFromSeriesViewRectToCanvas)(new Point_1.Point(x1, y1), seriesViewRect, true);
            var p2 = (0, translate_1.translateFromSeriesViewRectToCanvas)(new Point_1.Point(x2, y2), seriesViewRect, true);
            var pCenter = (0, translate_1.translateFromSeriesViewRectToCanvas)(new Point_1.Point(xCenter, yCenter), seriesViewRect, true);
            return {
                x1: p1.x * multiplier,
                y1: p1.y * multiplier,
                x2: p2.x * multiplier,
                y2: p2.y * multiplier,
                xCenter: pCenter.x * multiplier,
                yCenter: pCenter.y * multiplier
            };
        }
        return this.arcAnnotationBorders;
    };
    ArcAnnotationBase.prototype.svgStringArcAdornerTemplate = function (x1, y1, x2, y2, xCenter, yCenter) {
        var colorLine = this.selectionBoxStroke;
        return "<svg xmlns=\"http://www.w3.org/2000/svg\">\n    <line x1=\"".concat(x1, "\" y1=\"").concat(y1, "\" x2=\"").concat(x2, "\" y2=\"").concat(y2, "\" stroke=\"").concat(colorLine, "\" stroke-width=\"6\" />\n    <line x1=\"").concat(x1, "\" y1=\"").concat(y1, "\" x2=\"").concat(xCenter, "\" y2=\"").concat(yCenter, "\" stroke=\"").concat(colorLine, "\" stroke-width=\"6\" />\n    <line x1=\"").concat(xCenter, "\" y1=\"").concat(yCenter, "\" x2=\"").concat(x2, "\" y2=\"").concat(y2, "\" stroke=\"").concat(colorLine, "\" stroke-width=\"6\" />\n    ").concat(this.canDragPoint(AnnotationBase_1.EDraggingGripPoint.x1y1) ? this.getAnnotationGripSvg(x1, y1) : "", "\n    ").concat(this.canDragPoint(AnnotationBase_1.EDraggingGripPoint.x2y2) ? this.getAnnotationGripSvg(x2, y2) : "", "\n    ").concat(this.getAnnotationGripSvg(xCenter, yCenter), "\n    </svg>");
    };
    ArcAnnotationBase.prototype.updateAdornerInner = function () {
        this.deleteAdorner();
        if (this.isSelected) {
            var _a = this.getArcAnnotationBorders(true, 1 / DpiHelper_1.DpiHelper.PIXEL_RATIO), x1 = _a.x1, x2 = _a.x2, y1 = _a.y1, y2 = _a.y2, xCenter = _a.xCenter, yCenter = _a.yCenter;
            var svgString = this.svgStringArcAdornerTemplate(x1, y1, x2, y2, xCenter, yCenter);
            this.svgAdorner = annotationHelpers_1.annotationHelpers.createSvg(svgString, this.svgAdornerRoot);
        }
    };
    ArcAnnotationBase.prototype.getViewportHeight = function () {
        return this.parentSurface.isCopyCanvasSurface
            ? SciChartSurfaceBase_1.SciChartSurfaceBase.domMasterCanvas.height
            : this.parentSurface.renderSurface.viewportSize.height;
    };
    ArcAnnotationBase.prototype.normalizeAnglesAndHitTest = function (startAngleInput, endAngleInput, testAngleInput) {
        var startAngle = labelHelper_1.labelHelper.normalizeAngle(startAngleInput);
        var endAngle = labelHelper_1.labelHelper.normalizeAngle(endAngleInput);
        if (startAngle > endAngle) {
            endAngle += 2 * Math.PI;
        }
        var isHit = false;
        if (testAngleInput) {
            var testAngle = labelHelper_1.labelHelper.normalizeAngle(testAngleInput);
            if (testAngle < startAngle) {
                testAngle += 2 * Math.PI;
            }
            if (startAngle <= testAngle && testAngle <= endAngle) {
                isHit = true;
            }
        }
        return { startAngle: startAngle, endAngle: endAngle, isHit: isHit };
    };
    ArcAnnotationBase.prototype.isDataValueMode = function () {
        return this.xCoordinateMode === ECoordinateMode_1.ECoordinateMode.DataValue;
    };
    return ArcAnnotationBase;
}(RenderContextAnnotationBase_1.RenderContextAnnotationBase));
exports.ArcAnnotationBase = ArcAnnotationBase;
