package com.hexabeast.lasermonster;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class GameScreen implements Screen 
{
	public static SpriteBatch batch;

	static OrthographicCamera cam;
	
	Monster monster;
	public static ArrayList<Mob> mobs = new ArrayList<Mob>();
	
	public static boolean end = false;
	public static boolean begin = false;
	
	float spawnrate = 0.9f;
	float currentTime = 0;
	
	static int killNumber = 0;
	
	float totaltime = 0;
	
	public static float health = 100;
	
	Button retry;
	Button start;
	
	public GameScreen()
	{

	}
	
	@Override
	public void show() 
	{
		monster = new Monster();
		
		retry  = new Button(30,30,Main.texRetry);
		
		start  = new Button(Main.width/2-Main.texStart.getWidth()/2,200,Main.texStart);
		
		batch = new SpriteBatch();
		cam = new OrthographicCamera(Main.width,Main.height);
		cam.position.set(Main.width/2,Main.height/2,0);
	}

	@Override
	public void render(float delta) 
	{
		if(health<0)
		{
			health = 0;
			end = true;
		}
		
		spawnrate -= Main.delta*0.015f; 
		if(spawnrate<0.1f)spawnrate=0.1f;
		
		currentTime+=Main.delta;
		
		if(!end)totaltime+=Main.delta;
		
		if(currentTime>spawnrate && !end && begin)
		{
			currentTime=0;
			
			double ran = Math.random();
			
			if(ran>0.75)mobs.add(new Mob(-130,150));
			else if(ran>0.5)mobs.add(new Mob(-130,675));
			else if(ran>0.25)mobs.add(new Mob(2050,150));
			else mobs.add(new Mob(2050,675));
		}
		
		
		Gdx.gl.glClearColor(0f,0f,0f,1f);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
			
			
		
		cam.update();
		batch.setProjectionMatrix(cam.combined);
		batch.begin();
			
		batch.draw(Main.texBackground,0,0);
		
		monster.draw();
		
		for(int i = mobs.size()-1; i>=0; i--)
		{
			mobs.get(i).draw();
			if(mobs.get(i).isDead)mobs.remove(i);
		}
		
		Collections.sort(mobs, new Comparator<Mob>() {
	        @Override
	        public int compare(Mob  mob1, Mob  mob2)
	        {
	            return  (int) ((mob1.y - mob2.y)*1000);
	        }
	    });
		
		
		if(retry.isTouched() && end)retry();
		
		if(!begin)
		{
			if(start.isTouched())
			{
				begin = true;
				start.fadeout = true;
			}
		}
		
		
		monster.drawLasers();
		
		batch.draw(Main.texForeground,0,0);
		
		Main.font.draw(batch, String.valueOf(killNumber), 50, 1030);
		
		retry.draw(!end);
		start.draw(false);
		
		batch.end();
	}
	
	public void retry()
	{
		monster = new Monster();
		
		mobs = new ArrayList<Mob>();
		
		end = false;
		
		spawnrate = 0.9f;
		currentTime = 0;
		
		killNumber = 0;
		
		totaltime = 0;
		
		health = 100;
	}
	
	

	@Override
	public void resize(int width, int height) 
	{
		
	}

	@Override
	public void pause() 
	{
		
	}

	@Override
	public void resume() 
	{
		
	}

	@Override
	public void hide() 
	{
		
	}

	@Override
	public void dispose() 
	{
		
	}
	
	public static Vector2 getAbsolutePos()
	{
		Vector3 worldCoord = new Vector3();
		worldCoord.x = Gdx.input.getX();
		worldCoord.y = Gdx.input.getY();
		worldCoord.z = 0;
		GameScreen.cam.unproject(worldCoord);
		return new Vector2(worldCoord.x,worldCoord.y);
	}

}
