const preact = require('preact');
const { Component } = require('preact');
const times = require('lodash/times')
const { connect } = require('preact-redux');

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

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

const addState = connect(
    function receiveState(state) {
        const { animCb } = state;
        return { animCb };
    }
);

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

    render() {
        return (
            <svg
                class='skill-animation blue'
                version="1.1"
                id="electrify"
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 128 128">
                <defs>
                    <filter id="electrifyFilter">
                        <feGaussianBlur in="SourceGraphic" stdDeviation="5" />
                        <feMerge>
                            <feMergeNode />
                            <feMergeNode in="SourceGraphic" />
                        </feMerge>
                    </filter>
                </defs>
                <path
                    class='blue'
                    filter='url("#electrifyFilter")'
                    d='M10,64 L10,64 L20,64 L30,64 L40,64 L50,64 L60,64 L70,64 L80,64 L90,64 L100,64 L110,64'
                />
                <path
                    class='white'
                    filter='url("#electrifyFilter")'
                    d='M10,64 L10,64 L20,64 L30,64 L40,64 L50,64 L60,64 L70,64 L80,64 L90,64 L100,64 L110,64'
                />
            </svg>
        );
    }

    componentDidMount() {
        this.animations.push(anime({
            targets: ['#electrify'],
            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',
        }));

        const path = () => `M10,64
            L10,${anime.random(34, 94)}
            L20,${anime.random(34, 94)}
            L30,${anime.random(34, 94)}
            L40,${anime.random(34, 94)}
            L50,${anime.random(34, 94)}
            L60,${anime.random(34, 94)}
            L70,${anime.random(34, 94)}
            L80,${anime.random(34, 94)}
            L90,${anime.random(34, 94)}
            L100,${anime.random(34, 94)}
            L110,64
        `;

        this.animations.push(anime({
            targets: ['#electrify path'],
            d: times(10, path),
            easing: 'easeInOutSine',
            loop: true,
            duration: TIMES.TARGET_DURATION_MS / 5,
        }));

        this.animations.push(anime({
            targets: ['#electrify path.white'],
            strokeWidth: [2, 1],
            easing: 'easeInOutSine',
            direction: 'alternate',
            loop: true,
            duration: TIMES.TARGET_DURATION_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();
        }
        this.props.animCb && this.props.animCb();
    }
}

module.exports = addState(Electrocute);
