package doragame;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import javax.swing.JFrame;

//Simple navigational system
enum Scene {

    HOME, CITY, MOMS, PREPHEROIN, INJECT, FINAL
}

public class MainGame extends Game {

    //To display in window label
    public static final String NAME = "Dora the XXX-plorer";

    //Images
    BufferedImage homeImg, cityImg, momsImg, valleyImg, doraDefault, doraCool, doraSneaky;

    //All "clickables"
    Item spoonHeroin, lighterSyringe, rightArrow, leftArrow, lighter, spoon, leftArm, rightArm;

    //To know which scene is currently displaying
    Scene currentScreen = Scene.HOME;

    //To know when items are collected
    private boolean hasSpoonHeroin = false;
    private boolean hasLighterSyringe = false;

    public void startGame() {
        //Decided to group loading of images as this provides structure as opposed to exclusively loading them when used
        homeImg = loadImage("scenes/home.jpg");
        cityImg = loadImage("scenes/city.jpg");
        momsImg = loadImage("scenes/momsHouse.jpg");
        valleyImg = loadImage("scenes/catchTheDragon.png");

        doraDefault = loadImage("dora/doraDefault.png");
        doraCool = loadImage("dora/doraCool.png");
        doraSneaky = loadImage("dora/doraSneaky.png");

        BufferedImage rightImg = loadImage("arrows/rightArrow.png");
        BufferedImage leftImg = loadImage("arrows/leftArrow.png");
        BufferedImage leftArmImg = loadImage("arms/syringeArm.png");
        BufferedImage rightArmImgOpen = loadImage("arms/veinArmOpen.png");

        BufferedImage spoonHeroinImg = loadImage("items/spoonHeroin.png");
        BufferedImage lighterSyringeImg = loadImage("items/lighterSyringe.png");
        BufferedImage lighterImg = loadImage("items/lighterNoFlame.png");
        BufferedImage spoonImg = loadImage("items/spoon.png");

        //Navigational
        rightArrow = new Item(rightImg, FRAME_DIM, new Point(FRAME_DIM.width - rightImg.getWidth() / 2, FRAME_DIM.height / 2));
        leftArrow = new Item(leftImg, FRAME_DIM, new Point(leftImg.getWidth() / 2, (FRAME_DIM.height / 2)));

        //Items
        spoonHeroin = new Item(spoonHeroinImg, FRAME_DIM, new Point(FRAME_DIM.width / 2, FRAME_DIM.height / 2));
        lighterSyringe = new Item(lighterSyringeImg, FRAME_DIM, new Point(FRAME_DIM.width / 2, FRAME_DIM.height / 2));
        lighter = new Item(lighterImg, FRAME_DIM);
        spoon = new Item(spoonImg, FRAME_DIM, new Point(FRAME_DIM.width / 2, FRAME_DIM.height / 2));

        leftArm = new Item(leftArmImg, FRAME_DIM);
        rightArm = new Item(rightArmImgOpen, FRAME_DIM, new Point(rightArmImgOpen.getWidth() / 2, 0 + FRAME_DIM.height / 2));

        //All setup for frame, canvas and bufferstrategy is only necessary once as they
        //are statically inherited from game class
        frame = new JFrame(NAME);
        frame.setIgnoreRepaint(true);   //Don't want the operating system to try and paint the image, that's our job
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        canvas = new Canvas();
        canvas.setIgnoreRepaint(true);  //Ditto
        canvas.setSize(FRAME_DIM);

        frame.add(canvas);
        frame.pack();
        frame.setVisible(true);

        canvas.createBufferStrategy(2); //E.g. "page-flipping" which is kinda like double-buffering
        bs = canvas.getBufferStrategy();

        setCursor("arrows/cursor.png");

        //Start listening for input
        mouse = new MouseHandler(canvas);
        playSound("start.wav");
        render();
    }

    public void render() {
        while (true) {
            try {
                g2d = (Graphics2D) bs.getDrawGraphics();
                //Start drawing stuff
                switch (currentScreen) {
                    case HOME:
                        g2d.drawImage(homeImg, 0, 0, FRAME_DIM.width, FRAME_DIM.height, null);

                        g2d.drawImage(doraDefault, FRAME_DIM.width / 2, FRAME_DIM.height / 2, null);

                        //Nav arrows
                        rightArrow.draw(g2d);
                        leftArrow.draw(g2d);

                        //Check hitboxes
                        if (rightArrow.getHitbox().contains(mouse.getClick())) {
                            currentScreen = Scene.MOMS;
                            playSound("enterHouse.wav");
                        } else if (leftArrow.getHitbox().contains(mouse.getClick())) {
                            currentScreen = Scene.CITY;
                        }

                        if (hasSpoonHeroin && hasLighterSyringe) {
                            hideCursor();
                            currentScreen = Scene.PREPHEROIN;
                        }
                        break;
                    case CITY:
                        g2d.drawImage(cityImg, 0, 0, FRAME_DIM.width, FRAME_DIM.height, null);

                        g2d.drawImage(doraCool, FRAME_DIM.width / 2, FRAME_DIM.height / 2, null);

                        rightArrow.draw(g2d);

                        //Item
                        if (!hasSpoonHeroin) {
                            spoonHeroin.draw(g2d);
                        }

                        if (rightArrow.getHitbox().contains(mouse.getClick())) {
                            currentScreen = Scene.HOME;
                        } else if (!hasSpoonHeroin && spoonHeroin.getHitbox().contains(mouse.getClick())) {
                            hasSpoonHeroin = true;
                            playSound("spoonHeroin.wav");
                        }
                        break;
                    case MOMS:
                        g2d.drawImage(momsImg, 0, 0, FRAME_DIM.width, FRAME_DIM.height, null);

                        g2d.drawImage(doraSneaky, FRAME_DIM.width / 2, FRAME_DIM.height / 2, null);

                        leftArrow.draw(g2d);

                        if (!hasLighterSyringe) {
                            lighterSyringe.draw(g2d);
                        }

                        if (leftArrow.getHitbox().contains(mouse.getClick())) {
                            currentScreen = Scene.HOME;
                        } else if (!hasLighterSyringe && lighterSyringe.getHitbox().contains(mouse.getClick())) {
                            hasLighterSyringe = true;
                            playSound("lighterSyringe.wav");
                        }
                        break;
                    case PREPHEROIN:
                        g2d.drawImage(valleyImg, 0, 0, FRAME_DIM.width, FRAME_DIM.height, null);

                        if (spoon.getHitbox().contains(mouse.getClick())) {
                            lighter.setImage(ImageIO.read(new File("items/lighterFlame.png")));

                            playSound("noice.wav");

                            spoon.draw(g2d);
                            lighter.draw(g2d);
                            bs.show();
                            Thread.sleep(3000);

                            currentScreen = Scene.INJECT;
                        }

                        spoon.draw(g2d);
                        lighter.setPos(mouse.getHover());
                        lighter.draw(g2d);
                        break;
                    case INJECT:
                        g2d.drawImage(valleyImg, 0, 0, FRAME_DIM.width, FRAME_DIM.height, null);

                        if (rightArm.getHitbox().contains(mouse.getClick())) {
                            rightArm.setImage(ImageIO.read(new File("arms/veinArmClosed.png")));
                            g2d.setColor(Color.BLACK);
                            g2d.fillRect(0, 0, FRAME_DIM.width, FRAME_DIM.height);

                            bs.show();

                            mouse.reset();

                            new HeroinGame().start(valleyImg, leftArm, rightArm);
                        }

                        rightArm.draw(g2d);

                        leftArm.setX(mouse.getHover().x - leftArm.getImg().getWidth());
                        leftArm.setY(mouse.getHover().y);

                        leftArm.draw(g2d);

                        break;

                    default:
                        System.out.println("Lel, error. Resetting to home screen.");
                        currentScreen = Scene.HOME;
                }

                //Reset click
                mouse.reset();

                if (!bs.contentsLost()) {
                    bs.show();  //Display the rendered graphics using our bufferstrategy
                }
                Thread.yield(); //Give system time to do other stuff

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (g2d != null) {
                    g2d.dispose();  //Clear the graphics object
                }
            }
        }
    }

}
