package GamePanels;
import Engine.*;

import javax.swing.*;
import java.awt.*;
//import java.awt.image.BufferedImage;
import java.net.URL;

/**
 * Created by sagi on 12/18/15.
 */
public class GamePanel extends JPanel implements Runnable{

    private final URL bgURL= getClass().getResource("/Images/bg.jpg");
    private final URL gameOverURL= getClass().getResource("/Images/gameOver.png");
    private final URL startURL= getClass().getResource("/Images/start.png");


    private GameEngine engine;
    private JLabel lbl_score, lbl_gameOver, lbl_bg, lbl_start;

    private ImageIcon img_bg, img_go, img_start;
    private Image bg_image;



    /**
     * 
     * Constructor.
     * initializes all objects on panel and creates gameEngine
     */
    public GamePanel(int width, int height){
        this.setLayout(null);

        this.engine = new GameEngine(width, height);
        this.addMouseListener(engine);
        this.setFocusable(true);
        this.requestFocus();

        //set score label at top left corner
        this.lbl_score = new JLabel("");
        lbl_score.setBounds(15,15,width,30);


        //set Static background
        img_bg = new ImageIcon(bgURL);
        Image tmp_BG = img_bg.getImage();
        bg_image = tmp_BG.getScaledInstance(width, height,  Image.SCALE_SMOOTH);
        img_bg = new ImageIcon(bg_image);
        lbl_bg = new JLabel(img_bg);
        lbl_bg.setBounds(0,0,width,height);

        //create "start game" & "game over" labels
        img_start = new ImageIcon(startURL);
        lbl_start = new JLabel(img_start);
        lbl_start.setVisible(true);
        lbl_start.setBounds(0,0,width,height);
        this.add(lbl_start);

        img_go = new ImageIcon(gameOverURL);
        lbl_gameOver = new JLabel(img_go);
        lbl_gameOver.setVisible(false);
        lbl_gameOver.setBounds(0,0,width,height);
        this.add(lbl_gameOver);


        lbl_score.setFont(new Font("Ariel", Font.BOLD, 24));
        lbl_score.setForeground(Color.BLACK);
        this.add(lbl_score);
        repaint();


    }

    @Override
    public void run() {

        while(engine.gameOn){
            engine.update();
            
            //check if game is over and draw labels accordingly
            if(engine.isGameOver()){
                if(!engine.isFirstGame)
                this.lbl_gameOver.setVisible(true);
            }else{
                this.lbl_gameOver.setVisible(false);
                lbl_start.setVisible(false);

            }
            //render graphics
            engine.render(this);



            repaint();
            
            //sleep for other processes to take control
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void addNotify(){
        super.addNotify();
        (new Thread(this)).start();
    }


    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;

        g.drawImage(bg_image, 0, 0, this); //draw the background

        g2d.drawImage(engine.getScene(),0,0,this); //Draw the scene


        
        //Update Score
        lbl_score.setText("SCORE : " + engine.getScore() + "\tHIGH SCORE : " + engine.getHeighScore());
//        g.dispose();
    }


}
