import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.awt.Image;


public class Wizard extends Soldier {
    // OVERVIEW: A WizardInfantry is a soldier that performace a walk,
    //    moving one square in a direction toward enemy each step.
    //      Attacks enemies untill dead or victorius
    // Low Life, Low Damage, Slow, Special: cast attack spell

    
    private boolean inTurn; // Currently in the middle of a turn (used for coloring)
   
    private int magic;
    private int maxhealth;
    
    public Wizard () {
	inTurn = false;
        setFight(false);
        setHealth(35);
        setSpeed(500);
      //  setAttacknum(1); //default is 1
        setDamage(5);
        magic=0;
        
        maxhealth=getHealth();
       // color=c1;
        
       
    }
    
    
   
   
    public String getType()
    {   //Effect: returns soldier type
    return "Wizard";
    
    }
    
    
    
    
    public Color getColor()
    {   //Effect: returns soldier color or white if moving
	if (inTurn) {
	    return Color.white;
	} else {
	    return color;
	}
    } //@nowarn NonNullResult // ESC/Java doesn't know Color constants are not null
    
    
    
    public boolean Special()
    {
       // magic++;
        
        if(magic >= 5)
        {
             synchronized(getGrid())
              {
                  
            Spell spl= new Spell();
            spl.setTeam(getTeam());
            
            Vector vec = getVecEmptyNeighbors();
            if(vec==null||vec.size()<1)
            return false;
        
       
            SimObject s1 = (SimObject) vec.get(1);
             getGrid().setObjectAt(s1.getRow(),s1.getColumn(),spl);
        
             Thread simObjectThread = new Thread (spl);
             simObjectThread.start();
       
                      
       magic-=5;
       setExp(getExp()+1);
        
       System.out.println( toString()+ " cast spell" );
       return true;
        
        }
        }
        
     
        return false;
    }
    
    
    
    
    
    public void drawShield(Graphics g,int x, int y,int w, int h, double rat)
    {    //Effect: displays soldier graphic
        int x2=x;int y2=y;
        
        int m=(int)(4*rat);
        x2=x+w/3+m;
        y2=y+h/3-m;
        
        
         
        if(!inTurn)
        {
        m=(int)(4*rat);    
        //x2+=w/4;
        g.setColor(Color.black);
        g.fillRect(x2,y2+m,w/3-m,h/3);
        g.setColor(Color.yellow);
        
       // m-(int)(1*rat);
        g.drawRect(x2,y2+m,w/3-m,h/3);
        x2+=(int)(4*rat);
       // x2+=(int)(2*rat);
        x2+=(int)(3*rat);y2+=(int)(2*rat);
        g.drawRect(x2,y2+m,w/3-5-2*m,h/3-4);
         
        }
        else
        {
         m=(int)(4*rat);    
        x2+=w/4;
        x2-=2*m;
        g.setColor(Color.black);
        g.fillRect(x2,y2+m,w/3-m,h/3);
        g.setColor(Color.yellow);
        
       // m-(int)(1*rat);
        g.drawRect(x2,y2+m,w/3-m,h/3);
        x2+=(int)(4*rat);
       // x2+=(int)(2*rat);
        x2+=(int)(3*rat);y2+=(int)(2*rat);
        if(fight)
            g.setColor(Color.yellow.brighter());
        
        g.drawRect(x2,y2+m,w/3-5-2*m,h/3-4);
                    
        }
            
    }
    
    
    public void drawCloth(Graphics g,int x, int y,int w, int h, double rat)
    {
        int x2=x;int y2=y;
        
        y2+=h;
        y2-=h/3+h/6;
        
        x2+=w/6;
        
        g.setColor(getTeam());
        g.fillOval(x2,y2,w/2+w/7,h/3);
        g.setColor(getColor());
        g.drawArc(x2,y2,w/2+w/7,h/3,45,195);
        
    }
    
    
    
     public void drawWeapon( Graphics g,int x, int y,int w, int h, double rat)
    {    //Effect: displays soldier graphic
        
        int x2=x;int y2=y;
        
        int m=(int)(4*rat);
        x2=x+w/3+w/4;
        y2=y+h/2;
        
        int x3=x2;
        int y3=y2;
        
        if(fight)
        {
        x3+=w/3-(int)(2*rat);
        y3-=h/2-h/3;
           
        }
        else
        {
        x3+=w/3-(int)(2*rat);
        y3-=h/4-(int)(2*rat);
            
        }
        if((fight)&&(inTurn))
        {
        
        y3-=h/6;
        //y2-=w/5;
        x2-=w/5;
           
        }
        
        int x4=x2;
        int y4=y2;
        
        g.setColor(new Color(58,75,89));
        
        if(fight)
            g.setColor(Color.yellow);
            
        g.drawLine(x2,y2,x3,y3);
        x2+=(1*rat);
         y2+=(1*rat);
         
         g.setColor(new Color(58,75,89));
         
        g.drawLine(x2,y2,x3,y3);
        x2+=(1*rat);
         y2+=(1*rat);
        g.drawLine(x2,y2,x3,y3);
        x2+=(1*rat);
         y2+=(1*rat);
        g.drawLine(x2,y2,x3,y3);
        x2+=(1*rat);
        
        if(fight)
            g.setColor(Color.yellow);
        
        g.drawLine(x2,y2,x3,y3);
        
        
        
        
        g.setColor(Color.pink);
        
        x4-=2;
        
        g.fillRect(x4,y4,3,3);
        
        g.drawLine(x4,y4,x2,y2);
         x4+=(1*rat);
         y4+=(1*rat);
         g.drawLine(x4,y4,x2,y2);
          x4+=(1*rat);
         y4+=(1*rat);
         g.drawLine(x4,y4,x2,y2);
          x4+=(1*rat);
         y4+=(1*rat);
         g.drawLine(x4,y4,x2,y2);
     }
    
    
        
    public void drawMan(Graphics g,int x, int y,int w, int h)
    {    //Effect: displays soldier graphic
        double rat=w/50;
        


        drawBody(g,x,y,w,h,rat);
        drawWeapon(g,x,y,w,h,rat);
        
        drawCloth(g,x,y,w,h,rat);
        
        //if(health > maxhealth/7)
        //drawShield(g,x,y,w,h,rat);
        
    }
    
    void TestEmpty()
    {
     Vector vec = getVecEmptyNeighbors();
            
     if(vec==null||vec.size()<1)
     {  System.out.println(" no empty");
         
         return;}
            
            
        
          for(int i=0;i<vec.size();i++)
          {
            SimObject s1 = (SimObject) vec.get(1);
          //   getGrid().setObjectAt(s1.getRow(),s1.getColumn(),spl);
             
             System.out.println("empty at "+ s1.getRow()+ " " + s1.getColumn() + " ");
        
             
            getGrid().setObjectAt(s1.getRow(),s1.getColumn(),s1);
             
             
          }
        
        
    }
    


    public void executeTurn() throws RuntimeException
       // Note: requires isInitialized is inherited from SimObject
       // EFFECTS: if enemy one away attacks first, closest enemy with for damage  
       // if not attacking Picks a direction closer to enemy and tries to move that way. 
       //          If the move is successful, return true. If the move fails
       //          because the spot being attempted to move into is already
       //          occupied then return false.
       //          then delays for given speed
    {
        if(getHealth()<0)
        {   
            Died();
            getGrid().killObjectAt(mrow,mcol);
            return;
        }
        
     
	inTurn = true;
        magic++;
        
        
        
        if((! Attack()&&!Special())  )  
	MoveTo();
            
         delay (getSpeed());
            
         inTurn = false;
    
    
    }
}