package waterfall;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

import scenes.GamePlay;


public class Waterfall extends Sprite
{

    private World world;

    private Body body;

    private TextureAtlas waterfallAtlas;

    private Animation<TextureRegion> waterfallAnimation;

    private float elapsedTime;

    public Waterfall(float x, float y)
    {
        super(new Texture("Buttons/Waterfall.png"));

        setPosition(x - 30f, y + 20f - 690f); //method coming from Sprite class
        createBody(x - 30f, y + 20f - 690f);
        waterfallAtlas = new TextureAtlas("Animation/Waterfall Animation.png.atlas");
    }

    void createBody(float x, float y)
    {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.StaticBody;

        bodyDef.position.set(x, y);

        body = GamePlay.getWorld().createBody(bodyDef);

        PolygonShape shape = new PolygonShape();
        //        shape.setAsBox((getWidth() / 2f), getHeight() / 2f);
        shape.setAsBox(getX() / 2f, getY() / 2f);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;

        body.createFixture(fixtureDef);

        shape.dispose();
    }

    public void setSpritePosition(float x, float y)
    {
        setPosition(x, y);
        createBody(x, y);
    }

    public void drawWaterfallAnimation(SpriteBatch batch)
    {
        elapsedTime += Gdx.graphics.getDeltaTime();

        waterfallAnimation = new Animation<TextureRegion>(1f / 10f, waterfallAtlas.getRegions());

        // draw all the animation in the animation set
        batch.draw(waterfallAnimation.getKeyFrame(elapsedTime, true), getX(), getY());
    }
}
