const Phaser = require('phaser');
const { POSITIONS: { COMBAT }, DELAYS } = require('./constants');

const randomColour = () => {
    const colours = ['green', 'blue', 'red', 'white', 'yellow'];
    return colours[Math.floor(Math.random() * 5)];
};

const animationParams = (sourceAlly) => {
    const spawnLocation = sourceAlly ? COMBAT.width() * 0.35 : COMBAT.width() * 0.65;
    const speed = sourceAlly ? 250 : -250;
    const img = randomColour();
    const angleMin = sourceAlly ? 320 : 180;
    const angleMax = sourceAlly ? 360 : 220;
    return { spawnLocation, speed, img, angleMin, angleMax };
};

const randomAttack = () => {
    const animations = ['wall', 'spit', 'gravBlast', 'gravBomb', 'chargeBall'];
    return animations[Math.floor(Math.random() * 5)];
};

class CombatSkills extends Phaser.GameObjects.Group {
    constructor(scene) {
        super(scene);
        this.scene = scene;
    }

    getSkill(type, sourceAlly, targetAlly, castLocation) {
        const genericHeal = ['Heal', 'Triage', 'TriageTick', 'DecayTick'];
        const genericBlock = ['Block', 'Parry', 'Evasion', 'Shield'];

        if (genericHeal.includes(type)) {
            this.genericHeal(targetAlly, castLocation);
        } else if (genericBlock.includes(type)) {
            this.genericBlock(sourceAlly);
        } else {
            this[randomAttack()](sourceAlly);
        }
    }

    genericHeal(sourceAlly, castLocation) {
        // const { sourceX, sourceY } = getCrypPosition(sourcePos, 0);
        const lifespan = DELAYS.ANIMATION_DURATION;
        const colour = randomColour();
        const particles = this.scene.add.particles(colour);
        const x = sourceAlly ? COMBAT.width() * 0.3 : COMBAT.width() * 0.7;

        const emitter2 = particles.createEmitter({
            x: castLocation.x,
            y: castLocation.y,
            moveToX: x,
            moveToY: COMBAT.height() * 0.2,
            speed: 500,
            lifespan: lifespan / 3,
            scale: { start: 0.5, end: 1 },
            quantity: 3,
            _frequency: 20,
            blendMode: 'ADD',
            emitZone: { source: new Phaser.Geom.Rectangle(-200, -100, 400, 200) },
        });

        const emitter = particles.createEmitter({
            x,
            y: COMBAT.height() * 0.2,
            angle: { min: 250, max: 290 },
            speed: 250,
            gravityY: 1000,
            quantity: 4,
            scale: { start: 0.1, end: 1 },
            blendMode: 'ADD',
            lifespan,
            active: false,
        });


        this.add(particles);
        this.scene.time.delayedCall(lifespan / 3, () => { emitter2.stop(); }, [], this);
        this.scene.time.delayedCall(lifespan / 3, () => { emitter.active = true; }, [], this);

        this.scene.time.delayedCall(lifespan, () => { emitter.stop(); }, [], this);
    }

    genericBlock(sourceAlly) {
        const lifespan = DELAYS.ANIMATION_DURATION;
        const colour = randomColour();
        const x = sourceAlly ? COMBAT.width() * 0.3 : COMBAT.width() * 0.7;
        const emitter1 = this.scene.add.particles(colour).createEmitter({
            x,
            y: COMBAT.height() * 0.4,
            scale: { start: 0.75, end: 0.25 },
            blendMode: 'ADD',
            emitZone: {
                source: new Phaser.Geom.Rectangle(-100, -100, 200, 200),
                type: 'edge',
                quantity: 24,
                yoyo: true,
            },
        });
        const emitter2 = this.scene.add.particles(colour).createEmitter({
            x,
            y: COMBAT.height() * 0.4,
            blendMode: 'SCREEN',
            scale: { start: 0.2, end: 0 },
            speed: { min: -100, max: 100 },
            quantity: 50,
            active: false,
            emitZone: {
                source: new Phaser.Geom.Rectangle(-100, -100, 200, 200),
                type: 'edge',
                quantity: 50,
            },
        });
        this.scene.time.delayedCall(lifespan / 2, () => { emitter1.stop(); }, [], this);
        this.scene.time.delayedCall(lifespan / 2, () => { emitter2.active = true; }, [], this);
        this.scene.time.delayedCall(lifespan, () => { emitter2.stop(); }, [], this);
    }

    wall(sourceAlly) {
        const lifespan = DELAYS.ANIMATION_DURATION;
        const { spawnLocation, speed, img } = animationParams(sourceAlly);
        const particles = this.scene.add.particles(img);
        const emitter = particles.createEmitter({
            x: spawnLocation,
            y: { min: COMBAT.height() * 0.2, max: COMBAT.height() * 0.5 },
            speedX: { min: speed, max: speed * 2 },
            scale: { start: 0.4, end: 0 },
            quantity: 4,
            blendMode: 'ADD',
            lifespan,
        });
        this.add(particles);
        this.scene.time.delayedCall(1000, () => { emitter.stop(); }, [], this.scene);
    }

    spit(sourceAlly) {
        const lifespan = DELAYS.ANIMATION_DURATION;
        const { spawnLocation, speed, img, angleMin, angleMax } = animationParams(sourceAlly);
        const particles = this.scene.add.particles(img);
        const emitter = particles.createEmitter({
            x: spawnLocation,
            y: COMBAT.height() * 0.35,
            angle: { min: angleMin, max: angleMax },
            speed: speed * 2,
            scale: { start: 0.4, end: 1 },
            gravityY: 250,
            quantity: 4,
            blendMode: 'ADD',
            lifespan,
        });
        this.add(particles);
        this.scene.time.delayedCall(lifespan, () => { emitter.stop(); }, [], this);
    }

    gravBomb(sourceAlly) {
        const lifespan = DELAYS.ANIMATION_DURATION;

        const { spawnLocation, img } = animationParams(!sourceAlly);
        const particles = this.scene.add.particles(img);
        const well = particles.createGravityWell({
            x: spawnLocation,
            y: COMBAT.height() * 0.25,
            power: 4,
            gravity: 500,
        });
        this.emitter = particles.createEmitter({
            x: spawnLocation,
            y: COMBAT.height() * 0.25,
            speed: 1000,
            scale: { start: 0.7, end: 1 },
            blendMode: 'ADD',
            lifespan,
        });
        this.add(particles);
        this.scene.time.delayedCall(lifespan, () => { this.emitter.stop(); well.active = false; }, [], this.scene);
    }

    gravBlast(sourceAlly) {
        const lifespan = DELAYS.ANIMATION_DURATION;
        const WELL_END = lifespan / 2;

        const img = randomColour();
        const spawnLocation = sourceAlly ? COMBAT.width() * 0.35 : COMBAT.width() * 0.65;
        const isEnemyLocation = sourceAlly ? COMBAT.width() * 0.7 : COMBAT.width() * 0.3;
        const particles = this.scene.add.particles(img);
        const bounds = sourceAlly
            ? { x: COMBAT.width() * 0.3, y: COMBAT.height() * 0.2, w: COMBAT.width() * 0.5, h: COMBAT.height() * 0.2 }
            : { x: 0.2 * COMBAT.width(), y: COMBAT.height() * 0.2, w: COMBAT.width() * 0.5, h: COMBAT.height() * 0.2 };
        const well = particles.createGravityWell({
            x: spawnLocation,
            y: COMBAT.height() * 0.35,
            power: 4,
            gravity: 500,
        });
        const emitter = particles.createEmitter({
            x: spawnLocation,
            y: COMBAT.height() * 0.35,
            speed: 1000,
            scale: { start: 0.7, end: 1 },
            blendMode: 'ADD',
            bounds,
            lifespan,
        });
        this.add(particles);
        this.scene.time.delayedCall(WELL_END, () => { emitter.stop(); well.x = isEnemyLocation; }, [], this.scene);
        this.scene.time.delayedCall(lifespan, () => { well.active = false; }, [], this.scene);
    }

    chargeBall(sourceAlly) {
        const lifespan = DELAYS.ANIMATION_DURATION;
        const CHARGE_LIFESPAN = lifespan / 3;

        const { img, spawnLocation } = animationParams(sourceAlly);
        const targetLocation = sourceAlly ? 0.7 * COMBAT.width() : 0.25 * COMBAT.width();
        const particles = this.scene.add.particles(img);
        const emitter = particles.createEmitter({
            x: 0,
            y: 0,
            moveToX: spawnLocation,
            moveToY: COMBAT.height() * 0.1,
            scale: 0.75,
            quantity: 4,
            _frequency: 20,
            blendMode: 'ADD',
            emitZone: { source: new Phaser.Geom.Rectangle(0, 0, COMBAT.width(), COMBAT.height()) },
            lifespan: CHARGE_LIFESPAN,
        });
        const emitter2 = particles.createEmitter({
            radial: false,
            x: { min: spawnLocation, max: targetLocation, steps: 90 },
            y: { min: COMBAT.height() * 0.1, max: COMBAT.height() * 0.4, steps: 90 },
            quantity: 4,
            gravityY: 0,
            scale: { start: 2, end: 0.1, ease: 'Power3' },
            blendMode: 'ADD',
            active: false,
            lifespan: CHARGE_LIFESPAN,
        });
        this.add(particles);
        this.scene.time.delayedCall(CHARGE_LIFESPAN, () => { emitter.stop(); }, [], this.scene);
        this.scene.time.delayedCall(CHARGE_LIFESPAN * 2, () => { emitter2.active = true; }, [], this.scene);
        this.scene.time.delayedCall(lifespan, () => { emitter2.stop(); }, [], this.scene);
    }

    cleanup() {
        this.children.entries.forEach(obj => obj.destroy());
    }
}

module.exports = CombatSkills;
