package bullithell.panels;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JPanel;
import javax.swing.Timer;

import bullithell.images.Images;
import bullithell.objects.Player;
import bullithell.objects.missiles.Ball;
import bullithell.objects.missiles.BombMissile;
import bullithell.objects.missiles.Missile;

@SuppressWarnings("serial")
public class ContentPanel extends JPanel {
	Timer framestimer, gameloop;
	Player player;
	ArrayList<Missile> missiles;
	long oldtime;
	
	public ContentPanel(){
		player = new Player(400,700);
		missiles = new ArrayList<Missile>();
		this.addKeyListener(player);
		this.setFocusable(true);

		gameloop = new Timer(1000/30, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				player.reCalculate();
				if(Math.random()*1000 > 900){
					missiles.add(new BombMissile((int)(Math.random()*800), 10, "slow"));
				}
				Iterator<Missile> i = missiles.iterator();
				while(i.hasNext()){
					Missile m = i.next();					
					if(m.outOfScreen(0, 0, 800, 800) || m.getExplodeState() == 11){
						i.remove();
					}else if(player.missileIntersect(m)){
						m.explode(false);
					}else if(player.intersect(missiles)){
					}
					
					m.recalculate();
				}
				shootBalls();
				
				

			}
		});
		//update 60fps
		framestimer = new Timer(1000/60, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				repaint();
			}
		});
		framestimer.start();
		gameloop.start();
		

	}
	public void shootBalls(){
		if(System.currentTimeMillis() - oldtime > 5000){
		oldtime = System.currentTimeMillis();
		for(int i1 = -9; i1 < 9; i1++){
			missiles.add(new Ball(400, 10, Color.red, i1*0.8, Math.pow(i1, 2)*0.05+4));
		}
		}
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		
		//g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		
		g2.drawImage(Images.background, 0,0,getWidth(), getHeight(),null);
		for(Missile m: missiles){
			m.draw(g2);
		}
		player.draw(g2);
		
		//player statics menu
		g2.setFont(new Font("FreeSans", Font.BOLD, 20));
		g2.setPaint(new GradientPaint(new Point((int)(150),(int)(getHeight()-35)), Color.red.darker(),new Point((int)(150),(int)(getHeight()-15)) , Color.red));
		g2.drawString("Health", 40, getHeight()-20);
		g2.fillRect(150, getHeight()-35, player.getHealth(), 20);
		g2.setPaint(new GradientPaint(new Point((int)(150),(int)(getHeight()-35)), Color.blue.darker(),new Point((int)(150),(int)(getHeight()-15)) , Color.blue));
		g2.drawString("Shield", 340, getHeight()-20);
		g2.fillRect(450, getHeight()-35, player.getShieldpower(), 20);
		g2.setColor(Color.WHITE);
		g2.drawString("Level 1", 640, getHeight()-20);
		g2.drawString("Score: 4000", 40, 40);
		g2.drawString("Upgrade available", 550, 40);
		
		Toolkit.getDefaultToolkit().sync();
	}
}
