package eu.theunitry.fabula.levels;


import eu.theunitry.fabula.UNGameEngine.graphics.UNGameScreen;
import eu.theunitry.fabula.UNGameEngine.graphics.UNColor;
import eu.theunitry.fabula.UNGameEngine.graphics.UNLevel;
import eu.theunitry.fabula.UNGameEngine.graphics.UNGraphicsObject;
import eu.theunitry.fabula.UNGameEngine.launcher.UNLauncher;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;

/**
 * Level 1
 * Maarten Bode
 */
public class Level1 extends UNLevel
{
    private Timer timer;
    private JButton button;
    private boolean winning;
    private int need, touch;
    private String lastHelp;

    /**
     * Level 1
     * @param gameScreen
     * @param hudEnabled
     */
    public Level1(UNGameScreen gameScreen, boolean hudEnabled)
    {
        super(gameScreen, hudEnabled);

        gameScreen.stopAudio();

        this.need = 3 + new Random().nextInt(3);

        this.setQuestion("Oh nee! Blobby probeert de wereld te vernietigen. Stop hem!");
        this.addHelp("Jammer! Je moet " + need + " appels in de mand stoppen");
        this.addHelp("Helaas! Er moeten " + need + " appels in de mand zitten");
        this.setHelp("Sleep het aantal appels in de mand");
        this.setBackgroundImage(gameScreen.unResourceLoader.backgrounds.get("the-end"));
        gameScreen.getMusic().get("the-end").play(true);
        gameScreen.getMusic().get("the-end").setVolume(0.1);

        this.winning = false;
        this.lastHelp = getHelp();

        this.button = new JButton("Vuren!");

        this.setLayout(null);
        this.button.setBounds(618, 64, 150, 50);
        this.button.setBackground(new Color(51, 51, 51));
        this.button.setFont(new Font("Minecraftia", Font.PLAIN, 15));
        this.button.setForeground(Color.white);
        this.button.setOpaque(true);

        /**
         * Reset Default Styling
         */
        this.button.setFocusPainted(false);
        this.button.setBorderPainted(false);

        button.addActionListener(e -> {
            if (button.getText().equals("Doorgaan"))
            {
                levelDone(5);
            }
            if (isHelperDoneTalking()) {
                if (winning) {
                    getHelper().setState(3);
                    setHelp("Goed gedaan, dat wordt smikkelen en smullen!");

                    button.setText("Door");
                } else {
                    addMistake();
                    if (getMistakes() < 3) {
                        getHelper().setState(4);
                        while(lastHelp.equals(getHelp())) {
                            setHelp(getHelpList().get(new Random().nextInt(getHelpList().size())));
                        }
                        lastHelp = getHelp();
                    } else {
                        getHelper().setState(4);
                        if (touch < need) {
                            setHelp("Jammer, er moest" + ((need - touch == 1) ? "" : "en") + " nog " + (need - touch) +
                                    " appel" + ((need - touch == 1) ? "" : "s")  + " bij. Want " + touch + " plus " +
                                    (need - touch)  + " is " + need
                            );
                        } else {
                            setHelp("Jammer, er moest" + ((touch - need == 1) ? "" : "en") + " " + (touch - need) +
                                    " appel" + ((touch - need == 1) ? "" : "s")  + " af. Want " + touch + " min " +
                                    (touch - need)  + " is " + need
                            );
                        }

                        button.setText("Door");
                    }
                }
            }
        });

        this.getPanel().add(button);



        timer = new Timer(1, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                touch = 0;
                winning = (touch == need);
            }
        });

        timer.start();
    }
}

@@ -1016,24 +1016,63 @@
 extInt(3);%0A%0A
+        // Load questions & help texts%0A
         this
@@ -1144,24 +1144,24 @@
 top hem!%22);%0A
-
         this
@@ -1368,16 +1368,49 @@
  mand%22);
+%0A%0A        //  Set resources/audio
 %0A       
@@ -1487,24 +1487,25 @@
 the-end%22));%0A
+%0A
         game
@@ -1607,24 +1607,49 @@
 lume(0.1);%0A%0A
+        // Set variables%0A
         this
@@ -1740,32 +1740,60 @@
 ton(%22Vuren!%22);%0A%0A
+        // Standard styling%0A
         this.set
@@ -1805,16 +1805,47 @@
 t(null);
+%0A%0A        // Set button styling
 %0A       
@@ -2113,55 +2113,35 @@
    /
-**%0A         * Reset Default Styling%0A         */
+/ Makes it look good on Mac
 %0A   
@@ -2223,24 +2223,53 @@
 ed(false);%0A%0A
+        // Speaks for itself%0A
         butt
@@ -4034,18 +4034,16 @@
 tton);%0A%0A
-%0A%0A
         

Add comments to Level1