import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.lang.Math;

import java.awt.*;
import javax.swing.*;

public class GameElement{
	private short x, y;
	private short xspeed, yspeed; //only for obstacles
	private byte health;
	private byte type;
	private long lastUpdate;
	//private boolean isIt;

	public GameElement(){
		x = y = xspeed = yspeed = 0;
		type = 127; // 0 is it, 1 is not it
		health = 0;
		lastUpdate = Long.MAX_VALUE - 20000;
	}

	public GameElement(GDelta d){
		applyDelta(d);
	}
    
    public void drawSelf(Graphics2D g){
	g.fillOval(x-5, y-5, 10, 10);
    }
    
	public void applyDelta(GDelta d){
		x = d.x;
		y = d.y;
		//health = d.health;
		type = d.type;
		//lastUpdate = System.currentTimeMillis();
	}
	
	
	public long lastUpdate(){
		return lastUpdate;
	}
	

	public GDelta getGDelta(){
		return new GDelta(null, (byte) 0, health, type, x, y);
	}
	
	/*
	 * public byte[] toByteArray(){ }
	 */
	
	public short getX(){
		return x;
	}

	public short getY(){
		return y;
	}


	public short setX(short x){
		this.x = x;
		return x;
	}

	public short setY(short y){
		this.y = y;
		return y;
	}
	
	public boolean getIsIt(){
		return getType() == 0;
	}
	
	public boolean setIsIt(boolean b){
		if(b)
			setType((byte) 0);
		else
			setType((byte) 1);
		return b;
	}
	
/*
	public byte getHealth(){
		return health;
	}

	public byte setHealth(byte b){
		this.health = b;
		return b;
	}
	
	public byte applyDamage(short damage){
		if(health - (byte) damage < (byte) -128)
			health = (byte) -128;
		if(health - (byte) damage > (byte) 127)
		 	health = (byte) 127;
		else
			health -= (byte) damage;
		return health;
    }
*/
	public int getType(){
		return type + 128;
	}

	public byte setType(int x){
		type = (byte) (x - 128);
		return type;
	}
/*	
	public short getXspeed(){
		if(type > 1)
			return xspeed;
		else
			return -1;
	}
	
	public short getYspeed(){
		if(type > 1)
			return yspeed;
		else
			return -1;
	}
	
	public short setXspeed(short xspeed){
		if(type > 1)
			return this.xspeed = xspeed;
		else
			return -1;
	}
	
	public short setYspeed(short yspeed){
		if(type > 1)
			return this.yspeed = yspeed;
		else
			return -1;
	}
*/
	public double getDistance(GameElement e){
		double ans = 0;
		if(e != null){
		double x2 = (double) e.getX();
		double y2 = (double) e.getY();
		ans = Math.sqrt( ((double)x - x2)*((double)x - x2) + ((double)y - y2)*((double)y - y2));
	}
		return ans;
	}
	

	//Movement
	//Check to see if moving will put player off screen
	//If so, do nothing
	
	//(0,0) is in the top-left corner, yes?
	
	public void moveLeft(){
		short currentX = getX();
		if(currentX - Followers.PLAYER_MOVE_DISTANCE > 0){
			setX((short)(currentX - Followers.PLAYER_MOVE_DISTANCE));
		}
	}
	
	public void moveRight(){
		short currentX = getX();
		if(currentX + Followers.PLAYER_MOVE_DISTANCE < BaseGame.MAX_X){
			setX((short)(currentX + Followers.PLAYER_MOVE_DISTANCE));
		}
	}
	
	public void moveUp(){
		short currentY = getY();
		if(currentY + Followers.PLAYER_MOVE_DISTANCE > 0){
			setY((short)(currentY - Followers.PLAYER_MOVE_DISTANCE));
		}	
	}
	
	public void moveDown(){
		short currentY = getY();
		if(currentY + Followers.PLAYER_MOVE_DISTANCE < BaseGame.MAX_Y){
			setY((short)(currentY + Followers.PLAYER_MOVE_DISTANCE));
		}
	}
}
