import java.awt.Color;
import java.awt.Polygon;
import java.awt.Graphics;
import Ship;
import Explosion;

public class Zephi extends Flying
{
 Polygon   shape1; // Kroppen
 Polygon   shape2; // Bakdelen
 Polygon   backSprite;
 Explosion explosion;
 Ship      ship;
 int    aWidth;
 int    aHeight;
 int    counter;
 double speedX;
 double speedY;
 public double      middleX;
 public double      middleY;
 public Polygon     sprite;
 public int         shield;
 public int         zLevel;
 public boolean     shown;
 public boolean     crashing;
 public boolean     hitting;

 public Zephi(int gameWidth, int gameHeight, Ship ship)
 {
  zLevel    = 0;
  aWidth    = gameWidth;
  aHeight   = gameHeight;
  shown     = false;
  crashing  = false;
  hitting   = false;
  shield    = 0;
  this.ship = ship;
  explosion = new Explosion();
 }

 public void start()
 {
  if(!shown && !explosion.exploding)
  {
   shape1 = new Polygon();
   shape1.addPoint(-12, 8);
   shape1.addPoint(-8, 14);
   shape1.addPoint(0, 16);
   shape1.addPoint(8, 14);
   shape1.addPoint(12, 8);
   shape1.addPoint(9, -11);
   shape1.addPoint(4, -16);
   shape1.addPoint(-4, -16);
   shape1.addPoint(-9, -11);
   shape2 = new Polygon();
   shape2.addPoint(-12, 8);
   shape2.addPoint(-8, 14);
   shape2.addPoint(0, 16);
   shape2.addPoint(8, 14);
   shape2.addPoint(12, 8);
   shape2.addPoint(8, 2);
   shape2.addPoint(0, 0);
   shape2.addPoint(-8, 2);

   zLevel    = 85;
   shown     = true;
   crashing  = false;
   shield    = 6;
   speedX    = 0;
   speedY    = 0;
   counter   = 0;

   switch((int)(Math.random() * 4))
   {
    case 0:
     middleX = - 20;
     middleY = Math.random() * aHeight;
     break;
    case 1:
     middleX = aWidth + 20;
     middleY = Math.random() * aHeight;
     break;
    case 2:
     middleX = Math.random() * aWidth;
     middleY = - 20;
     break;
    default:
     middleX = Math.random() * aWidth;
     middleY = aHeight + 20;
   }
   render();
  }
 }

 public void move()
 {
  if(shown)
  {
   if(!crashing)
   {
    if(counter == 4 && zLevel > 50)
    {
     zLevel  -= 1;
     counter = 0;
    }
    else
    {
     counter += 1;
    }
    middleX += (aWidth/2 - middleX) / (zLevel - 49) / 4;
    middleY += (aHeight/2 - middleY) / (zLevel - 49) / 4;
    if(!hitting
     && Math.pow((int)middleX-aWidth/2, 2)
      + Math.pow((int)middleY-aHeight/2, 2)
      <= 1600)
    {
     hitting = true;
    }
    else if(hitting)
    {
     crash();
    }
   }
   else
   {
    middleX += (aWidth/2 - middleX) / (zLevel - 49)*0.2;
    middleY += (aHeight/2 - middleY) / (zLevel - 49)*0.2;
    explosion.middleX = (int)middleX;
    explosion.middleY = (int)middleY;
    if(explosion.stadium >= 40)
    {
     shown = false;
    }
   }
   render();
  }
  explosion.go();
 }

 public void paint(Graphics g)
 {
  if(shown)
  {
   g.setColor(new Color(0xdddddd));
   g.fillPolygon(sprite);
   g.setColor(new Color(0xbbbbaa));
   g.fillPolygon(backSprite);
   g.setColor(new Color(0x999999));
   g.drawPolygon(sprite);
   g.drawPolygon(backSprite);
   g.drawLine(backSprite.xpoints[5], backSprite.ypoints[5],
    sprite.xpoints[6], sprite.ypoints[6]);
   g.drawLine(backSprite.xpoints[7], backSprite.ypoints[7],
    sprite.xpoints[7], sprite.ypoints[7]);
  }
  explosion.paint(g);
 }

 public void paintBar(Graphics g)
 {
  if(shown && shield != 0 && zLevel > 25)
  {
   g.setColor(Color.black);
   g.fillRect((int)middleX - 5, (int)middleY - 20, 10, 3);
   if(shield == 6)
   {
    g.setColor(new Color(0x00AA00));
   }
   else if(shield >= 3)
   {
    g.setColor(new Color(0xBBBB00));
   }
   else
   {
    g.setColor(new Color(0xBB0000));
   }
   g.fillRect((int)middleX - 5, (int)middleY - 20, shield*10/6, 3);
   g.setColor(Color.gray);
   g.drawRect((int)middleX - 5, (int)middleY - 20, 10, 3);
  }
 }

 public void crash()
 {
  if(shown && !crashing)
  {
   Sounds.play(Sounds.enemy);
   crashing  = true;
   hitting   = false;
   shield    = 0;
   explosion.explode((int)middleX, (int)middleY, 40);
  }
 }

 public boolean hit(int impact)
 {
  if(shown)
  {
   shield -= impact;
   if(shield <= 0)
   {
    crash();
    return true;
   }
  }
  return false;
 }

 public boolean inside(int x, int y)
 {
  return shown && !crashing && zLevel > 25 && sprite.contains(x, y);
 }

 void render()
 {
  if(middleY == aHeight/2)
  {
   return;
  }
  sprite     = new Polygon();
  backSprite = new Polygon();
  double atan_k = Math.atan((aWidth/2-middleX)/(aHeight/2-middleY));
  double sin_a  = Math.sin(atan_k);
  double cos_a  = Math.cos(atan_k);
  if(middleY < aHeight/2)
  {
   sin_a = -sin_a;
   cos_a = -cos_a;
  }
  for(int i = 0; i < shape1.npoints; i++)
  {
   sprite.addPoint((int)(Math.round(shape1.xpoints[i]*cos_a
    + shape1.ypoints[i]*sin_a)*zLevel/100 + middleX),
    (int)(Math.round(shape1.ypoints[i]*cos_a
    - shape1.xpoints[i]*sin_a)*zLevel/100 + middleY));
  }
  for(int i = 0; i < shape2.npoints; i++)
  {
   backSprite.addPoint((int)(Math.round(shape2.xpoints[i]*cos_a
    + shape2.ypoints[i]*sin_a)*zLevel/100 + middleX),
    (int)(Math.round(shape2.ypoints[i]*cos_a
    - shape2.xpoints[i]*sin_a)*zLevel/100 + middleY));
  }
 }

}