package avs.models;

import avs.utils.Animator;
import avs.utils.Textures;
import avs.utils.BufferedImageLoader;
import avs.models.Grid;

import java.util.Iterator;
import java.io.File;
import java.awt.image.*;
import java.awt.*;

import javax.swing.*;
import javax.imageio.ImageIO;

public class Banga extends Plant implements Runnable {
	private Animator anim; //animates this plant
    private Rectangle deathZone; //all the zombies that will come near this area will die
    private static BufferedImage deathZoneImg; //image for the death zone

    public Banga(Textures texx){
		super(95, 159, 150, texx);
		this.animation = texx.getBangaStaticArray();
		anim = new Animator(5,animation);
        this.deathZone = null;
	}

	public void tick(){
        if (this.deathZone != null) killZombies();

        anim.setImages(this.animation);
		anim.runAnimation();
	}

	public void render(Graphics g){
        if (this.deathZone != null) {
            g.drawImage(deathZoneImg, (int)deathZone.getX(), (int)deathZone.getY(),
                (int)deathZone.getWidth(), (int)deathZone.getHeight(), null);
        }

		anim.drawAnimation(g,getX(),getY(),width,height,0);
    }

    public void run() {
        try {
            int x = Grid.BUS_OFFSET + (this.getCol() - 1) * Grid.TILE_WIDTH;
            int y = Grid.SIDEWALK_OFFSET + (this.getRow() - 1) * Grid.TILE_HEIGHT;

            Thread.sleep(3000);

            this.deathZone = new Rectangle(x + 1, y + 1, Grid.TILE_WIDTH * 3 - 2, Grid.TILE_HEIGHT * 3 - 2);
            this.animation = texx.getBangaSpinningArray();
            Thread.sleep(2000);
            this.deathZone = null;

            this.animation = texx.getBangaDyingArray();
            Thread.sleep(580);

            this.kill();
            Game.getInstance().getGrid().setPlant(this.getRow(), this.getCol(), null);
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    //iterates through the Zombie list and if intersects or it is contained in the bounds of the Banga, it will die
    public void killZombies() {
        Iterator<Zombie> iter = Game.getInstance().getZombieList().iterator();
        while (iter.hasNext()) {
            Zombie z = iter.next();
            if (z == null) continue;
            if (deathZone.intersects(z.getBounds()) || deathZone.contains(z.getBounds())) {
                z.kill();
            }
        }
    }

    //temporary
    @Override
    public void setY(double y) {
        super.setY(y - 26);
    }

    public static class AssetLoader implements Runnable {
        public void run() {
            try {
                Banga.deathZoneImg = new BufferedImageLoader().loadImage("/assets/img/area-of-death.png");
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }
}

@@ -559,16 +559,48 @@
 ath zone
+%0A    private boolean killModeOn;
 %0A%0D%0A    p
@@ -760,34 +760,54 @@
 his.
-deathZone = null;%0D%0A%09%7D%0D%0A%0D%0A%09
+killModeOn = false;%0D%0A%09%7D%0D%0A%0D%0A    @Override%0D%0A    
 publ
@@ -818,14 +818,16 @@
 oid 
-tick
+start
 ()
+ 
 %7B%0D%0A 
@@ -826,35 +826,185 @@
 t() %7B%0D%0A        i
-f (
+nt x = Grid.BUS_OFFSET + (this.getCol() - 1) * Grid.TILE_WIDTH;%0D%0A        int y = Grid.SIDEWALK_OFFSET + (this.getRow() - 1) * Grid.TILE_HEIGHT;%0D%0A        
 this.deathZone !
@@ -1002,23 +1002,183 @@
 athZone 
-!
 = n
-ull
+ew Rectangle(x + 1, y + 1, Grid.TILE_WIDTH * 3 - 2, Grid.TILE_HEIGHT * 3 - 2);%0D%0A%0D%0A        super.start();%0D%0A    %7D%0D%0A%0D%0A%09public void tick()%7B%0D%0A        if (this.killModeOn
 ) killZo
@@ -1315,25 +1315,18 @@
 his.
-deathZone != null
+killModeOn
 ) %7B%0D
@@ -1623,302 +1623,65 @@
     
-int x = Grid.BUS_OFFSET + (this.getCol() - 1) * Grid.TILE_WIDTH;%0D%0A            int y = Grid.SIDEWALK_OFFSET + (this.getRow() - 1) * Grid.TILE_HEIGHT;%0D%0A%0D%0A            Thread.sleep(3000);%0D%0A%0D%0A            this.deathZone = new Rectangle(x + 1, y + 1, Grid.TILE_WIDTH * 3 - 2, Grid.TILE_HEIGHT * 3 - 2)
+Thread.sleep(3000);%0D%0A%0D%0A            this.killModeOn = true
 ;%0D%0A 
@@ -1768,24 +1768,26 @@
 eep(2000);%0D%0A
+%0D%0A
             
@@ -1795,27 +1795,27 @@
 his.
-deathZone = null;%0D%0A
+killModeOn = false;
 %0D%0A  
@@ -2499,24 +2499,86 @@
  continue;%0D%0A
+            if (deathZone == null) System.out.println(true);%0D%0A
             

Fix banga death zone bug.