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 Blob extends Soldier {
    // OVERVIEW: A Blob 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, Medium Damage, Slow, Special: replicates itself

    
    private boolean inTurn; // Currently in the middle of a turn (used for coloring)
    
    int magic;
    int maxhealth;
    
    public Blob () {
	inTurn = false;
        setFight(false);
       
       setHealth(10);
        
        
        setSpeed(300);
      //  setAttacknum(1); //default is 1
        setDamage(5);
        magic=0;
       
        
        maxhealth=getHealth();
       // color=c1;
        
       
    }
    
     public String getType()
    {   //Effect: returns soldier type
     return "Blob";   
    }
  
    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 splits
       if(getHealth()<=8)
           return false;
        
        if(magic >= 15)
        {
            
              synchronized(getGrid())
              {
            
            Blob spl= new Blob();
            spl.setTeam(getTeam());
            
            Vector vec = getVecEmptyNeighbors();
            if(vec==null||vec.size()<1)
            return false;
            
            SimObject s1 = (SimObject) vec.get(0);
        
        getGrid().setObjectAt(s1.getRow(),s1.getColumn(),spl);
        
        Thread simObjectThread = new Thread (spl);
        simObjectThread.start();
         
        
        setHealth(getHealth()+1);
        magic=0;
        
        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(Color.black);
        g.fillRect(x2,y2+m,w/3-m,h/3);
        g.setColor(Color.yellow);
        g.drawRect(x2,y2+m,w/3-m,h/3);
        x2+=(int)(4*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);
        
        g.drawRect(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.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)
    {    //Effect: displays soldier graphic
        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 drawBody(Graphics g,int x, int y,int w, int h, double rat)
    {    //Effect: displays soldier graphic
        
        if(magic <= 5)
        {
         w=w/2+w/5;   
        }
        
        int x2=x;int y2=y;
         y2+=h/5;
        
        
        x2+=w/5;
        
        int size=(w/3+w/2);
        
        if(maxhealth < 20)
        {
           size-=w/4; 
        }
      
         g.setColor(getTeam());
        g.fillOval(x2,y2,size/2,size/2);
        
        
        
        g.setColor(Color.cyan);
        g.drawOval(x2,y2,size/2,size/2);
        int cc=0;
        for(int ww=size;ww >1;ww-=w/8)
        {
            
           g.setColor(new Color(34,230,185));
           
           if(cc%2==1)
               g.setColor(getColor());
         
         cc++;
         
        g.fillOval(x2,y2,ww,ww);  
        
        if(x2-w/20 > x+2)
            x2-=w/20;
        }
      
        g.setColor(getTeam());
        g.fillOval(x2,y2,size/2,size/2);
        
    }

    
    
     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);
      
        
    }
    


    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)
        {   
            System.out.println( toString()+ " Blob has poped" );
            getGrid().killObjectAt(mrow,mcol);
            pauseObject();
        
        return;
        }
        
        
	inTurn = true;
          
      
	 
         magic+=1;
      
         if(!  Attack()&&(!Special())) 
             MoveTo();
         
          delay (getSpeed());
         
                
	inTurn = false;
  
    
    }
}