package sk.perri.ggj18f;

import org.lwjgl.Sys;
import org.newdawn.slick.*;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import java.util.Random;
import java.util.Vector;

public class Game extends BasicGameState implements InputListener
{
    private Image rocket;
    private Vector<Image> w = new Vector<>();
    private int x = 0;
    private int timer = 10000;
    Main main;
    private boolean wi = false;
    private boolean line = false;
    private float ly = 0;
    private int randX;

    Game(Main main)
    {
        this.main = main;
    }

    @Override
    public int getID()
    {
        return 1;
    }

    @Override
    public void init(GameContainer gameContainer, StateBasedGame stateBasedGame) throws SlickException
    {
        rocket = Holder.images.get(0).getScaledCopy(0.25f);
        w.add(Holder.images.get(1).getScaledCopy(0.1f));
        w.add(Holder.images.get(2).getScaledCopy(0.1f));
        w.add(Holder.images.get(3).getScaledCopy(0.1f));
        w.add(Holder.images.get(4).getScaledCopy(0.1f));

        gameContainer.getGraphics().setBackground(Color.white);
        gameContainer.setMaximumLogicUpdateInterval(50);
    }

    @Override
    public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics graphics) throws SlickException
    {
        if(rocket == null)
            init(gameContainer, main);

        rocket.drawCentered(x, gameContainer.getHeight()*0.75f);

        if(wi)
        {
            w.get(Math.abs(timer / 100) % w.size()).drawCentered((randX + 1) * (gameContainer.getWidth() / 8), 20);
        }

        if(line)
        {
            for(int i = 0; i < 8; i++)
            {
                graphics.setColor(Color.black);
                if(i != randX)
                    graphics.drawString("________", i*(gameContainer.getWidth()/7), ly);
            }
        }
    }

    @Override
    public void update(GameContainer gameContainer, StateBasedGame stateBasedGame, int i) throws SlickException
    {
        timer -= i;

        if(Math.abs(timer) < 5)
        {
            wi = true;
            randX = (new Random()).nextInt(7);
            System.out.println("a");
        }

        if(timer < -5000)
        {
            wi = false;
            line = true;
            ly += 5f;
        }

        if(ly > gameContainer.getHeight()-20)
        {
            line = false;
            ly = 0;
            timer = 10000;

            if((randX-1)*(gameContainer.getWidth()/7) > x || (randX + 1)*(gameContainer.getWidth()/7) < x)
                System.exit(0);
        }
    }

    @Override
    public void mouseMoved(int oldx, int oldy, int newx, int newy)
    {
        super.mouseMoved(oldx, oldy, newx, newy);

        x = newx;
    }
}
