import {Line} from '@int/geotoolkit/scene/shapes/Line';
import {Point} from '@int/geotoolkit/util/Point';
import {Polygon} from '@int/geotoolkit/scene/shapes/Polygon';
import {ComponentNode} from '@int/geotoolkit/schematics/scene/ComponentNode';
import {Rect} from '@int/geotoolkit/util/Rect';
import {obfuscate} from '@int/geotoolkit/lib';

class NodeCasingBW extends ComponentNode {
    constructor (data) {
        super();
        this._linePos = new Line({
            'from': new Point(),
            'to': new Point(),
            'linestyle': 'black'
        });
        this._lineNeg = new Line({
            'from': new Point(),
            'to': new Point(),
            'linestyle': 'black'
        });

        this._shoePos = new Polygon()
            .setFillStyle('black');
        this._shoeNeg = new Polygon()
            .setFillStyle('black');

        this._szShoeDepthPx = 5; // in device space
        this._szShoeValuePx = 15; // in device space

        this._geometryBounds = null;

        this.setNotification(false);
        this.addChild([this._linePos, this._lineNeg, this._shoePos, this._shoeNeg]);
        this.setNotification(true);
        super.setData(data);
    }

    setGeometryData (geometry, deepCopy) {
        if (!geometry) {
            return;
        }
        super.setGeometryData(geometry, deepCopy);

        const radInner = geometry['diameter']['inner'] / 2;
        const depthMin = geometry['depth']['from'];
        const depthMax = geometry['depth']['to'];
        this.setNotification(false);
        this._linePos.setLine({
            'from': new Point(radInner, depthMin),
            'to': new Point(radInner, depthMax)
        });
        this._lineNeg.setLine({
            'from': new Point(-radInner, depthMin),
            'to': new Point(-radInner, depthMax)
        });
        this._shoePos.updateState();
        this._shoeNeg.updateState();

        this._geometryBounds = null;
        this.setModelLimits(this.getGeometryBounds());
        this.setNotification(true);
    }

    updateShapeGeometry (context) {
        if (this.getWorldTransform()) {
            context = context.pushTransformation(this.getWorldTransform());
        }

        const geometry = this.getGeometryData();
        if (!geometry || geometry.length < 1) {
            return;
        }

        // var depthMin = geometry['depth']['from'];
        const depthMax = geometry['depth']['to'];
        const radInner = geometry['diameter']['inner'] / 2;
        // var radOuter = geometry['diameter']['outer'] / 2;

        // Update device-sized elements:
        const tr = context.getTransformation();
        // VERTICAL case ONLY:
        const szShoeDepth = this._szShoeDepthPx / tr.getScaleY();
        const szShoeValue = this._szShoeValuePx / tr.getScaleX();

        const aValuePos = [radInner, radInner + szShoeValue, radInner];
        const aDepthPos = [depthMax, depthMax, depthMax - szShoeDepth];

        this._shoePos.setNotification(false);
        this._shoePos.setCoordinates(aValuePos, aDepthPos);
        this._shoePos.setNotification(true);

        const aValueNeg = [-radInner, -radInner - szShoeValue, -radInner];
        const aDepthNeg = [depthMax, depthMax, depthMax - szShoeDepth];

        this._shoeNeg.setNotification(false);
        this._shoeNeg.setCoordinates(aValueNeg, aDepthNeg);
        this._shoeNeg.setNotification(true);
    }

    checkCollision (context) {
        this.updateShapeGeometry(context);
        return super.checkCollision(context);
    }

    render (context) {
        this.updateShapeGeometry(context);
        super.render(context);
    }

    getGeometryBounds () {
        if (!this._geometryBounds) {
            const geometry = this.getGeometryData();
            if (!geometry || geometry.length < 1) {
                return null;
            }
            const depthMin = geometry['depth']['from'];
            const depthMax = geometry['depth']['to'];
            const radOuter = geometry['diameter']['outer'] / 2;
            this._geometryBounds = new Rect(-radOuter, depthMin, radOuter, depthMax);
        }
        return this._geometryBounds;
    }
}
obfuscate(NodeCasingBW);
export {NodeCasingBW};
