package com.mygdx.mount.game.actors;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.mygdx.mount.game.models.SawConfiguration;
import com.mygdx.mount.game.models.ShooterConfigurator;

import java.util.ArrayList;

/**
 * Created by wannabe on 25.04.15.
 */
public class Shooter extends Boundable {
    public enum DIRECTION {
        UP, RIGHT, DOWN, LEFT
    }

    public static int[] TEXTURE_X = {
            6,//down
            63,//up
            14,//left
            84//right
    };
    public static int[] TEXTURE_Y = {
            3, 3,
            76,
            77

    };

    public final static String TEXTURE_URL = "sprites/cannon.png";
    public final static int WIDTH = 60;
    public final static int HEIGHT = 60;
    protected TextureRegion blockTexture;
    public DIRECTION direction;
    public ArrayList<Bullet> bullets = new ArrayList<Bullet>();
    private int step;
    private int range;
    private int speed;

    public Shooter(DIRECTION direction, int step, int range, int speed, Texture texture) {
        super();
        int i = getIndexFromDirection(direction);
        blockTexture = new TextureRegion(texture, TEXTURE_X[i], TEXTURE_Y[i], WIDTH, HEIGHT);
        this.direction = direction;
        this.step = step;
        this.range = range;
        this.speed = speed;
        setWidth(WIDTH);
        setHeight(HEIGHT);
    }

    public Shooter(ShooterConfigurator configuration, Texture texture) {
        this(configuration.direction, configuration.step, configuration.range, configuration.speed, texture);
        setX(configuration.x);
        setY(configuration.y);
        setBoundRectangle((int) getX(), (int) getY(), (int) getWidth(), (int) getHeight());
    }

    public TextureRegion getBlockTexture() {
        return blockTexture;
    }

    public void moveBullets() {
        ArrayList<Bullet> bulletsTmp = new ArrayList<Bullet>();
        for (Bullet bullet : bullets) {
            if (Math.abs(bullet.getX() - getX()) > range) {
                continue;
            }
            if (this.direction == DIRECTION.UP) {
                bullet.setY(bullet.getY() + speed);
            } else if (this.direction == DIRECTION.DOWN) {
                bullet.setY(bullet.getY() - speed);
            } else if (this.direction == DIRECTION.RIGHT) {
                bullet.setX(bullet.getX() + speed);
            } else {
                bullet.setX(bullet.getX() - speed);
            }
            bullet.setBoundRectangle((int) bullet.getX(), (int) bullet.getY(), (int) bullet.getWidth(), (int) bullet.getHeight());
            bulletsTmp.add(bullet);
        }
        this.bullets = bulletsTmp;
    }

    public Bullet spawnBullet() {
        Bullet bullet = new Bullet();
        if (this.direction == DIRECTION.UP) {
            bullet.setY(this.getY() + speed);
            bullet.setX(getX());
        } else if (this.direction == DIRECTION.DOWN) {
            bullet.setY(this.getY() - speed);
            bullet.setX(getX());
        } else if (this.direction == DIRECTION.RIGHT) {
            bullet.setX(this.getX() + speed);
            bullet.setY(getY());
        } else {
            bullet.setX(this.getX() - speed);
            bullet.setY(getY());
        }
        bullet.setBoundRectangle((int) bullet.getX(), (int) bullet.getY(), (int) bullet.getWidth(), (int) bullet.getHeight());
        this.bullets.add(bullet);
        return bullet;
    }

    public Bullet getLastBullet() {
        return !bullets.isEmpty() ? bullets.get(bullets.size() - 1) : null;
    }

    public int getStep() {
        return step;
    }

    public boolean isLastBulletValid() {
        if (getLastBullet() == null) {
            return false;
        }
        if (this.direction == DIRECTION.UP || this.direction == DIRECTION.DOWN) {
            return Math.abs(getLastBullet().getY() - getY()) > getStep();
        } else {
            return Math.abs(getLastBullet().getX() - getX()) > getStep();
        }
    }

    private int getIndexFromDirection(DIRECTION direction){
        if(direction.equals(DIRECTION.DOWN)){
            return 0;
        }
        if(direction.equals(DIRECTION.UP)){
            return 1;
        }
        if(direction.equals(DIRECTION.LEFT)){
            return 2;
        }
        if(direction.equals(DIRECTION.RIGHT)){
            return 3;
        }
        return 1;
    }
}

@@ -2913,32 +2913,46 @@
 s.getY() + speed
+ + getHeight()
 );%0A            b
@@ -2960,32 +2960,54 @@
 llet.setX(getX()
+ + 10 + getWidth() / 2
 );%0A        %7D els
@@ -3124,16 +3124,38 @@
 X(getX()
+ - 10 + getWidth() / 2
 );%0A     
@@ -3240,32 +3240,45 @@
 s.getX() + speed
+ + getWidth()
 );%0A            b
@@ -3286,32 +3286,55 @@
 llet.setY(getY()
+ + getHeight() / 2 + 10
 );%0A        %7D els
@@ -3375,32 +3375,32 @@
 etX() - speed);%0A
-
             bull
@@ -3413,16 +3413,39 @@
 Y(getY()
+ + getHeight() / 2 + 10
 );%0A     
@@ -4252,16 +4252,17 @@
 rection)
+ 
 %7B%0A      
@@ -4257,32 +4257,33 @@
 on) %7B%0A        if
+ 
 (direction.equal
@@ -4300,16 +4300,17 @@
 N.DOWN))
+ 
 %7B%0A      
@@ -4337,32 +4337,33 @@
     %7D%0A        if
+ 
 (direction.equal
@@ -4378,16 +4378,17 @@
 ION.UP))
+ 
 %7B%0A      
@@ -4415,32 +4415,33 @@
     %7D%0A        if
+ 
 (direction.equal
@@ -4458,16 +4458,17 @@
 N.LEFT))
+ 
 %7B%0A      
@@ -4503,16 +4503,17 @@
       if
+ 
 (directi
@@ -4539,16 +4539,17 @@
 .RIGHT))
+ 
 %7B%0A      

complete cannon sprites