package hello;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.IOException;

public class mySprite
{
    private Sprite sprt;
    private Image img;
    private double posX;
    private double posY;
    private int rows;
    private int cols;
    private int type = 0;
    private boolean updated;
    private boolean active = false;
    private double velx, vely;
    private double rotation;
    private int refpixx;
    private int refpixy;
    private int counter = 0;
    private int resetCounter = 0;
    private boolean isCharacter = false;

    public mySprite(String filename, int width, int height)
    {
        try{
            img = Image.createImage(filename);
            sprt = new Sprite(img, width, height);
            cols = sprt.getWidth()/width;
            rows = sprt.getHeight()/height;
        } catch (IOException exception)
        {
            exception.printStackTrace();
        }
       // System.out.println("Done creating sprite " + filename);
    }

    public void setType(int type)
    {
        this.type = type;
    }

    public int getType()
    {
        return type;
    }

    public boolean isUpdated()
    {
        return updated;
    }

    public void setActive()
    {
	active = true;
    }

    public void setInactive()
    {
	active = false;
    }

    public boolean isActive()
    {

        return active;
    }

    public double getPosx()
    {
	return sprt.getRefPixelX();
    }

    public void setPosx(double posx)
    {
        sprt.setRefPixelPosition((int)posx, sprt.getRefPixelY());
	updated = true;
    }

    public double getPosy()
    {
	return sprt.getRefPixelY();
    }

    public void setPosy(double posy)
    {
	sprt.setRefPixelPosition(sprt.getRefPixelX(), (int)posy);
	updated = true;
    }

    public void setLocation(double x, double y)
    {
	sprt.setRefPixelPosition((int)x, (int)y);
	updated = true;
    }

    public void move(double dx, double dy)
    {
	setPosx(getPosx()+dx);
	setPosy(getPosy()+dy);
    }

    public double getVelx()
    {
	return velx;
    }

    public void setVelx(double velx)
    {
	this.velx = velx;
    }

    public double getVely()
    {
	return vely;
    }

    public void setVely(double vely)
    {
	this.vely = vely;
    }

    public void setVelocity(double velx, double vely)
    {
	setVelx(velx);
	setVely(vely);
    }

    public double getSpeed()
    {
        return Math.sqrt(velx*velx+vely*vely);
    }

    public void setDirection(double angle)
    {
	double speed = Math.sqrt(velx*velx+vely*vely);
	velx = speed * Math.cos(angle);
	vely = -speed * Math.sin(angle);
    }

    public void turn(double angleInRads)
    {
	//double dir = getDirection();
	//dir += angleInRads;
	//setDirection(dir);
    }

    public double getRotation()
    {
	return rotation;
    }

    public void setRotation(double rotation)
    {
	this.rotation = rotation;

        if(rotation == 90)
            sprt.setTransform(sprt.TRANS_ROT90);
        else if (rotation == 180)
            sprt.setTransform(sprt.TRANS_MIRROR);
        else if (rotation == 270)
            sprt.setTransform(sprt.TRANS_MIRROR_ROT90);
        else
            sprt.setTransform(sprt.TRANS_NONE);

	updated = true;
    }

    public void draw(Graphics g)
    {
	if(active)
            sprt.paint(g);
       // System.out.println("mySprite Draw");
    }

    public void update(GameCanvas l)
    {
	if(!active) return;

        if(counter == 0)
        {
            setPosx(getPosx() + velx);
            setPosy(getPosy() + vely);
            counter = 15;
        }
        else
            counter--;

        if(isCharacter)
        {
            if(getPosx()<0)
            {
                setPosx(0);
                setVelx(-velx);
            }
            else if((getPosx()+sprt.getWidth())>l.getWidth())
            {
                setPosx(l.getWidth()-sprt.getWidth());
                setVelx(-velx);
            }

            if(getPosy()<0)
            {
                setPosy(0);
                setVely(-vely);
            }
            else if((getPosy()+sprt.getHeight())>l.getHeight())
            {
                setPosy(l.getHeight()-sprt.getHeight());
                setVely(-vely);
            }
        }
    }

    public void isCharacter()
    {
        isCharacter = true;
    }

    public double getWidth()
    {
	return sprt.getWidth();
    }

    public double getHeight()
    {
	return sprt.getHeight();
    }

    public void setRefPix(int x, int y)
    {
        sprt.defineReferencePixel(x, y);
    }

    public void collisionArea(int x, int y, int w, int h)
    {
        sprt.defineCollisionRectangle(x, y, w, h);
    }

    public boolean collision(mySprite s, boolean pixel)
    {
        if(s != null)
        {
            if(sprt.collidesWith(s.sprt, pixel))
            {
                return true;
            }
            else
                return false;
        }
        else
            return false;
    }
}
