package de.thdeg.greiner.superpangworld.graphics.base;

import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.helper.XAxisComparator;

import java.util.*;

/**
 * Offers different movement patterns for moveable game objects.
 */
public class MovementPatterns {


    private HashMap<String, ArrayList<Position>> patterns;
    private Random random;

    /**
     * Create the MovementPatterns object providing access to different movement patterns.
     */
    public MovementPatterns(){
        patterns = new HashMap<>();
        random = new Random();

        ArrayList<Position> square = new ArrayList<>((Arrays.asList(new Position(30,30),new Position(930,30),
                                            new Position(930,510),new Position(30,510))));
        ArrayList<Position> zigZag = new ArrayList<>((Arrays.asList( new Position(300,200),new Position(400,340),new Position(500,200),
                                            new Position(600,340),new Position(700,200),new Position(800,340))));
        ArrayList<Position> zero = new ArrayList<>();
        zero.addAll(square);
        zero.addAll(zigZag);
        zero.sort(Comparator.naturalOrder());
        ArrayList<Position> xFirst = new ArrayList<>(zero);
        xFirst.sort(new XAxisComparator());

        Comparator<Position> yAxisComparator = new Comparator<Position>() {
            @Override
            public int compare(Position o1, Position o2) {
                return Double.compare(o1.y,o2.y);
            }
        };
        ArrayList<Position> yFirst = new ArrayList<>(zero);
        yFirst.sort(yAxisComparator);


        ArrayList<Position> centered = new ArrayList<>(zero);
        centered.sort((p1,p2)->{
            Position pMid = new Position(GameView.WIDTH/2,GameView.HEIGHT/2);
            return Double.compare(p1.distance(pMid),p2.distance(pMid));
        });

        patterns.put("square",square);
        patterns.put("zigzag",zigZag);
        patterns.put("zero",zero);
        patterns.put("XFirst",xFirst);
        patterns.put("YFirst",yFirst);
        patterns.put("Centered",centered);
    }

    /**
     * Get a movement pattern by its name.
     * @param pattern the pattern name
     * @return the movement pattern
     */
    public ArrayList<Position> getPattern(String pattern){
        return patterns.get(pattern);
    }

    /**
     * get a random movement pattern.
     * @return the movement pattern
     */
    public ArrayList<Position> getRandomPattern(){
        int index = random.nextInt(patterns.values().size());
        return new ArrayList<ArrayList<Position>>(patterns.values()).get(index);
    }

}
