package com.lubjam16.epiceggsmash.game;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.lubjam16.epiceggsmash.global.Utils;

/**
 * Created by stonek on 1/30/16.
 */
public class Egg {
    public static final boolean LEFT = true;
    public static final boolean RIGHT = false;
    public static final Vector2 LEFT_EGG_INIT =
            new Vector2(Graphics.bigRightEgg.getWidth()/2,300);
    public static final Vector2 RIGHT_EGG_INIT =
            new Vector2(800 - 1.5f*Graphics.bigRightEgg.getWidth(),300);
    public static final Vector2[] rightEggShellPositionRel = new Vector2[10];
    public static final Vector2[] leftEggShellPositionRel = new Vector2[10];
    public Vector2[] eggShellsPosition;

    public EggState state = EggState.NONE;
    public boolean winning = false;

    static {
        rightEggShellPositionRel[0] = new Vector2(55,-14);
        rightEggShellPositionRel[1] = new Vector2(18,30);
        rightEggShellPositionRel[2] = new Vector2(-6,-25);
        rightEggShellPositionRel[3] = new Vector2(-30,31);
        rightEggShellPositionRel[4] = new Vector2(-52,-26);
        rightEggShellPositionRel[5] = new Vector2(-53,3);
        rightEggShellPositionRel[6] = new Vector2(-67,-9);
        rightEggShellPositionRel[7] = new Vector2(-56,23);
        rightEggShellPositionRel[8] = new Vector2(-77,2);
        rightEggShellPositionRel[9] = new Vector2(-74,-15);

        leftEggShellPositionRel[0] = new Vector2(-58,-5);
        leftEggShellPositionRel[1] = new Vector2(-24,12);
        leftEggShellPositionRel[2] = new Vector2(-20,41);
        leftEggShellPositionRel[3] = new Vector2(-6,-39);
        leftEggShellPositionRel[4] = new Vector2(26,25);
        leftEggShellPositionRel[5] = new Vector2(39,-20);
        leftEggShellPositionRel[6] = new Vector2(49,21);
        leftEggShellPositionRel[7] = new Vector2(66,8);
        leftEggShellPositionRel[8] = new Vector2(63,-18);
        leftEggShellPositionRel[9] = new Vector2(72,3);

    };

    private Vector2 position;
    private Vector2 sourcePos;
    private Vector2 targetPos;
    private float speed;
    private Vector2 tmpDir;
    private Vector2 initPos;
    private boolean side;
    private GameScreen gameScreen;

    private Texture fullEgg;
    private Texture[] eggShells;
    private float elapsedExplosion;
    private ParticleEffect explosion;

    public Egg(boolean side, GameScreen gameScreen){
        this.gameScreen = gameScreen;
        this.side = side;
        this.eggShellsPosition = new Vector2[10];
        if(side == LEFT){
            initPos = LEFT_EGG_INIT;
            fullEgg = Graphics.bigLeftEgg;
            eggShells = Graphics.leftEggShells;
            explosion = Particles.explosionParticlesLeft;
        }
        else{
            initPos = RIGHT_EGG_INIT;
            fullEgg = Graphics.bigRightEgg;
            eggShells = Graphics.rightEggShells;
            explosion = Particles.explosionParticlesRight;
        }
        init();
    }

    public void init(){
        position = initPos.cpy();
        sourcePos = initPos.cpy();
        targetPos = initPos.cpy().add(Utils.randomVector(20));
        speed = 5;
        state = EggState.NONE;
        winning = false;
        for(int i = 0; i<10; i++){
            eggShellsPosition[i] = initPos.cpy();
        }
    }

    public void updateOutcome(float delta){
        switch (state){
            case MOVING:
                if(side == LEFT){
                    position.x += delta*750.0f;
                    if(position.x > 400 - fullEgg.getWidth()*0.75){
                        if(!winning){
                            state = EggState.EXPLODING;
                            initExplosion();
                        }
                        else{
                            state = EggState.NONE;
                        }
                    }
                }
                else{
                    position.x -= delta*750.0f;
                    if(position.x < 400 - fullEgg.getWidth()*0.25){
                        if(!winning){
                            state = EggState.EXPLODING;
                            initExplosion();
                        }
                        else{
                            state = EggState.NONE;
                        }
                    }
                }
                break;
            case EXPLODING:
                elapsedExplosion += delta;
                for(int i = 0; i<10; i++){
                    if(side == LEFT){
                        eggShellsPosition[i].add(leftEggShellPositionRel[i].cpy().scl(delta * 10));
                    }
                    else{
                        eggShellsPosition[i].add(rightEggShellPositionRel[i].cpy().scl(delta * 10));
                    }
                }
                if(winning){
                    return;
                }
                break;
            default:
                break;
        }
    }

    public void initExplosion() {
        for(int i = 0; i < 10; i++){
            eggShellsPosition[i] = position.cpy();
        }
        elapsedExplosion = 0;
        gameScreen.setShake();
        Sounds.explosion.play(0.7f);
        explosion.reset();
        explosion.start();
        for(int i = 0; i < 4; i++){
            explosion.getEmitters().get(i)
                    .setPosition(position.x + 64, position.y + 64);
        }
    }

    public void updateFight(float delta){
        float wayPrc = position.dst2(targetPos)/sourcePos.dst2(targetPos)*100;

        speed = wayPrc*(wayPrc-100.0f)*-0.0005f+0.1f;

        speed *= delta*50;

        tmpDir = targetPos.cpy().add(sourcePos.cpy().scl(-1));
        tmpDir.limit2(speed);

        position.add(tmpDir);

        if(wayPrc > 99){
            sourcePos = position;
            while(sourcePos.dst2(targetPos) < 0.5) {
                targetPos = initPos.cpy().add(Utils.randomVector(20));
            }
        }
    }

    public void draw(SpriteBatch batch,float delta) {
        switch (state){
            case EXPLODING:
                batch.draw(Animations.explosion
                                .getKeyFrame(elapsedExplosion, false),
                        position.x-32,
                        position.y-64);

                explosion.draw(batch, delta);

                for(int i = 0; i < 10; i++){
                    batch.draw(eggShells[i],
                            eggShellsPosition[i].x,
                            eggShellsPosition[i].y);
                }
                break;
            default:
                batch.draw(fullEgg, position.x, position.y);
                break;
        }
    }
}
