import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;

/**
 * Write a description of class Bottle here.
 * 
 * @author Jonathan Lutz
 * @version (a version number or a date)
 */
public class throw_bottle extends Weapon
{
    private static int sizex = 25;
    private static int sizey = 25;

    private boolean alreadypicked = false;
    private long alreadypickedmillis = 0;
    private int count = 0;
    public static boolean monstertouched = false;
    public static boolean available;
    public static boolean threw = false;
    private static int amount = 0;
    private int bufX = 10;
    private int bufY = 10;
    private int direction;

    private long lastthrow = 0;

    private static int speed = 5;
    private static int speed2 = 3;

    private static String[] bottlesright = {"Skins/Waffe/JackDaniels1.png","Skins/Waffe/JackDaniels2.png","Skins/Waffe/JackDaniels3.png","Skins/Waffe/JackDaniels4.png","Skins/Waffe/JackDaniels5.png","Skins/Waffe/JackDaniels6.png","Skins/Waffe/JackDaniels7.png","Skins/Waffe/JackDaniels8.png"};
    private static String[] bottlesleft = {"Skins/Waffe/JackDaniels1.png","Skins/Waffe/JackDaniels8.png","Skins/Waffe/JackDaniels7.png","Skins/Waffe/JackDaniels6.png","Skins/Waffe/JackDaniels5.png","Skins/Waffe/JackDaniels4.png","Skins/Waffe/JackDaniels3.png","Skins/Waffe/JackDaniels2.png"};

    static GreenfootSound tomahawk = new GreenfootSound("tomahawk.mp3");
    static GreenfootSound tomahawk2 = new GreenfootSound("tomahawkimpact.mp3");
    Linch linch;

    public throw_bottle()
    {
        GreenfootImage img = new GreenfootImage("Skins/Waffe/JackDaniels1.png");
        img.scale(sizex, sizey);
        setImage(img);

    }

    /**
     * Act - do whatever the Bottle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Welt.pause != true)
        {
            if (available)
            {
                if (Greenfoot.isKeyDown("m") && System.currentTimeMillis()-lastthrow > 3000)
                {
                    Throwimage.transparent = true;
                    threw = true;
                    setLocation(linch.Xcoord,linch.Ycoord);
                    lastthrow = System.currentTimeMillis();
                }
                else if (System.currentTimeMillis()-lastthrow < 3000)
                {
                    Throwimage.transparent = true;
                }
                if (threw == true)
                {
                    checkDirection();
                    tomahawk.playLoop();
                }

                throwbottle();

                if (monstertouched == true)
                {
                    tomahawk.stop();
                    tomahawk2.play();
                    setLocation(-100,-100);
                    direction = 0;
                    monstertouched = false;
                }
            }
        }
    }    

    public void checkDirection()
    {
        if (Linch.dir.equals("oben"))
        {
            direction = 1;
            threw = false;
        }
        else if (Linch.dir.equals("unten"))
        {
            direction = 2;
            threw = false;
        }
        else if (Linch.dir.equals("rechts"))
        {
            direction = 3;
            threw = false;
        }
        else if (Linch.dir.equals("links"))
        {
            direction = 4;
            threw = false;
        }
        else if (Linch.dir.equals("untenlinks"))
        {
            direction = 5;
            threw =false;
        }
        else if (Linch.dir.equals("obenlinks"))
        {
            direction = 6;
            threw =false;
        }
        else if (Linch.dir.equals("untenrechts"))
        {
            direction = 7;
            threw =false;
        }
        else if (Linch.dir.equals("obenrechts"))
        {
            direction = 8;
            threw =false;
        }
    }

    public void throwbottle()
    {
        int width = getWorld().getWidth();
        int height = getWorld().getHeight();
        if (direction == 0)
        {
            setLocation(-100,-100);
        }
        else if (direction == 1)
        {
            if (!Background.mask.getColorAt(getX(), ((getY() - bufY) < 0 ? 0 : (getY() - bufY))).equals(Color.black))
            {
                //werfen
                setLocation(getX(),getY()-speed);
            }               
            else
            {
                setLocation(-100,-100);
                threw = false;
                direction = 0;
                tomahawk.stop();
                tomahawk2.play();
                Throwimage.transparent = true;
            }
            move( bottlesright, true, sizex, sizey);
        }
        else if (direction == 2)
        {
            if (!Background.mask.getColorAt(getX() , ((getY() + bufY) >= height ? height-1 : (getY() + bufY))).equals(Color.black))
            {
                //werfen
                setLocation(getX(),getY()+speed);
            }
            else
            {
                setLocation(-100,-100);
                threw = false;
                direction = 0;
                tomahawk.stop();
                tomahawk2.play();
                Throwimage.transparent = true;
            }
            move( bottlesright, true, sizex, sizey);
        }
        else if (direction == 3)
        {
            if (!Background.mask.getColorAt(((getX() + bufX) >= width ? width-1 : (getX() + bufX)), getY()).equals(Color.black))
            {
                //werfen
                setLocation(getX()+speed,getY());
            }
            else
            {
                setLocation(-100,-100);
                threw = false;
                direction = 0;
                tomahawk.stop();
                tomahawk2.play();
                Throwimage.transparent = true;
            }
            move( bottlesright, true, sizex, sizey);
        }
        else if (direction == 4)
        {
            if (!Background.mask.getColorAt(((getX() - bufX) < 0 ? 0 : (getX() - bufX)), getY()).equals(Color.black))
            {
                //werfen
                setLocation(getX()-speed,getY());
            }
            else
            {
                setLocation(-100,-100);
                threw = false;
                direction = 0;
                tomahawk.stop();
                tomahawk2.play();
                Throwimage.transparent = true;
            }
            move( bottlesleft, true, sizex, sizey);
        }
        else if (direction == 5)
        {
            if (!Background.mask.getColorAt(((getX() - bufX) < 0 ? 0 : (getX() - bufX)), ((getY() + bufY) >= height ? height-1 : (getY() + bufY))).equals(Color.black))
            {
                //werfen
                setLocation(getX()-speed2,getY()+speed2);
            }
            else
            {
                setLocation(-100,-100);
                threw = false;
                direction = 0;
                tomahawk.stop();
                tomahawk2.play();
                Throwimage.transparent = true;
            }
            move( bottlesleft, true, sizex, sizey);
        }
        else if (direction == 6)
        {
            if (!Background.mask.getColorAt(((getX() - bufX) < 0 ? 0 : (getX() - bufX)), ((getY() - bufY) < 0 ? 0 : (getY() - bufY))).equals(Color.black))
            {
                //werfen
                setLocation(getX()-speed2,getY()-speed2);
            }
            else
            {
                setLocation(-100,-100);
                threw = false;
                direction = 0;
                tomahawk.stop();
                tomahawk2.play();
                Throwimage.transparent = true;
            }
            move( bottlesleft, true, sizex, sizey);
        }
        else if (direction == 7)
        {
            if (!Background.mask.getColorAt(((getX() + bufX) >= width ? width-1 : (getX() + bufX)), ((getY() + bufY) >= height ? height-1 : (getY() + bufY))).equals(Color.black))
            {
                //werfen
                setLocation(getX()+speed2,getY()+speed2);
            }
            else
            {
                setLocation(-100,-100);
                threw = false;
                direction = 0;
                tomahawk.stop();
                tomahawk2.play();
                Throwimage.transparent = true;
            }
            move( bottlesright, true, sizex, sizey);
        }
        else if (direction == 8)
        {
            if (!Background.mask.getColorAt(((getX() + bufX) >= width ? width-1 : (getX() + bufX)), ((getY() - bufY) < 0 ? 0 : (getY() - bufY))).equals(Color.black))
            {
                //werfen
                setLocation(getX()+speed2,getY()-speed2);
            }
            else
            {
                setLocation(-100,-100);
                threw = false;
                direction = 0;
                tomahawk.stop();
                tomahawk2.play();
                Throwimage.transparent = true;
            }
            move( bottlesright, true, sizex, sizey);
        }

        if (direction != 0 && (getX() > 640 || getX() < 0 || getY() > 480 || getY() < 0))
        {    
            tomahawk.stop();
            tomahawk2.play();

            direction = 0;    
        }

        if (direction == 0 && System.currentTimeMillis()-lastthrow > 3000)
        {
            Throwimage.transparent = false;
        }

        if (direction != 0)
        {
            Monster.checkthethrow_bottle = true;
            Monsterboss.checkthethrow_bottle = true;
        }
    }

    public void giveLinch(Linch linchget)
    {
        linch = linchget;
    }

    public void bottlecheck(Bottlecheck bottlecheck)
    {

    }

}
