package unit;

import java.awt.Image;
import java.awt.image.ImageObserver;

import scenery.Scenery;

import bounds.Bounds;
import coords.Coords;
import coords.CharaPath;
import gUI.PirateNinjaBoxerPanel;
import gUI.MoveTimer;
import gUI.Mover;
import gUI.Camera;

public class Unit extends ID implements Mover {

    private double mL = 10;
    public Bounds bounda = null;
    public Coords placement;
    public Coords lastplacement;
    public Coords[] path = new Coords[1];
    private CharaPath nextpath;
    public int pathplace = 0;
    public int lastplace = -1;
    public double width = 50.0;
    public double height = 100.0;
    //public PirateNinjaBoxerPanel pP;
    //protected MoveTimer _timer;
    public Image[] imagear = null;
    public int imagearraynum = 0;

    public Unit(Coords placementa, PirateNinjaBoxerPanel aP, String idR, String idP, Image[] apa) {
        super("Unit", idR, idP);
        placement = placementa;
        lastplacement = placement;
        path[0] = placement;
        //pP = aP;
        //_timer = aP._timer;
        imagear = apa;
        bounda = new Bounds(this, apa);
    }

    public Unit(Coords placementa, PirateNinjaBoxerPanel aP, String idR, String idP) {
        super("Unit", idR, idP);
        placement = placementa;
        lastplacement = placement;
        path[0] = placement;
        //pP = aP;
        //_timer = _t;
        bounda = new Bounds(this);
    }

    //These ones are to be kept
    public Unit(Coords placementa, String idR, String idP, Image[] apa) {
        super("Unit", idR, idP);
        placement = placementa;
        lastplacement = placement;
        path[0] = placement;
        //pP = aP;
        //_timer = aP._timer;
        imagear = apa;
        bounda = new Bounds(this, apa);
    }

    public Unit(Coords placementa, String idR, String idP) {
        super("Unit", idR, idP);
        placement = placementa;
        lastplacement = placement;
        path[0] = placement;
        //pP = aP;
        //_timer = _t;
        bounda = new Bounds(this);
    }

    public void developPath(double xa, double ya, double x1, double y1, Scenery[] objects, Camera viewer) {
        //_timer.stop();
//		System.out.println(placement.getX() + " : " + placement.getY());
//		lastplacement = new Coords(placement);
        //placement = temp;
        //System.out.println(placement.getX() + " : " + placement.getY());



//		placement = new Coords(path[pathplace]);
        //System.out.println(placement.getX() + " : " + placement.getY());
//		Coords temp = new Coords(placement);
//		path = new Coords[1];
//		placement = new Coords(temp);
//		path[0] = placement;
//		Coords[] line = {new Coords(placement), new Coords(xa, ya)};
        this.nextpath = new CharaPath(new Coords(x1, y1), new Coords(xa, ya), this);
//		System.out.println("I've a found a path a!");
        this.nextpath.checkPath(this, objects);
//		System.out.println("I've a found a path b!");
        this.pathplace = 0;
        this.lastplace = -1;
        Coords[] toss = this.nextpath.getPath();
        this.path = toss;
//		this.placement = new Coords(this.path[0]);
        //_timer.start();
        return;
    }

    public void developPath(double xa, double ya, double x1, double y1) {
        //_timer.stop();
//		System.out.println(placement.getX() + " : " + placement.getY());
//		lastplacement = new Coords(placement);
        //placement = temp;
        //System.out.println(placement.getX() + " : " + placement.getY());



//		placement = new Coords(path[pathplace]);
        //System.out.println(placement.getX() + " : " + placement.getY());
//		Coords temp = new Coords(placement);
//		path = new Coords[1];
//		placement = new Coords(temp);
//		path[0] = placement;
//		Coords[] line = {new Coords(placement), new Coords(xa, ya)};
        this.nextpath = new CharaPath(new Coords(x1, y1), new Coords(xa, ya), this);
        this.pathplace = 0;
        this.lastplace = -1;
        Coords[] toss = this.nextpath.getPath();
        this.path = toss;
        this.placement = new Coords(this.path[0]);
        //_timer.start();
        return;
    }

    public double getMoveLen() {
        return mL;
    }

    public void move() {
//		//lastplacement = new Coords(placement);
//		while(pathplace <= path.length - 1)
//		{
//			placement = new Coords(path[pathplace]);
//			//System.out.println(placement.getX() + " : " + placement.getY());
//			pathplace ++;
//			break;
//		}
//		Coords[] toss = {placement};
//		path = toss;
//		pathplace = 0;
//		pP.repaint();

        lastplacement = new Coords(placement);
        try {
            placement = new Coords(path[pathplace]);
            pathplace++;
            lastplace++;
        } catch (Exception e) {
            pathplace--;
            lastplace--;
            placement = new Coords(lastplacement);
            Coords[] toss = new Coords[1];
            toss[0] = placement;
            path = toss;
        }

//		java.awt.Graphics2D g = (java.awt.Graphics2D) pP.getGraphics();
//		pP.repaint();
//		this.paintComponent(g, pP);
        //System.out.println(lastplacement.getX() + " : " + lastplacement.getY());
        //return;
        //System.out.println(lastplacement.getX() + " : " + lastplacement.getY());
//		if(pathplace < path.length -1)
//		{
//			lastplace = pathplace;
//			pathplace ++;
//			
//		}else if (pathplace >= path.length -1)
//		{
//			path = new Coords[1];
//			path[0] = new Coords(placement);
//			pathplace = 0;
//			lastplace = pathplace;
//		}
        this.bounda.rect.setLocation((int) this.placement.getX() - bounda.rect.width, (int) this.placement.getY() - this.bounda.rect.height);

    }

    public void unMove() {
//		//lastplacement = new Coords(placement);
//		while(pathplace <= path.length - 1)
//		{
//			placement = new Coords(path[pathplace]);
//			//System.out.println(placement.getX() + " : " + placement.getY());
//			pathplace ++;
//			break;
//		}
//		Coords[] toss = {placement};
//		path = toss;
//		pathplace = 0;
//		pP.repaint();

        lastplacement = new Coords(placement);
        try {
            placement = new Coords(path[pathplace]);
            pathplace--;
            lastplace = pathplace + 1;
        } catch (Exception e) {
            placement = new Coords(lastplacement);
            Coords[] toss = new Coords[1];
            toss[0] = placement;
            path = toss;
        }

//		java.awt.Graphics2D g = (java.awt.Graphics2D) pP.getGraphics();
//		pP.repaint();
//		this.paintComponent(g, pP);
        //System.out.println(lastplacement.getX() + " : " + lastplacement.getY());
        //return;
        //System.out.println(lastplacement.getX() + " : " + lastplacement.getY());
//		if(pathplace < path.length -1)
//		{
//			lastplace = pathplace;
//			pathplace ++;
//			
//		}else if (pathplace >= path.length -1)
//		{
//			path = new Coords[1];
//			path[0] = new Coords(placement);
//			pathplace = 0;
//			lastplace = pathplace;
//		}
        this.bounda.rect.setLocation((int) this.placement.getX() - bounda.rect.width, (int) this.placement.getY() - this.bounda.rect.height);

    }

    public void paintComponent(java.awt.Graphics2D bB, PirateNinjaBoxerPanel iO, Image[] imagea, Camera viewer) {

        
        int drawX = 10;
        int drawY = 0;
        
        if (iO.axesShow.getX() > 0) {
            drawX = (int) this.placement.flattenGlobal().getX();
            drawY = (int) this.placement.flattenGlobal().getY();
        } else if (iO.axesShow.getX() < 0) {
            drawX = (int) (iO.farRight - this.placement.flattenGlobal().getX());
            drawY = (int) this.placement.flattenGlobal().getY();
        } else {
            //TODO: Cover -Z 
            drawX = (int) this.placement.flattenGlobal().getZ();
            drawY = (int) this.placement.flattenGlobal().getY();
        }

        if (imagear == null) {
            //
//            if (iO.axesShow.getX() > 0) {
//                bB.drawRect((int) this.placement.flattenGlobal().getX(), (int) this.placement.flattenGlobal().getY(), 20, 20);
//            } else if (iO.axesShow.getX() < 0) {
                bB.drawRect(drawX-10, drawY-10, 20, 20);
                bB.fillRect(drawX-10, drawY-10, 20, 20);
//            } else {
//                bB.drawRect((int) this.placement.flattenGlobal().getZ(), (int) this.placement.flattenGlobal().getY(), 20, 20);
//            }

//            bB.drawRect((int)(this.bounda.getLeftBound() - viewer.getFarX()), (int)(this.bounda.getUpBound() - viewer.getFarY()), (int)(this.bounda.rect.width), (int)(this.bounda.rect.height));
        } else if (imagear != null) {

            bB.drawImage(getImage(imagea), (int) (this.bounda.getLeftBound() - viewer.getFarX()), (int) (this.bounda.getUpBound() - viewer.getFarY()), (int) this.bounda.rect.width, (int) this.bounda.rect.height, iO);
            if (imagearraynum < imagea.length) {
                imagearraynum++;
            } else if (imagearraynum >= imagea.length) {
                imagearraynum = 0;
            }
        }

        //- this.width - this.height
        try {
            //bB.drawLine((int)this.placement.getX(), (int)this.placement.getY(), (int)this.lastplacement.getX(), (int)lastplacement.getY());
//			for (int a = 0; a<path.length; a++)
//			{
//				//System.out.println(path[a].getX() + " : " + path[a].getY());
//				bB.drawLine((int)(path[a].getX() - viewer.getX()), (int)(path[a].getY() - viewer.getY()), (int)(path[a+1].getX() - viewer.getX()), (int)(path[a+1].getY() - viewer.getY()));
//				
//			}
        } catch (Exception e) {
        }

    }

    public Image getImage(Image[] imagea) {
        if (imagearraynum < imagea.length - 1) {
            imagearraynum++;
        } else {
            imagearraynum = 0;
        }
        return imagea[imagearraynum];
    }
}
