package project.scenes;

import pi.Controller;
import pi.Scene;
import pi.actor.Actor;
import pi.actor.Animation;
import pi.actor.Rectangle;
import pi.actor.Text;
import pi.event.FrameListener;
import pi.event.MouseButton;
import pi.event.MouseClickListener;
import pi.graphics.geom.Vector;
import pi.resources.sound.SoundPlayback;
import project.Main;

import static pi.Controller.fonts;

import java.awt.Font;


public class VictoryScene extends Scene implements FrameListener, MouseClickListener { 
    Font font;
    boolean faded_out;
    Text designer;
    Text pr;
    Text programmer;
    Rectangle rect;
    Actor sixseven;

    boolean clicked;
    boolean game_over_playing;

    public VictoryScene() {
        Controller.jukebox.playSound("sounds/reward.mp3");

        clicked = false;
        game_over_playing = false;

        font = fonts.get("fonts/BitcountGridDouble-Regular.ttf");

        designer = new Text("Design: Alf");
        designer.color("#ffffff");
        designer.font(font);
        designer.height(2.5);
        pr = new Text("PR: Kevon Faison");
        pr.color("#ffffff");
        pr.font(font);  
        pr.height(2.5);
        programmer = new Text("Programming: Benjamin Burkhardt");
        programmer.color("#ffffff");
        programmer.font(font);
        programmer.height(1.5);
        rect = new Rectangle(Main.WINDOW_WIDTH/camera().meter(), Main.WINDOW_HEIGHT/camera().meter()+5);
        rect.color("#4d0000");

        designer.center(0, 4);
        pr.center(0, 0);
        programmer.center(0, -4);
        rect.center(0, 0);

        add(designer);
        add(pr);
        add(programmer);
        add(rect);

        sixseven = Animation.createFromAnimatedGif("images/kebab-pack/reward.gif", 1.48*15, 1.25*15);
        sixseven.center(0, 0);

        rect.applyImpulse(new Vector(0, -1).multiply(67000));
        
        sixseven.angularVelocity(-0.1);

        add(sixseven);
    }

    @Override
    public void onFrame(double pastTime)
    {
        sixseven.center(rect.center());

        if(clicked) {
            for(SoundPlayback sp : Controller.jukebox.allSounds()) {
                if(sp.toString().contains("Mozart") || sp.toString().contains("reward")) {
                    sp.volume(sp.volume()*0.97);
                    if(sp.volume() < 0.01) {
                        if(!game_over_playing) {
                            Controller.jukebox.playSound("sounds/Game-Over.mp3");
                            game_over_playing = true;
                        }
                    }
                };
            }
            camera().zoomOut(0.01);
            
            if (camera().meter() < 0.5) {
                System.exit(0);
            };
        }
    }

    @Override
    public void onMouseDown(Vector position, MouseButton button)
    {
        sixseven.visible(false);
        rect.visible(false);

        clicked = true;
    }
}
