package MissileCommand;




public class Turret extends GP.Graphics.FilledOval {

  
  private boolean _isSelected, _isValid;
  private Holder _holder;
  private GP.Graphic _gun;
  private GP.Graphics.Text _shotDisplay;
  private int _shotsLeft;
  private GP.Container _container;
  private GP.Attributes.Color _color, _selcolor, _guncolor, _dispcolor;


  public Turret(GP.Container container, Holder holder, GP.Attributes.Position pos, int shotsLeft) {
    super(container);
  
    _holder=holder;
    _container=container;
 
    _color=new GP.Attributes.Colors.Black();
    _selcolor=new GP.Attributes.Colors.Orange();
    _guncolor=new GP.Attributes.Colors.Yellow();
    _dispcolor=new GP.Attributes.Colors.White();

    this.SetColor(_color);
    this.SetSize(new GP.Attributes.Size(50,50));
    this.SetPosition(pos);

    double posx=pos.GetX();
    double posy=pos.GetY();

    _gun=new GP.Graphics.FilledRectangle(container);
    _gun.SetColor(new GP.Attributes.Colors.Black());
    _gun.SetSize(new GP.Attributes.Size(5,10));
    _gun.SetPosition(new GP.Attributes.Position(posx,posy-30));

    _shotsLeft=shotsLeft;

    _shotDisplay=new GP.Graphics.Text(container, ""+_shotsLeft+"");
    _shotDisplay.SetColor(new GP.Attributes.Colors.Black());
    _shotDisplay.SetPosition(new GP.Attributes.Position(posx,posy-10));

    _isSelected=false;
    _isValid=true;

  }


  public void UpdateShots(int numToAdd) {
    _shotsLeft +=numToAdd;
    _shotDisplay.SetString(""+_shotsLeft+"");
  }

  public void setColors() {
    _color=_holder.getColor();
    this.SetColor(_color);
    _gun.SetColor(_guncolor);
    _shotDisplay.SetColor(_dispcolor);
  }

  public void Attack(double toX, double toY) {
   
    if (_shotsLeft > 0 ) {
      GoodMissile gm=new GoodMissile(_container, this, _holder, toX, toY);
      gm.Start();
      this.UpdateShots(-1); // substract one shot
    }

  }
    
  public void Cycle() {
    if (_isValid) {

      if (_isSelected) {
	this.SetColor(_color);
	_isSelected=false;
      }
      else {
	this.SetColor(_selcolor);
	_isSelected=true;
	_holder.setCurrentTurret(this);
      }
    
    }

  }


  public void blowUp() {
    _shotDisplay.Hide();
    _shotsLeft=0;
    TurretExplosion te=new TurretExplosion(_container, this, _gun);
    te.Start();
  }

  public void Wreck() { 
    System.err.println("Wrecking."); 
    this.setValid(false);
    this.SetColor(new GP.Attributes.Color(47,79,79)); // take care of "stranded" selection colors
    
  }

  public void setValid(boolean newStatus) {
    _isValid=newStatus;
  }
  public boolean isValid() {
    System.err.println("Turret is valid? "+_isValid);
    return _isValid;
  }

}
