package ru.m.puzzlez.render.mask;

import flash.display.GraphicsPathCommand;
import ru.m.puzzlez.core.Part;
import ru.m.puzzlez.render.mask.IPartMaskBuilder;

class SquareMaskBuilder extends BaseMaskBuilder {

    override private function createBound(path:DrawPath, fromX:Float, fromY:Float, toX:Float, toY:Float, type:BoundType):Void {
        var dx = toX == fromX ? 0 : (toX - fromX) / Math.abs(toX - fromX);
        var dy = toY == fromY ? 0 : (toY - fromY) / Math.abs(toY - fromY);
        var boundLength = Math.max(Math.abs(toX - fromX), Math.abs(toY - fromY));
        var spikeWidth = boundLength * 0.2;
        var spikeOffset = (boundLength - spikeWidth) / 2;
        var spikeDepth = switch type {
            case NONE: 0;
            case IN: spikeWidth;
            case OUT: -spikeWidth;
        }
        switch type {
            case NONE:
            case IN | OUT:
                path.commands.push(GraphicsPathCommand.LINE_TO);
                path.data.push(fromX + spikeOffset * dx);
                path.data.push(fromY + spikeOffset * dy);
                path.commands.push(GraphicsPathCommand.LINE_TO);
                path.data.push(fromX + spikeOffset * dx - spikeDepth * dy);
                path.data.push(fromY + spikeDepth * dx + spikeOffset * dy);
                path.commands.push(GraphicsPathCommand.LINE_TO);
                path.data.push(fromX + (spikeOffset + spikeWidth) * dx - spikeDepth * dy);
                path.data.push(fromY + spikeDepth * dx  + (spikeOffset + spikeWidth) * dy);
                path.commands.push(GraphicsPathCommand.LINE_TO);
                path.data.push(fromX + (spikeOffset + spikeWidth) * dx);
                path.data.push(fromY + (spikeOffset + spikeWidth) * dy);
        }
        super.createBound(path, fromY, fromY, toX, toY, type);
    }
}
