
import java.awt.*;
import java.applet.*;

public class Minigolf extends Applet implements MyButtonPushListener {

    Color colorFrameLight = new Color (255,255,255);     // Color.white
    Color colorFrameDark  = new Color (128,128,128);     // Color.gray
    Color colorWindow     = new Color (192,192,192);     // Color.lightGray
    Color colorPlayfield  = new Color (102,102,153);
    Color colorException  = new Color (222,222,222);

    Font textFont = new Font("Helvetica", Font.BOLD, 16);

    MyButton itsButtonReset;
    MyButton itsButtonNext;
    MyButton itsButtonPrev;

    Scrollbar itsStrength;
    MyButton itsButtonShot;

    BahnVerwalter itsBahnverwalter;



    public void init() {
        super.init();
        System.out.println("Initialisierung (init)");

        /* Buttons */
        setLayout(null);
        setBackground (colorWindow);

        itsButtonReset = new MyButton("Reset");
        itsButtonNext  = new MyButton("Next");
        itsButtonPrev  = new MyButton("Previous");
        add(itsButtonReset);
        add(itsButtonNext);
        add(itsButtonPrev);
        itsButtonReset.reshape(30,20, 100,20);
        itsButtonNext.reshape(30,45, 100,20);
        itsButtonPrev.reshape(30,70, 100,20);
        itsButtonReset.addButtonPushListener(this);
        itsButtonNext.addButtonPushListener(this);
        itsButtonPrev.addButtonPushListener(this);

        /* Slider und Zubehör */
        itsStrength = new Scrollbar(Scrollbar.VERTICAL, 100, 1, 0, 100);
        add(itsStrength);
        itsStrength.reshape(15,180, 15,150);
        itsButtonShot = new MyButton("Shot");
        add(itsButtonShot);
        itsButtonShot.reshape(40, 310, 90, 20);
        itsButtonShot.addButtonPushListener(this);

        /* Spielleiter oder Bahnvewalter */
        itsBahnverwalter = new BahnVerwalter();

        /* lese Eingabe Parameter */
        String parameterName;
        String parameterValue;
        for (int bahnNummer =1; bahnNummer <=18; bahnNummer++) {
            parameterName = "Bahn" + bahnNummer;
            parameterValue = getParameter (parameterName);
            if (parameterValue == null) continue;
            itsBahnverwalter.addNewBahn (bahnNummer, parameterValue);
        };

        /* wähle erste Bahn aus */
        itsBahnverwalter.selectBahn(1);
        itsBahnverwalter.selectNextBahn();
        itsBahnverwalter.selectPrevBahn();
        itsBahnverwalter.resetCurrentBahn();

        /* Ball */
        itsBahnverwalter.itsBall.init();
        add(itsBahnverwalter.itsBall,0);

    };

    public void destroy()
    {
        System.out.println("Destroy (destroy)");
    };



    // Method directly from Component class
    // overwrite here to see what happens if we don't clear the screen
    public void update(Graphics g) {
	//g.setColor(getBackground());
	//g.fillRect(0, 0, width, height);
	//g.setColor(getForeground());
	paint(g);
    }


    public void paint(Graphics g)
    {
        int kFrameWidth = 5;
        int kBezelWidth = 2;

        System.out.println("Jetzt wird gezeichnet");

        // zeichne Window
        //setBackground (colorWindow);  // is method of super-super-class of Applet
        //g.clearRect(0,0,size().width,size().height);
        // not used here to reduce flicker
        // but clear left side of screen
        setBackground (colorWindow);
        g.clearRect(0,0,150,size().height);

        // Buttons werden automatisch gezeichnet
        // aber plaziere Ihn am unteren Bildrand
        itsButtonShot.reshape(40, size().height - 40, 90, 20);
        int sizeYStrength = Math.min (size().height - 180, 150);
        int posYStrength = size().height - 20 - sizeYStrength;
        itsStrength.reshape(15,posYStrength, 15,sizeYStrength);

        // zeichne Rahmen, draw it yourself
        for (int i = 0; i < kFrameWidth; i++)
        {
            g.setColor (colorFrameLight);
            g.drawLine (i, i, size().width -i, i);
            g.drawLine (i, i, i, size().height -i);
            g.setColor (colorFrameDark);
            g.drawLine (size().width -i, i, size().width -i, size().height -i );
            g.drawLine (i,  size().height -i, size().width -i, size().height -i);
        }

        // 'Zeichne' Text
        g.setColor(Color.black);
        g.setFont(textFont);
        g.drawString ("Bahn: "+itsBahnverwalter.currentBahnIndex(), 40, 120);
        g.drawString ("Par: ", 40, 140);
        g.drawString ("Shot: 1", 40, 160);
        g.drawString ("Strength", 40, posYStrength +20);


        // zeichne Bezel ausserhalb der Zeichenfläche, use 3D method
        g.setColor (colorWindow);
        for (int i = 0; i < kBezelWidth; i++)
        {
            g.draw3DRect (150-i-1, 15-i-1, size().width -165+2*(i+1), size().height -30+2*(i+1), false);
        }

        // teste auf Exception und zeichne entsprechende Zeichenfläche
        if (itsBahnverwalter.eineBahnHatException())
            drawExceptionField(g, 150, 15, size().width -15-150+1, size().height -15-15+1);
        else
            drawPlayField(g, 150, 15, size().width -15-150+1, size().height -15-15+1);
    };


    private void drawExceptionField(Graphics g, int fieldPosX, int fieldPosY, int fieldWidth, int fieldHeight)
    {
        // for combining Fontstyle, just add the 'constants'
        Font f = new Font("TimesRoman", Font.BOLD + Font.ITALIC, 14);
        FontMetrics metrics = getFontMetrics(f);

        g.setColor(colorException);
        g.fillRect(fieldPosX, fieldPosY, fieldWidth, fieldHeight);

        // use predefined red color
        g.setColor(Color.red);
        g.setFont(f);
        int posX = 10;
        int posY = 25 + metrics.getHeight();
        if (itsBahnverwalter.bahnException() != null) {
            for (int i=0; i<itsBahnverwalter.bahnException().length; i++) {
                g.drawString (itsBahnverwalter.bahnException()[i], fieldPosX + posX, fieldPosY + posY);
                posY += metrics.getHeight();
            };
        };

    }

    private void drawPlayField(Graphics g, int fieldPosX, int fieldPosY, int fieldWidth, int fieldHeight)
    {
        g.setColor(colorPlayfield);
        g.fillRect(fieldPosX, fieldPosY, fieldWidth, fieldHeight);

        if (itsBahnverwalter.currentBahn() != null)
            itsBahnverwalter.currentBahn().draw (g, fieldPosX, fieldPosY);
        itsBahnverwalter.itsBall.repaint();
        itsBahnverwalter.itsBall.drawArrow(g);
    }


    public void myButtonPushed (MyButton aPressedButton) {
        // Irgend ein Button wurde gedrückt
        // suche welcher
        // teste Reset Button
        if (aPressedButton == itsButtonReset) {
            System.out.println("Reset!!");
            itsBahnverwalter.resetCurrentBahn();
            //zeichne die gewählte Bahn
            repaint ();
        }
        // teste Next Button
        else if (aPressedButton == itsButtonNext) {
            System.out.println("Next!!");
            itsBahnverwalter.selectNextBahn();
            itsBahnverwalter.resetCurrentBahn();
            System.out.println("Neue Bahn: "+ itsBahnverwalter.currentBahnIndex());
            //zeichne die gewählte Bahn
            repaint ();
        }
        // teste Previous Button
        else if (aPressedButton == itsButtonPrev) {
            System.out.println("Previous!!");
            itsBahnverwalter.selectPrevBahn();
            itsBahnverwalter.resetCurrentBahn();
            System.out.println("Neue Bahn: "+ itsBahnverwalter.currentBahnIndex());
            //zeichne die gewählte Bahn
            repaint ();
        }
        // teste Shot Button
        else if (aPressedButton == itsButtonShot) {
            System.out.println("Schuss!!, Stärke: " + (100-itsStrength.getValue()));
            itsBahnverwalter.itsBall.setSpeedPercent(100-itsStrength.getValue());
            itsBahnverwalter.itsBall.start();
        };

    };

    public boolean mouseMove(Event evt, int x, int y) {
        if (itsBahnverwalter.itsBall.newDirectionSet(x,y)) {
            this.repaint(itsBahnverwalter.itsBall.getPos().x-30,itsBahnverwalter.itsBall.getPos().y-30,60,60);
        };
       	return false;
    }

}
