import { Collider, Shape, Transform } from 'dacha';
import { Color } from 'pixi.js';
import { getCurrentZoom } from '../../../../utils/get-current-zoom';
const DEFAULT_COLLIDER_COLOR = '#4DFFB8';
const FILL_COLOR_ALPHA = 0.15;
const STROKE_WIDTH = 1.5;
const DEFAULT_PROPS = {
    strokeWidth: 1,
    strokeAlignment: 1,
    pixelLine: false,
    opacity: 1,
    blending: 'normal',
    disabled: false,
    sortingLayer: 'editor-layer-0',
    sortCenter: [0, 0],
};
const getCapsuleShape = (collider, transform, color, fillColor) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    const { centerX, centerY } = collider;
    const point1X = (_b = (_a = collider.point1) === null || _a === void 0 ? void 0 : _a.x) !== null && _b !== void 0 ? _b : 0;
    const point1Y = (_d = (_c = collider.point1) === null || _c === void 0 ? void 0 : _c.y) !== null && _d !== void 0 ? _d : 0;
    const point2X = (_f = (_e = collider.point2) === null || _e === void 0 ? void 0 : _e.x) !== null && _f !== void 0 ? _f : 0;
    const point2Y = (_h = (_g = collider.point2) === null || _g === void 0 ? void 0 : _g.y) !== null && _h !== void 0 ? _h : 0;
    const dx = point2X - point1X;
    const dy = point2Y - point1Y;
    transform.local.rotation = Math.atan2(dy, dx);
    transform.local.position.x = centerX + (point1X + point2X) / 2;
    transform.local.position.y = centerY + (point1Y + point2Y) / 2;
    const length = Math.hypot(dx, dy);
    const radius = (_j = collider.radius) !== null && _j !== void 0 ? _j : 0;
    return new Shape(Object.assign({ type: 'roundRectangle', width: length + radius * 2, height: radius * 2, radius, strokeColor: color, fill: fillColor }, DEFAULT_PROPS));
};
export const colliderViewer = {
    id: 'collider-viewer',
    title: 'Colliders',
    validate: (actor) => Boolean(actor.getComponent(Collider)),
    build: (actor, options) => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
        const { actorSpawner } = options;
        const collider = actor.getComponent(Collider);
        const color = (_a = collider.debugColor) !== null && _a !== void 0 ? _a : DEFAULT_COLLIDER_COLOR;
        const fillColor = new Color(color).setAlpha(FILL_COLOR_ALPHA).toHexa();
        const debugActor = actorSpawner.spawn('debugActor');
        const transform = debugActor.getComponent(Transform);
        transform.local.position.x = collider.centerX;
        transform.local.position.y = collider.centerY;
        let shape;
        switch (collider.type) {
            case 'box':
                shape = new Shape(Object.assign({ type: 'rectangle', width: (_b = collider.sizeX) !== null && _b !== void 0 ? _b : 0, height: (_c = collider.sizeY) !== null && _c !== void 0 ? _c : 0, strokeColor: color, fill: fillColor }, DEFAULT_PROPS));
                break;
            case 'capsule':
                shape = getCapsuleShape(collider, transform, color, fillColor);
                break;
            case 'circle':
                shape = new Shape(Object.assign({ type: 'circle', radius: (_d = collider.radius) !== null && _d !== void 0 ? _d : 0, strokeColor: color, fill: fillColor }, DEFAULT_PROPS));
                break;
            case 'segment':
                shape = new Shape(Object.assign({ type: 'line', point1X: (_f = (_e = collider.point1) === null || _e === void 0 ? void 0 : _e.x) !== null && _f !== void 0 ? _f : 0, point1Y: (_h = (_g = collider.point1) === null || _g === void 0 ? void 0 : _g.y) !== null && _h !== void 0 ? _h : 0, point2X: (_k = (_j = collider.point2) === null || _j === void 0 ? void 0 : _j.x) !== null && _k !== void 0 ? _k : 0, point2Y: (_m = (_l = collider.point2) === null || _l === void 0 ? void 0 : _l.y) !== null && _m !== void 0 ? _m : 0, strokeColor: color }, DEFAULT_PROPS));
                break;
        }
        debugActor.setComponent(shape);
        return debugActor;
    },
    update: (actor, debugActor, options) => {
        const transform = actor.getComponent(Transform);
        const debugTransform = debugActor.getComponent(Transform);
        const debugShape = debugActor.getComponent(Shape);
        if (debugShape.type === 'circle') {
            const maxScale = Math.max(transform.world.scale.x, transform.world.scale.y);
            debugTransform.world.scale.x = maxScale;
            debugTransform.world.scale.y = maxScale;
        }
        const zoom = getCurrentZoom(options.world);
        debugShape.strokeWidth = STROKE_WIDTH / zoom;
    },
};
