import {Player} from "../entities/Player";
export class RitualManager {
    circle:Circle;
    rotation = 0;

    totalTime = 0;
    playerTime = 0;

    constructor(public game:Phaser.Game) {
        this.circle = new Circle(this.game);
    }

    update = ()=> {
        this.circle.update();
        this.circle.angle += this.rotation;
    };

    rotate = (clockWise:boolean = true)=> {
        this.rotation = clockWise ? 0.45 : -0.45;
    };

    stopRotation = ()=> {
        this.rotation = 0;
    };

    setRotation = (rotation:number)=> {
        this.rotation = rotation;
    };

    check = (player:Player) => {
        var delta = this.game.time.elapsedMS;
        this.totalTime += delta;
        var x = player.sprite.position.x + (player.sprite.width / 2);
        var y = player.sprite.position.y + player.sprite.height;
        var playerPosition = new Phaser.Point(x, y);
        var playerField = this.circle.fields[5];
        var fieldX = playerField.x + this.game.world.centerX;
        var fieldY = playerField.y + this.game.world.centerY;
        var distance = playerPosition.distance(new Phaser.Point(playerField.x + this.game.world.centerX, playerField.y + this.game.world.centerY));
        if (distance < playerField.radius + 10) {
            this.playerTime += delta;
        }
    };

    checkIfWon = ()=> {
        var style = {font: "40px Arial", fill: "#ececec", align: "center"};
        var percent = 100 / this.totalTime * this.playerTime;
        if (percent < 90) {
            this.game.time.events.add(2000, ()=> {
                this.game.state.start('gameOver');
            });
            this.game.add.text(50, 100, "90%+ wins", style);
        }
        this.game.add.text(50, 50, (percent >= 90 ? "Won: " : "Lost: ") + percent + "%", style);
    };
}

export class Circle {
    x:number;
    y:number;
    angle = 0;
    fields:Field[] = [];
    radius = 200;

    constructor(private game:Phaser.Game) {
        this.x = this.game.world.centerX;
        this.y = this.game.world.centerY;

        var alpha = 360 / 6;
        for (var i = 0; i < 6; i++) {
            this.fields.push(this.getField(i * alpha));
            this.fields[i].groundCircle = this.game.add.graphics(this.game.world.centerX, this.game.world.centerY);
        }
    }

    getField = (angle:number) => {
        var result = new Field();
        result.angle = angle;
        result.setPosition(this.radius, 0);
        return result;
    };

    updateFields = () => {
        this.fields.forEach((field, index)=> {
            field.setPosition(this.radius, this.angle);
            field.drawCircle();
        });
    };

    update = ()=> {
        this.updateFields();
        //this.angle -= 0.45;
    };
}

export class Field {
    x:number;
    y:number;
    radius = 40;
    angle:number;

    groundCircle:Phaser.Graphics;

    setPosition = (radius:number, angle:number)=> {
        this.x = radius * Math.cos(Phaser.Math.degToRad(this.angle + angle));
        this.y = radius * Math.sin(Phaser.Math.degToRad(this.angle + angle));
    };

    drawCircle = ()=> {
        if (this.groundCircle) {
            this.groundCircle.clear();
            this.groundCircle.alpha = 0.5;
            this.groundCircle.beginFill(0x00bff3);
            this.groundCircle.arc(this.x, this.y, this.radius, 0, Phaser.Math.degToRad(360), false);
            this.groundCircle.endFill();
        }
    }
}