import java.util.List;
import java.util.ArrayList;
import java.awt.*;
import java.lang.Math.*;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Linch here.
 * 
 * @author Jonathan, Florian
 * @version (a version number or a date)
 */
public class Linch extends Moving
{
    public static double step = 3;
    public static int Xcoord = 0;
    public static int Ycoord = 0;

    public static int allhearts = 4;
    public static String dir;

    public int linchhearts = allhearts;
    public boolean right = false;
    public boolean left = false;
    public boolean down = false;
    public boolean up = false;
    public boolean untenlinks = false;
    public boolean untenrechts = false;

    private double stepTmp = 0;
    boolean test = false;
    public static boolean walking = false;

    static GreenfootSound steps = new GreenfootSound("grass.mp3");
    private static int volume = 100;
    
    public static String[] picsoben = {"Skins/up/linch_up1.png","Skins/up/linch_up2.png","Skins/up/linch_up3.png","Skins/up/linch_up4.png","Skins/up/linch_up5.png","Skins/up/linch_up6.png","Skins/up/linch_up7.png","Skins/up/linch_up8.png"};
    public static String[] picsunten = {"Skins/down/linch_down1.png","Skins/down/linch_down2.png","Skins/down/linch_down3.png","Skins/down/linch_down4.png","Skins/down/linch_down5.png","Skins/down/linch_down6.png","Skins/down/linch_down7.png","Skins/down/linch_down8.png"};
    public static String[] picslinks = {"Skins/left/linch_left1.png","Skins/left/linch_left2.png","Skins/left/linch_left3.png","Skins/left/linch_left4.png","Skins/left/linch_left5.png","Skins/left/linch_left6.png","Skins/left/linch_left7.png","Skins/left/linch_left8.png"};
    public static String[] picsrechts = {"Skins/right/linch_right1.png","Skins/right/linch_right2.png","Skins/right/linch_right3.png","Skins/right/linch_right4.png","Skins/right/linch_right5.png","Skins/right/linch_right6.png","Skins/right/linch_right7.png","Skins/right/linch_right8.png"};
    private int size = 38;

    Heart[] hearts = new Heart[5];

    public Linch(){
        GreenfootImage img = getImage();
        img.scale(40, 40);
        setImage(img);
        
    }

    
    
    /**
     * Act - do whatever the Knopf 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)
        {
            right = false;
            left = false;
            down = false;
            up = false;
            Xcoord = getX();
            Ycoord = getY();
            int bufX = 11;
            int bufY = 21;
            int width = getWorld().getWidth();
            int height = getWorld().getHeight();
            try{
                if(Greenfoot.isKeyDown("right") && !Background.mask.getColorAt(((getX() + bufX) >= width ? width-1 : (getX() + bufX)), getY()).equals(Color.black)){
                    setLocation(Math.min(getX() + (int)stepTmp, width-1), getY());
                    right = true;

                }
                if(Greenfoot.isKeyDown("left") && !Background.mask.getColorAt(((getX() - bufX) < 0 ? 0 : (getX() - bufX)), getY()).equals(Color.black)){
                    setLocation(Math.max(getX() - (int)stepTmp, 0), getY());
                    left = true;

                }
                if(Greenfoot.isKeyDown("down") && !Background.mask.getColorAt(getX() , ((getY() + bufY) >= height ? height-1 : (getY() + bufY))).equals(Color.black)){
                    setLocation(getX(), Math.min(getY() + (int)stepTmp, height-1));
                    down = true;
                }
                if(Greenfoot.isKeyDown("up") && !Background.mask.getColorAt(getX(), ((getY() - bufY) < 0 ? 0 : (getY() - bufY))).equals(Color.black)){
                    setLocation(getX(), Math.max(getY() - (int)stepTmp, 0));
                    up = true;
                }

            }catch(Error e){}

            setPicture();
            move(picslinks,false,size,size);
            /*String[] p = new String[5];
            p[0]="1.png";
            p[1]="2.png";
            p[2]="3.png";
            p[3]="4.png";*/

            if ((Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("right")) && Background.mask.getColorAt(((getX() + bufX) >= width ? width-1 : (getX() + bufX)), getY()).equals(Color.yellow) )
            {
                walking = true;
                walksound(true, 1);
            }
            else if (Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("right"))
            {
                walking = true;
                walksound(true, 0);
            }
            else
            {
                walking = false;
                walksound(false, 0);
            }
        }
        else
        {
            walksound(false, 0);
        }
    }

    public static void walksound(boolean on, int track)
    {
        if (track == 0)
        {
            if (on == true)
            {
                steps.setVolume(60);
                volume = 100;
                steps.playLoop();
            }
            else
            {

                if (steps.getVolume() > 0)
                {
                    volume = volume-5;
                    steps.setVolume(volume);
                }
                else 
                {
                    steps.pause();
                }
            }
        }
        else if (track == 1)
        {
            if (on == true)
            {
                steps.playLoop();
            }
            else
            {
                steps.pause();
            }
        }
    }

    public void giveHearts(Heart[] heartsgive)
    {
        for (int i = 0; i < heartsgive.length; i++)
        {
            hearts[i] = heartsgive[i];
        }
    }

    public void removeHeart()
    {
        if (linchhearts >= 0)
        {
            hearts[linchhearts].setVisibility(false);
            linchhearts--;
        }
        else
        {
            walksound(false, 0);
            String[] msg = {"Du bist leider GAME OVER!", "", "", "Probiere es erneut."};
            Greenfoot.playSound("player_dead.mp3");
            Background.msg(msg);
            try{
                Thread.sleep(3500);

            }catch(Exception e){}
            Greenfoot.setWorld(new Welt());
            
            Greenfoot.stop();
        }
    }

    public void addHeart()
    {
        if(linchhearts < 4){
            linchhearts++;
            hearts[linchhearts].setVisibility(true);
        }
    }

    public void setPicture()
    {
        if (right == true && up == true)
        {
            setRotation(0);
            stepTmp = Math.sqrt(step*step/2);
            move(picsrechts,true,size,size);
            dir = "obenrechts";
            //Bild obenrechts
        }
        else if (right == true && down == true)
        {
            // setRotation(45);
            stepTmp = Math.sqrt(step*step/2);
            move(picsrechts,true,size,size);
            untenrechts = true;
            dir = "untenrechts";
            //Bild untenrechts
        }
        else if (right == true)
        {
            setRotation(0);
            stepTmp = step;
            move(picsrechts,true,size,size);
            dir="rechts";
            //Bild rechts
        }
        if (left == true && up == true)
        {
            setRotation(0);
            stepTmp = Math.sqrt(step*step/2);
            move(picslinks,true,size,size);
            dir = "obenlinks";
            //Bild obenLinchs
        }
        else if (left == true && down == true)
        {
            setRotation(0);
            stepTmp = Math.sqrt(step*step/2);
            move(picslinks,true,size,size);
            untenlinks = true;
            dir = "untenlinks";
            //Bild untenLinchs
        }
        else if (left == true)
        {
            setRotation(0);
            stepTmp = step;
            move(picslinks,true,size,size);
            dir = "links";
            //Bild Linchs
        }
        if (up == true && left != true && right != true)
        {
            setRotation(0);
            stepTmp = step;
            move(picsoben,true,size,size);
            dir = "oben"; 
            //Bild oben
        }
        if (down == true && left != true && right != true)
        {
            setRotation(0);
            stepTmp = step;
            move(picsunten,true,size,size);
            dir = "unten";
            //Bild unten
        }

    }
}