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

function setAnimations(r, store) {
    const { focus, event: [type, variant] } = r;

    if (type === 'HitAoe') {
        const { construct } = variant;
        const aoeFocus = focus.concat(construct);

        store.dispatch(actions.setResolution(null));
        store.dispatch(actions.setAnimFocus(aoeFocus));
        store.dispatch(actions.setAnimTarget(r));

        return setTimeout(() => store.dispatch(actions.setAnimSource(null)), TIMES.SOURCE_DURATION_MS);
    }

    store.dispatch(actions.setAnimFocus(focus));

    if (type === 'Cast') {
        store.dispatch(actions.setResolution(null));
        store.dispatch(actions.setAnimSource(r));
        return setTimeout(() => store.dispatch(actions.setAnimSource(null)), TIMES.SOURCE_DURATION_MS);
    }

    if (type === 'Hit') {
        store.dispatch(actions.setResolution(null));
        store.dispatch(actions.setAnimTarget(r));
        return setTimeout(() => store.dispatch(actions.setAnimTarget(null)), TIMES.TARGET_DURATION_MS);
    }

    return store.dispatch(actions.setResolution(r));
}

function clearAnimations(store) {
    store.dispatch(actions.setAnimSkill(null));
    store.dispatch(actions.setAnimSource(null));
    store.dispatch(actions.setAnimTarget(null));
    store.dispatch(actions.setResolution(null));
    store.dispatch(actions.setAnimating(false));
    store.dispatch(actions.setGameEffectInfo(null));
    store.dispatch(actions.setAnimFocus(null));
}


module.exports = {
    setAnimations,
    clearAnimations,
};
