const preact = require('preact');
const { Component } = require('preact');

const anime = require('animejs').default;

const { TIMES } = require('../../constants');

class Silence extends Component {
    constructor() {
        super();
        this.animations = [];
    }

    render() {
        return (
            <svg
                class='skill-animation'
                version="1.1"
                id="silence"
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 128 128">
                <filter id="silenceFilter">
                    <feGaussianBlur in="SourceGraphic" stdDeviation="5" />
                    <feMerge>
                        <feMergeNode />
                        <feMergeNode in="SourceGraphic" />
                    </feMerge>
                </filter>
                <g filter="url(#silenceFilter)" stroke-width="4px" >
                    <path class="rOne blue" d="M 8 124 L 8 8" />
                    <path class="rTwo blue" d="M 124 8 L 124 124" />
                    <path class="rOne white" d="M 8 124 L 8 8" stroke-width="1px" />
                    <path class="rTwo white" d="M 124 8 L 124 124" stroke-width="1px" />
                </g>
            </svg>
        );
    }

    componentDidMount() {
        this.animations.push(anime({
            targets: ['#silence'],
            opacity: [
                { value: 1, delay: TIMES.TARGET_DELAY_MS, duration: TIMES.TARGET_DURATION_MS * 0.2 },
                { value: 0, delay: TIMES.TARGET_DURATION_MS * 0.6, duration: TIMES.TARGET_DURATION_MS * 0.2 },
            ],
            easing: 'easeInOutSine',
        }));

        this.animations.push(anime({
            targets: ['#silence'],
            rotate: [90, 90],
            scale: {
                value: 1,
                delay: TIMES.TARGET_DELAY_MS,
                duration: TIMES.TARGET_DURATION_MS * 0.2,
                easing: 'easeInExpo',
            },

            delay: TIMES.TARGET_DELAY_MS + TIMES.TARGET_DURATION_MS * 0.1,
            duration: TIMES.TARGET_DURATION_MS * 0.2,
            easing: 'easeOutSine',
        }));

        this.animations.push(anime({
            targets: ['.rOne'],

            d: 'M 124 8 L 8 124',

            delay: TIMES.TARGET_DELAY_MS + TIMES.TARGET_DURATION_MS * 0.4,
            duration: TIMES.TARGET_DURATION_MS * 0.4,
            easing: 'easeOutSine',
        }));

        this.animations.push(anime({
            targets: ['.rTwo'],

            d: 'M 124 124 L 8 8',

            delay: TIMES.TARGET_DELAY_MS + TIMES.TARGET_DURATION_MS * 0.4,
            duration: TIMES.TARGET_DURATION_MS * 0.4,
            easing: 'easeOutSine',
        }));
    }

    // this is necessary because
    // skipping / timing / unmounting race conditions
    // can cause the animations to cut short, this will ensure the values are reset
    // because preact will recycle all these components
    componentWillUnmount() {
        for (let i = this.animations.length - 1; i >= 0; i--) {
            this.animations[i].reset();
        }
    }
}

module.exports = Silence;
