import { HoverDieOpacity } from '../types';
/**
 * Responsible for drawing the dies inside the wafer map, adding dieText and scaling the canvas
 */
export class RenderingModule {
    constructor(wafermap) {
        this.wafermap = wafermap;
        this.minDieDim = 50;
    }
    updateSortedDiesAndDrawWafer() {
        this.dies = this.wafermap.dataManager.diesRenderInfo.sort((a, b) => {
            if (a.fillStyle > b.fillStyle) {
                return 1;
            }
            if (b.fillStyle > a.fillStyle) {
                return -1;
            }
            return 0;
        });
        this.drawWafer();
    }
    drawWafer() {
        this.wafermap.canvasContext.save();
        this.clearCanvas();
        this.scaleCanvas();
        this.renderDies();
        this.renderText();
        this.wafermap.canvasContext.restore();
        this.renderHover();
    }
    renderHover() {
        if (this.wafermap.dataManager.dieDimensions === undefined
            || this.wafermap.transform === undefined) {
            return;
        }
        this.wafermap.hoverWidth = this.wafermap.dataManager.dieDimensions.width
            * this.wafermap.transform.k;
        this.wafermap.hoverHeight = this.wafermap.dataManager.dieDimensions.height
            * this.wafermap.transform.k;
        this.wafermap.hoverOpacity = this.wafermap.hoverDie === undefined
            ? HoverDieOpacity.hide
            : HoverDieOpacity.show;
        this.wafermap.hoverTransform = this.calculateHoverTransform();
    }
    calculateHoverTransform() {
        if (this.wafermap.hoverDie !== undefined) {
            const scaledX = this.wafermap.dataManager.horizontalScale(this.wafermap.hoverDie.x);
            if (scaledX === undefined) {
                return '';
            }
            const scaledY = this.wafermap.dataManager.verticalScale(this.wafermap.hoverDie.y);
            if (scaledY === undefined) {
                return '';
            }
            const transformedPoint = this.wafermap.transform.apply([
                scaledX + this.wafermap.dataManager.margin.left,
                scaledY + this.wafermap.dataManager.margin.top
            ]);
            return `translate(${transformedPoint[0]}, ${transformedPoint[1]})`;
        }
        return '';
    }
    renderDies() {
        const context = this.wafermap.canvasContext;
        const dieWidth = this.wafermap.dataManager.dieDimensions.width;
        const dieHeight = this.wafermap.dataManager.dieDimensions.height;
        const transformedCanvasMinPoint = this.wafermap.transform.invert([
            0, 0
        ]);
        const transformedCanvasMaxPoint = this.wafermap.transform.invert([
            this.wafermap.canvas.width,
            this.wafermap.canvas.height
        ]);
        transformedCanvasMinPoint[0] -= dieWidth;
        transformedCanvasMinPoint[1] -= dieHeight;
        for (const die of this.dies) {
            if (this.isDieVisible(die, transformedCanvasMinPoint, transformedCanvasMaxPoint)) {
                context.fillStyle = die.fillStyle;
                context.fillRect(die.x, die.y, dieWidth, dieHeight);
            }
        }
    }
    renderText() {
        if (this.wafermap.dieLabelsHidden) {
            return;
        }
        const dieWidth = this.wafermap.dataManager.dieDimensions.width;
        const dieHeight = this.wafermap.dataManager.dieDimensions.height;
        const dieSize = dieWidth * dieHeight * (this.wafermap.transform.k !== 0 ? this.wafermap.transform.k : 1);
        if (dieSize >= this.minDieDim) {
            const fontsize = this.wafermap.dataManager.labelsFontSize;
            const context = this.wafermap.canvasContext;
            context.font = `${fontsize.toString()}px sans-serif`;
            context.fillStyle = '#ffffff';
            context.textAlign = 'center';
            context.lineCap = 'butt';
            const approximateTextHeight = context.measureText('M');
            const transformedCanvasMinPoint = this.wafermap.transform.invert([
                0, 0
            ]);
            const transformedCanvasMaxPoint = this.wafermap.transform.invert([
                this.wafermap.canvas.width,
                this.wafermap.canvas.height
            ]);
            transformedCanvasMinPoint[0] -= dieWidth;
            transformedCanvasMinPoint[1] -= dieHeight;
            for (const die of this.dies) {
                if (this.isDieVisible(die, transformedCanvasMinPoint, transformedCanvasMaxPoint)) {
                    context.fillText(die.text, die.x + dieWidth / 2, die.y + dieHeight / 2 + approximateTextHeight.width / 2, dieWidth - (dieWidth / 100) * 20);
                }
            }
        }
    }
    clearCanvas() {
        this.wafermap.canvasContext.clearRect(0, 0, this.wafermap.canvas.width, this.wafermap.canvas.height);
    }
    scaleCanvas() {
        this.wafermap.canvasContext.translate(this.wafermap.transform.x, this.wafermap.transform.y);
        this.wafermap.canvasContext.scale(this.wafermap.transform.k, this.wafermap.transform.k);
    }
    isDieVisible(die, minPoint, maxPoint) {
        return (die.x >= minPoint[0]
            && die.x < maxPoint[0]
            && die.y >= minPoint[1]
            && die.y < maxPoint[1]);
    }
}
//# sourceMappingURL=rendering.js.map