const preact = require('preact');
const { Component } = require('preact');
const anime = require('animejs').default;
const { connect } = require('preact-redux');

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

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

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

    render() {
        return (
            <svg
                class="skill-animation"
                version="1.1"
                id="curse"
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 198 200">
                <defs>
                    <filter id="curseFilter">
                        <feGaussianBlur in="SourceGraphic" stdDeviation="5" />
                        <feMerge>
                            <feMergeNode />
                            <feMergeNode in="SourceGraphic" />
                        </feMerge>
                    </filter>
                </defs>
                <g filter="url(#curseFilter)" class="blue" >
                    <circle id="curseFilterOne" cx="100" cy="100" r="0" />
                    <circle id="curseFilterTwo" cx="100" cy="100" r="0" />
                    <circle id="curseFilterThree" cx="100" cy="100" r="0" />
                </g>

                <g class="white" style={{ strokeWidth: '2px' }}>
                    <circle id="curseCircleOne" cx="100" cy="100" r="0" />
                    <circle id="curseCircleTwo" cx="100" cy="100" r="0" />
                    <circle id="curseCircleThree" cx="100" cy="100" r="0" />
                </g>
            </svg>
        );
    }

    componentDidMount() {
        this.animations.push(anime({
            targets: ['#curse'],
            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: ['#curseCircleOne', '#curseFilterOne'],
            r: 30,
            easing: 'easeInOutSine',
            delay: TIMES.TARGET_DELAY_MS,
            duration: TIMES.TARGET_DURATION_MS,
        }));

        this.animations.push(anime({
            targets: ['#curseCircleTwo', '#curseFilterTwo'],
            r: 60,
            easing: 'easeInOutSine',
            delay: TIMES.TARGET_DELAY_MS,
            duration: TIMES.TARGET_DURATION_MS,
        }));

        this.animations.push(anime({
            targets: ['#curseCircleThree', '#curseFilterThree'],
            r: 90,
            easing: 'easeInOutSine',
            delay: TIMES.TARGET_DELAY_MS,
            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(Curse);
