import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
 * Write a description of class Monsterboss here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Monsterboss extends Element
{
    public static boolean checkthethrow_bottle = false;
    private double step = 2;
    private boolean right;
    private boolean left;
    private boolean up;
    private boolean down;

    protected int size = 45;

    protected String[] picso = {"Skins/Boss/up/boss_up1.png", "Skins/Boss/up/boss_up2.png", "Skins/Boss/up/boss_up3.png", "Skins/Boss/up/boss_up4.png", "Skins/Boss/up/boss_up5.png", "Skins/Boss/up/boss_up6.png", "Skins/Boss/up/boss_up7.png", "Skins/Boss/up/boss_up8.png"};
    protected String[] picsu = {"Skins/Boss/down/boss_down1.png", "Skins/Boss/down/boss_down2.png", "Skins/Boss/down/boss_down3.png", "Skins/Boss/down/boss_down4.png", "Skins/Boss/down/boss_down5.png", "Skins/Boss/down/boss_down6.png", "Skins/Boss/down/boss_down7.png", "Skins/Boss/down/boss_down8.png"};
    protected String[] picsl = {"Skins/Boss/left/boss_left1.png", "Skins/Boss/left/boss_left2.png", "Skins/Boss/left/boss_left3.png", "Skins/Boss/left/boss_left4.png"};
    protected String[] picsr = {"Skins/Boss/right/boss_right1.png", "Skins/Boss/right/boss_right2.png", "Skins/Boss/right/boss_right3.png", "Skins/Boss/right/boss_right4.png"};
    protected String[] picsa = {"Skins/Boss/attack/boss_attack1.png", "Skins/Boss/attack/boss_attack2.png", "Skins/Boss/attack/boss_attack3.png", "Skins/Boss/attack/boss_attack4.png", "Skins/Boss/attack/boss_attack5.png", "Skins/Boss/attack/boss_attack6.png", "Skins/Boss/attack/boss_attack7.png", "Skins/Boss/attack/boss_attack8.png"};
    private String[] picsoben;
    private String[] picsunten;
    private String[] picslinks;
    private String[] picsrechts;

    private boolean moving;

    private long lastattack = 0;
    private long attackduration = 0;
    private long lasthit = 0;
    private long buftime = 1000;
    private long lastBottleHit = 0;
    
    private int live = 5;
    
    public boolean attacking = false;
    
    public Monsterboss()
    {
        picsoben = picso;
        picsunten = picsu;
        picsrechts = picsr;
        picslinks = picsl;
    }

    /**
     * Act - do whatever the Monsterboss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        if (!Welt.pause)
        {
            right=false;
            left=false;
            up=false;
            down=false;
            step = Greenfoot.getRandomNumber(1)+2;
            if ((Linch.Xcoord-getX())>0)
            {
                right = true;
            }
            else if ((Linch.Xcoord-getX())<0)
            {
                left = true;
            }
            if ((Linch.Ycoord-getY())>0)
            {
                down = true;
            }
            else if ((Linch.Ycoord-getY()<0))
            {
                up = true;
            }

            if (attacking)
            {
                step = 6;
            }
            else
            {
                step = 2;
            }
            
            moving = setPictureAndMove();
            if ((System.currentTimeMillis()-lastattack)>= (Greenfoot.getRandomNumber(3000)+2000))
            {
                picsoben = picsa;
                picsunten = picsa;
                picsrechts = picsa;
                picslinks = picsa; 
                attacking = true;
                if (System.currentTimeMillis()-lastattack >= Greenfoot.getRandomNumber(5000)+3000)
                {
                    lastattack = System.currentTimeMillis();
                }
            }
            else
            {
                picsoben = picso;
                picsunten = picsu;
                picsrechts = picsr;
                picslinks = picsl;
                attacking = false;
            }
            
            if (attacking)
            {
                checkLinch();
            }
            checkBottle();
            if (checkthethrow_bottle == true)
            {
                checkthrow_bottle();
                checkthethrow_bottle = false;
            }
        }
    }    

    private void checkBottle(){
        if((System.currentTimeMillis() - lastBottleHit) >= 1000){
            if(isTouching(Bottle.class)){
                if(live == 1){
                    getWorld().removeObject(this);
                    Greenfoot.playSound("zombiedie.mp3");
                }else{
                    live--;
                }
                lastBottleHit = System.currentTimeMillis();
            }
        }

    }
    
    private void checkthrow_bottle()
    {
        if (isTouching(throw_bottle.class)){
            if (live == 1){
                getWorld().removeObject(this);
                Greenfoot.playSound("zombiedie.mp3");
            }
            else{
                live--;
            }
            throw_bottle.monstertouched = true;
        }
    }
    
    public void livedown()
    {

        if(live == 1){
            getWorld().removeObject(this);
            Greenfoot.playSound("zombiedie.mp3");
        }else{
            live--;
        }

    }
    
    /**
     * @author Jonathan
     */
    private boolean setPictureAndMove()
    {
        double stepTmp;
        boolean returnVal = false;
        if (right == true && up == true)
        {
            setRotation(0);
            stepTmp = Math.sqrt(step*step/2);
            move(picsrechts,true,size,size);
            returnVal = setLocation(getX() + (int)stepTmp, getY() - (int)stepTmp, true);
            //Bild obenrechts
        }
        else if (right == true && down == true)
        {
            // setRotation(45);
            stepTmp = Math.sqrt(step*step/2);
            move(picsrechts,true,size,size);
            returnVal = setLocation(getX() + (int)stepTmp, getY() + (int)stepTmp, true);
            //Bild untenrechts
        }
        else if (right == true)
        {
            setRotation(0);
            stepTmp = step;
            move(picsrechts,true,size,size);
            returnVal = setLocation(getX() + (int)stepTmp, getY(), true);
            //Bild rechts
        }
        if (left == true && up == true)
        {
            setRotation(0);
            stepTmp = Math.sqrt(step*step/2);
            move(picslinks,true,size,size);
            returnVal = setLocation(getX() - (int)stepTmp, getY() - (int)stepTmp, true);
            //Bild obenLinchs
        }
        else if (left == true && down == true)
        {
            setRotation(0);
            stepTmp = Math.sqrt(step*step/2);
            move(picslinks,true,size,size);
            returnVal = setLocation(getX() - (int)stepTmp, getY() + (int)stepTmp, true);
            //Bild untenLinchs
        }
        else if (left == true)
        {
            setRotation(0);
            stepTmp = step;
            move(picslinks,true,size,size);
            returnVal = setLocation(getX() - (int)stepTmp, getY(), true);
            //Bild Linchs
        }
        if (up == true && left != true && right != true)
        {
            setRotation(0);
            stepTmp = step;
            move(picsoben,true,size,size);
            returnVal = setLocation(getX(), getY() - (int)stepTmp, true);
            //Bild oben
        }
        if (down == true && left != true && right != true)
        {
            setRotation(0);
            stepTmp = step;
            move(picsunten,true,size,size);
            returnVal = setLocation(getX(), getY() + (int)stepTmp, true);
            //Bild unten
        }
        return returnVal;
    }
    
     /**
     * Überschreibt setLocation und bewegt den Actor nur wenn die Maske es zulässt
     * und er nicht aus dem Bild läuft
     * 
     * @param x Neue-X-Koordinate
     * @param y Neue-Y-Koordinate
     * @param checkBeforeMoving Wenn TRUE wird Linch nur bewegt wenn er nicht aus dem Bild läuft und die Maske es zulässt
     * @author Florian
     * @return TRUE, wenn das Monster an die Stelle laufen darf
     */
    public boolean setLocation(int x, int y, boolean checkBeforeMoving){
        if(checkBeforeMoving){
            if(x >= 0 && y >= 0 && y < Welt.height && x < Welt.width){   
                if(!Background.mask.getColorAt(x, y).equals(Color.black)){  //Actor darf an die Stelle laufen
                    super.setLocation(x, y);
                    return true;
                }
            }else{
                return false;
            }
        }else{      //beim z.B: Scrollen soll nicht geprüft werden (Monster läuft aus dem Bild)
            super.setLocation(x, y);
        }
        return false;
    }
    
    private void checkLinch(){
        if (System.currentTimeMillis()-lasthit >= buftime)
        {
            if(isTouching(Linch.class)){
                lasthit = (System.currentTimeMillis());
                Linch linch = (Linch)getOneTouching(Linch.class);
                linch.removeHeart();
                linch.move(-5);
                Greenfoot.playSound("zombiedie.mp3");
            }
        }
    }
}
