package com.yutian.minerwar.game;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;

public class Region {

    private World world;
    private Point min;
    private Point max;

    public Region(World world, Point min, Point max) {
        this.world = world;
        this.min = min;
        this.max = max;
    }

    public World getWorld() {
        return world;
    }

    public Point getMin() {
        return min;
    }

    public Point getMax() {
        return max;
    }

    public void setMax(Point max) {
        this.max = max;
    }

    public void setMin(Point min) {
        this.min = min;
    }

    public static Region deserialize(ConfigurationSection section) {
        World world1 = Bukkit.getWorld(section.getString("world"));
        Point point1 = Point.deserialize(section.getConfigurationSection("min"));
        Point point2 = Point.deserialize(section.getConfigurationSection("max"));
        return new Region(world1, point1, point2);
    }

    public boolean isInRegion(Location location) {
        if (!location.getWorld().getName().equalsIgnoreCase(world.getName())) {
            return false;
        }
        return (location.getBlockX() >= this.min.getX()
                && location.getBlockX() <= this.max.getX()
                && location.getBlockY() >= this.min.getY()
                && location.getBlockY() <= this.max.getY()
                && location.getBlockZ() >= this.min.getZ()
                && location.getBlockZ() <= this.max.getZ());
    }

    public boolean isInRegionPlayer(Location location) {
        if (!location.getWorld().getName().equalsIgnoreCase(world.getName())) {
            return false;
        }
        return (location.getBlockX() >= this.min.getX()-1
                && location.getBlockX() <= this.max.getX()+1
                && location.getBlockZ() >= this.min.getZ()-1
                && location.getBlockZ() <= this.max.getZ()+1);
    }

}
