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 Mercenary extends Soldier {
    // OVERVIEW: A Mercenary is a soldier that performace a walk,
    //    moving one square in a direction toward enemy each step.
    //      Attacks enemies untill dead or victorius
   // Medium Life, Medium Damage, Fast, Special: when out numbered joins attackers

    
    private boolean inTurn; // Currently in the middle of a turn (used for coloring)
    
    
    int maxhealth;
    
    public Mercenary () {
	inTurn = false;
        setFight(false);
       
       setHealth(35);
        
        
        setSpeed(350);
      //  setAttacknum(1); //default is 1
        setDamage(8);
        
       
        
        maxhealth=getHealth();
       // color=c1;
        
       
    }
    
     public String getType()
    {   //Effect: returns soldier type
     return "Mercenary";   
    }
   
  
    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()
    {   //modifies: this
        //Effect: returns true if changes teams else false
       
        Vector enemies = toAttack();
        
        if(enemies.size() >=2)
        {
        Soldier ss1 = (Soldier) enemies.get(0); 
        
        if(!sameTeam(ss1))
        {
            System.out.println(toString() + " joins " + ss1.teamToString()); 
            setTeam(ss1.getTeam());
            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);    
        g.setColor(new Color(200,200,45));
        g.fillOval(x2,y2+m,w/3-m,h/3);
        g.setColor(Color.black);
        g.drawOval(x2,y2+m,w/3-m,h/3);
        x2+=(int)(4*rat);
      
        x2+=(int)(3*rat);y2+=(int)(2*rat);
        g.drawOval(x2,y2+m,w/3-5-2*m,h/3-4);
         
        }
        else
        {
         m=(int)(10*rat);    
        x2+=w/4;
        x2-=2*m;
        g.setColor(new Color(200,200,45));
        g.fillOval(x2,y2+m,w/3-m,h/3);
        g.setColor(Color.black);
        
        g.drawOval(x2,y2+m,w/3-m,h/3);
        x2+=(int)(4*rat);
    
        x2+=(int)(3*rat);y2+=(int)(2*rat);
        if(fight)
                   g.setColor(Color.black);
        
        g.drawOval(x2,y2+m,w/3-5-2*m,h/3-4);
                    
        }
            
    }
    
    
    
     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/3;
        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/2-(int)(2*rat);
            
        }
       
        
        if((fight)&&(inTurn))
        {
        
        y3-=h/6;
        //y2-=w/5;
        x2-=w/5;
           
        }
        
        int x4=x2;
        int y4=y2;
        
        g.setColor(Color.darkGray);
        
        if(fight)
            g.setColor(Color.red);
            
        g.drawLine(x2,y2,x3,y3);
        x2+=(1*rat);
         y2+=(1*rat);
         
         g.setColor(Color.darkGray);
         
        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.red);
        
        g.drawLine(x2,y2,x3,y3);
     
        
        g.setColor(Color.black);
        
        x4-=2;
        
        g.fillRect(x4,y4,5,5);
        
        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);
                
        if(getHealth() > maxhealth/7)
        drawShield(g,x,y,w,h,rat);
        
    }
    


    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;
      
        Attack();
      
         if((!Special())) 
             MoveTo();
         
         delay (getSpeed()); 
        
        inTurn = false;
    
    }
}