package doragame;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JFrame;

public abstract class Game {

    //Scaling everything to screen size
    public static final Dimension FRAME_DIM = Toolkit.getDefaultToolkit().getScreenSize();

    //All components of the graphics we're using.
    protected static BufferStrategy bs;
    protected static Graphics2D g2d;
    protected static JFrame frame;
    protected static Canvas canvas;

    //Handles the MouseListener and MouseMotionListener
    protected static MouseHandler mouse;

    //For random game elements
    protected static Random rnd = new Random();

    //In case an image is missing #robust
    protected static BufferedImage placeholder;

    protected Game() {
        try {
            placeholder = ImageIO.read(new File("placeholder.jpg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void hideCursor() {  //Sets the canvas cursor to a blank image
        BufferedImage blank = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(blank, new Point(canvas.getX(),
                canvas.getY()), "blank"));
    }

    public void setCursor(final String url) {   //Set the canvas cursor to whatever image you want
        Image image = Toolkit.getDefaultToolkit().getImage(url);
        canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(canvas.getX(),
                canvas.getY()), "img"));
    }

    public static BufferedImage loadImage(final String url) {   //My "wrapped" image loader
        BufferedImage image = placeholder;
        try {
            image = ImageIO.read(new File(url));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }

    public static void playSound(final String url) {    //Yet another "wrapper" only for audio
        try {
            Clip clip = AudioSystem.getClip();
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("sounds/" + url));
            clip.open(inputStream);
            clip.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
