/*
    Moenagade
    °°°°°°°°°

    Moenagade is a graphical programming tool for beginners. For this
    reason it is rather limited in its functionality. It aims to help
    students to gain knowledge about the programming structures. Due
    to the explicit conversion to real and executable Java code, it 
    should help to make an easy from block programming to coding.

    Copyright (C) 2009  Bob Fisch

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or any
    later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package moenagade.base;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.sound.sampled.Clip;
import javax.swing.Timer;
import java.util.ArrayList;

/**
 *
 * @author robert.fisch
 */
public abstract class Entity implements KeyListener {
    
    private String imageFile = "";
    private Image image = null;
    
    //private Point location = new Point(0,0);
    private double x = 0;
    private double y = 0;

    private double vSpeed = 0;
    private double hSpeed = 0;
    private double vBounceFactor = 0;
    private double hBounceFactor = 0;
    private boolean usesGravity = false;
    private double frictionFactor = 0;

    private World world;
    
    private int width = 1;
    private int height = 1;

    private double rotation = 0;

    private String filename;
    
    private ArrayList<Timer> timers = new ArrayList<Timer>();
    private ArrayList<java.util.Timer> scheduled = new ArrayList<java.util.Timer>();
    
    public Entity(World world) {
        setWorld(world);
        //onCreate(); --> make this call in the classes constructor in order to be able to access local attributes
    }
    
    public Entity(World world, int x, int y) {
        setWorld(world);
        this.x=x;
        this.y=y;
        //onCreate();
    }
    
    protected void onCreate() 
    {
    }
    
    public void loadImage(String filename)
    {
        imageFile = "/moenagade/images/"+filename;
        // load image
        try 
        {
            image = ImageIO.read(this.getClass().getResource(imageFile));
            width= image.getWidth(null);
            height=image.getHeight(null);
            this.filename=filename;
        } 
        catch (IOException ex) 
        {
            ex.printStackTrace();
        }
    }

    public String getFilename()
    {
        return filename;
    }

    public void draw(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        g2.rotate(Math.toRadians(rotation), getWidth()/2, getHeight()/2);

        if(image!=null && x+getWidth()>0 && y+getHeight()>0)
        {
            g.drawImage(image, 0, 0, width, height, null);
        }
        onDraw(g);

        g2.rotate(-Math.toRadians(rotation), getWidth()/2, getHeight()/2);
   }

    public void bringToFront()
    {
        getWorld().bringToFront(this);
    }

    @Override
    public void keyTyped(KeyEvent ke) {
        
    }

    @Override
    public void keyPressed(KeyEvent ke) {
        
    }

    @Override
    public void keyReleased(KeyEvent ke) {
        
    }
    
    public void moveLeft(double pixels)
    {
        x-=pixels;
        onPositionChanged();
        if(getWorld()!=null) getWorld().repaint();
        getWorld().checkCollisions();
    }
    
    public void moveRight(double pixels)
    {
        x+=pixels;
        onPositionChanged();
        if(getWorld()!=null) getWorld().repaint();
        getWorld().checkCollisions();
    }

    public void moveUp(double pixels)
    {
        y-=pixels;
        onPositionChanged();
        if(getWorld()!=null) getWorld().repaint();
        getWorld().checkCollisions();
    }
    
    public void moveDown(double pixels)
    {
        y+=pixels;
        onPositionChanged();
        if(getWorld()!=null) getWorld().repaint();
        getWorld().checkCollisions();
    }

    public void moveForward(double pixels)
    {
        x=x-Math.cos(Math.toRadians(rotation))*pixels;
        y=y-Math.sin(Math.toRadians(rotation))*pixels;
        onPositionChanged();
        if(getWorld()!=null) getWorld().repaint();
        getWorld().checkCollisions();
    }
    
    public void moveBackward(double pixels)
    {
        x=x+Math.cos(Math.toRadians(rotation))*pixels;
        y=y+Math.sin(Math.toRadians(rotation))*pixels;
        onPositionChanged();
        if(getWorld()!=null) getWorld().repaint();
        getWorld().checkCollisions();
    }
    
    public Entity setX(double x)
    {
        this.x=x;
        onPositionChanged();
        if(getWorld()!=null) getWorld().repaint();
        getWorld().checkCollisions();
        return this;
    }
    
    public Entity setY(double y)
    {
        this.y=y;
        onPositionChanged();
        if(getWorld()!=null) getWorld().repaint();
        getWorld().checkCollisions();
        return this;
    }

    public double getX()
    {
        return x;
    }
    
    public double getY()
    {
        return y;
    }

    public Entity rotate(float rotation)
    {
        this.rotation+=rotation;
        onPositionChanged();
        if(getWorld()!=null) getWorld().repaint();
        getWorld().checkCollisions();
        return this;
    }

    public Entity setRotation(double rotation)
    {
        this.rotation=rotation;
        onPositionChanged();
        if(getWorld()!=null) getWorld().repaint();
        getWorld().checkCollisions();
        return this;
    }

    public Entity setRotation(float rotation)
    {
        setRotation((double) rotation);
        return this;
    }

    public double getRotation()
    {
        return rotation;
    }

    

    public void setWorld(World world) {
        this.world = world;
    }
    
    public World getWorld() {
        return world;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }
    
    public void setWidth(int width) {
        this.width=width;
        getWorld().repaint();
    }

    public void setHeight(int height) {
        this.height=height;
        getWorld().repaint();
    }

    public void setDimension(int width, int height)
    {
        this.width=width;
        this.height=height;
        getWorld().repaint();
    }

    public int getMouseX()
    {
        return world.getMouseX();
    }

    public int getMouseY()
    {
        return world.getMouseY();
    }

    protected void onMousePressed(java.awt.event.MouseEvent evt) {                                  
        
    }

    protected void onMouseReleased(java.awt.event.MouseEvent evt) {                                  
        
    }

    protected void onMouseMoved(java.awt.event.MouseEvent evt) {                                  
        
    }

    protected void onMouseDragged(java.awt.event.MouseEvent evt) {                                  
        
    }

    public boolean mouseInside()
    {
        return (getX()<=getMouseX() && getMouseX()<=getX()+getWidth() &&
                getY()<=getMouseY() && getMouseY()<=getY()+getHeight());
    }

    public void delete()
    {
        if(world!=null)
            world.removeEntity(this);
    
        for(int i=0; i<timers.size(); i++)
            if(timers.get(i).isRunning())
                timers.get(i).stop();
        
        for(int i=0; i<scheduled.size(); i++)
        {
            scheduled.get(i).cancel();
            scheduled.get(i).purge();
        }
    }
    
    public void addTimer(Timer timer)
    {
        timers.add(timer);
    }

    public void addSchedule(java.util.Timer timer)
    {
        scheduled.add(timer);
    }
    
    public Rectangle.Double getRectangle()
    {
        return new Rectangle.Double(x, y, getWidth(), getHeight());
    }
    
    public boolean isTouching(Entity other)
    {
        return this.getRectangle().intersects(other.getRectangle());
    }

    public void onTouched(Entity other) 
    {        
    }
    
    public void onOutOfWorld()
    {        
    }

    public void onTouchBoundary()
    {        
    }
    
    public void onPositionChanged()
    {        
    }
    
    public void onDraw(Graphics g)
    {        
    }

    public boolean isTouchingSomeone()
    {
        for (int i = 0; i < getWorld().getEntities().size(); i++) {
            Entity other = getWorld().getEntities().get(i);

            if(other!=this && this.isTouching(other))
            {
                return true;
            }
        }
        return false;
    }
    
    public boolean processTimer()
    {
        boolean goOn = false;

        // vertical (gravity)
        if(usesGravity && (!isTouchingSomeone() || (vSpeed!=0 && isTouchingSomeone())))
        {
            if(vSpeed + getWorld().getGravity() > getWorld().getMaxVelocity()) 
                vSpeed=getWorld().getMaxVelocity();
            else 
                vSpeed = vSpeed + getWorld().getGravity();
            y+=vSpeed;

            for (int i = 0; i < getWorld().getEntities().size(); i++) {
                Entity other = getWorld().getEntities().get(i);
                
                if(other!=this && this.isTouching(other))
                {
                    y-=vSpeed;
                    vSpeed = (-vSpeed*vBounceFactor)+getWorld().getGravity();
                    if(vSpeed<0.5 && vSpeed>-0.5) vSpeed=0;

                    if(getWorld()!=null && getWorld().getMain()!=null)
                    {
                        this.onTouched(other);
                        other.onTouched(this);
                    }
                }
            }

            goOn=true;
        }

        if(hSpeed!=0)
        {
            x=x+hSpeed;
            
            for (int i = 0; i < getWorld().getEntities().size(); i++) {
                Entity other = getWorld().getEntities().get(i);
                
                if(other!=this && this.isTouching(other))
                {
                    x-=hSpeed;
                    hSpeed = (-hSpeed*hBounceFactor);

                    if(getWorld()!=null && getWorld().getMain()!=null)
                    {
                        this.onTouched(other);
                        other.onTouched(this);
                    }
                }
            }
            
            hSpeed*=frictionFactor;
            if(hSpeed<0.5 && hSpeed>-0.5) hSpeed=0;
            
            goOn=true;
        }

        if(goOn)
        {
            getWorld().repaint();
        }

        return goOn;
    }

    public void setUsesGravity(boolean value)
    {
        usesGravity=value;
    }

    public void jump(double speed)
    {
        if(vSpeed==0)
            vSpeed=-speed;
    }

    public void setVSpeed(double speed)
    {
        vSpeed=speed;
    }

    public void setHSpeed(double speed)
    {
        hSpeed=speed;
    }

    public double getVSpeed()
    {
        return vSpeed;
    }

    public double getHSpeed()
    {
        return hSpeed;
    }

    public void setVBounceFactor(double bounce)
    {
        vBounceFactor=bounce;
    }

    public void setHBounceFactor(double bounce)
    {
        hBounceFactor=bounce;
    }

    public double getVBounceFactor()
    {
        return vBounceFactor;
    }

    public double getHBounceFactor()
    {
        return hBounceFactor;
    }

    public double getFrictionFactor() {
        return frictionFactor;
    }

    public void setFrictionFactor(double frictionFactor) {
        this.frictionFactor = frictionFactor;
    }
    
    
}
