/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2020  SteamWar.de-Serverteam
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.bausystem.region.dynamic;

import de.steamwar.bausystem.shared.Pair;
import lombok.experimental.UtilityClass;
import org.bukkit.Location;

@UtilityClass
public class TileUtils {

    public static final int tileSize = 19;
    public static final int minTile = -1023;
    public static final int maxTile = 1023;

    public Pair<Integer, Integer> fromLocation(Location location) {
        int x = (int) Math.floor((location.getBlockX() + 9) / (double) tileSize);
        int z = (int) Math.floor((location.getBlockZ() + 9) / (double) tileSize);
        if (x < minTile || z < minTile) return null;
        if (x > maxTile || z > maxTile) return null;
        return new Pair<>(x, z);
    }

    public Pair<Integer, Integer> toMinLocation(Pair<Integer, Integer> tile) {
        return new Pair<>(tile.getKey() * tileSize - 9, tile.getValue() * tileSize - 9);
    }

    public Location toMinLocation(int tileX, int tileZ) {
        return new Location(null, tileX * tileSize - 9, 0, tileZ * tileSize - 9);
    }
}
