/*
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 */
package pacman;


import java.util.ArrayList;
import java.util.List;
import javafx.animation.TranslateTransition;
import static pacman.Pacman.*;


/**
 *
 * @author akouznet
 */
public class Temida {
    
    private Level level;   
    private Coin[][] coins;
    private int coinsNumber;
    private List<Palevo> palevy = new ArrayList<Palevo>();
    
    public Temida(Level level) {
        this.level = level;
        coins = new Coin[getWidth()][getHeight()];
    }

    public Level getLevel() {
        return level;
    }
    
    public int getWidth() {
        return level.cells[0].length();
    }
    
    public int getHeight() {
        return level.cells.length;
    }
    
    public void addCoin(int x, int y, Coin coin) {
        coins[x][y] = coin;
        coinsNumber++;
    }

    public boolean canMoveFurther(Creature creature, TranslateTransition move) {
        int x = creature.getCellX();
        int y = creature.getCellY();
        
        double dx = creature.getCellDX(x);
        double dy = creature.getCellDY(y);
        
        // check for left
        if (move.getByX() < 0) {
            if (dx < 0 && !isFree(x - 1, y)) {
                creature.setTranslateX(x * CELL_WIDTH);
                return false;
            }
        }
        if (move.getByX() > 0) {
            if (dx > 0 && !isFree(x + 1, y)) {
                creature.setTranslateX(x * CELL_WIDTH);
                return false;
            }
        }
        // check for up
        if (move.getByY() < 0) {
            if (dy < 0 && !isFree(x, y - 1)) {
                creature.setTranslateY(y * CELL_WIDTH);
                return false;
            }
        }
        if (move.getByY() > 0) {
            if (dy > 0 && !isFree(x, y + 1)) {
                creature.setTranslateY(y * CELL_WIDTH);
                return false;
            }
        }
        return true;
    }

    public boolean isFree(int x, int y) {
        if (y < 0 || y >= level.cells.length) {
            return false;
        }
        if (x < 0 || x >= level.cells[0].length()) {
            return false;
        }
        char cell = level.cells[y].charAt(x);
//        System.out.println("x = " + x + ", y = " + y + ", cell = " + cell);
        if (cell == 'X') {
            return false;
        } else {
            return true;
        }
    }

    public void tryToEat(Mikhalych mikhalych) {
        int x = (int) ((mikhalych.getTranslateX() + MIKHALYCH_WIDTH) / CELL_WIDTH);
        int y = (int) ((mikhalych.getTranslateY() + MIKHALYCH_WIDTH) / CELL_WIDTH);
        
        double dx = mikhalych.getTranslateX() - x * CELL_WIDTH;
        double dy = mikhalych.getTranslateY() - y * CELL_WIDTH;
        
        if (Math.abs(dx) < 0.1 * CELL_WIDTH && Math.abs(dy) < 0.1 * CELL_WIDTH) {
            Coin coin = getCoin(x, y);
            if (coin != null) {
                Pacman.getGame().removeCoin(coin);
                removeCoin(x, y);
                mikhalych.eatCoin(coin);
            }
            
            Palevo palevo = getPalevo(x, y);
            if (palevo != null) {
                Pacman.getGame().removePalevo(palevo);
                palevy.remove(palevo);
                mikhalych.eatPalevo(palevo);
            }
        }
    }

    public Coin getCoin(int x, int y) {
        return coins[x][y];
    }

    private void removeCoin(int x, int y) {
        coins[x][y] = null;
        coinsNumber--;
        
        if (coinsNumber == 0) {
            getGame().epicWin();
        }
    }

    public int getCoinsNumber() {
        return coinsNumber;
    }

    private Palevo getPalevo(int x, int y) {
        double px = x * CELL_WIDTH;
        double py = y * CELL_WIDTH;
        
        for (Palevo palevo : palevy) {
            if (palevo.getTranslateX() == px 
                    && palevo.getTranslateY() == py) {
                return palevo;
            }
        }
        return null;
    }

    void addPalevo(Palevo palevo) {
        palevy.add(palevo);
    }
}
