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

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

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

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

    render() {
        return (
            <svg
                class='skill-animation red'
                version="1.1"
                id="bash"
                xmlns="http://www.w3.org/2000/svg"
                style={{ transform: 'scale(3)' }}
                viewBox="0 0 128 128">
                <filter id='bashFilter'>
                    <feTurbulence type="turbulence" baseFrequency="0" numOctaves="1" result="turbulence"></feTurbulence>
                    <feDisplacementMap in2="turbulence" in="SourceGraphic" scale="1" xChannelSelector="R" yChannelSelector="G"></feDisplacementMap>
                </filter>
                <rect
                    x="32"
                    y="32"
                    width="64"
                    height="64"
                    style={{ filter: 'url("#bashFilter")' }}>
                </rect>
            </svg>
        );
    }

    componentDidMount() {
        this.animations.push(anime({
            targets: ['#bash'],
            opacity: [
                { value: 1, delay: TIMES.TARGET_FADE_IN_DELAY, duration: TIMES.TARGET_FADE_IN_DURATION },
                // this is badly behaved, nfi why
                { value: 0, delay: TIMES.FADE_DELAY_MS / 2, duration: TIMES.FADE_OUT_DURATION },
            ],
            easing: 'easeInOutSine',
        }));

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

            keyframes: [
                { translateX: 0, translateY: 0 },
                { translateX: -5, translateY: 0 },
                { translateX: 0, translateY: -5 },
                { translateX: 5, translateY: 0 },
                { translateX: 0, translateY: 5 },
                { translateX: -2, translateY: 0 },
                { translateX: 0, translateY: -2 },
                { translateX: 2, translateY: 0 },
                { translateX: 0, translateY: 2 },
            ],

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

        this.animations.push(anime({
            targets: ['#bashFilter feTurbulence', '#bashFilter feDisplacementMap'],
            baseFrequency: 2,
            scale: 10,
            numOctaves: 5,
            easing: 'easeOutSine',

            delay: TIMES.TARGET_FADE_IN_DELAY,
            duration: TIMES.RESOLUTION_TOTAL_MS,
        }));
    }

    // 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 = Bash;
