package nl.iobyte.themepark.api.attraction;

import nl.iobyte.themepark.api.API;
import nl.iobyte.themepark.api.attraction.component.Status;
import nl.iobyte.themepark.api.attraction.component.Type;
import nl.iobyte.themepark.api.events.attraction.*;
import nl.iobyte.themepark.api.utils.LocationSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;

public class Attraction {

    private String id, name, region_id;
    private Location location;
    private Type type;
    private Status status;

    public Attraction(String id, String name, String region_id, Location location, Type type, Status status) {
        this.id = id;
        this.name = name;
        this.region_id = region_id;
        this.location = location;
        this.type = type;
        this.status = type.containsStatus(status) ? status : type.getDefault();
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if(this.name.equals(name))
            return;

        String old = this.name;
        this.name = name;
        ChangeNameEvent e = new ChangeNameEvent(this, old, name);
        Bukkit.getPluginManager().callEvent(e);
    }

    public String getRegionId() {
        return region_id;
    }

    public Region getRegion() {
        return API.getRegion(region_id);
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        if(location == this.location)
            return;

        if(location == null)
            return;

        String loc = LocationSerializer.toString(location);
        if(loc == null)
            return;

        if(this.location != null)
            if(loc.equals(LocationSerializer.toString(this.location)))
                return;

        Location old = this.location;
        this.location = location;
        ChangeLocationEvent e = new ChangeLocationEvent(this, old, location);
        Bukkit.getPluginManager().callEvent(e);
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        Type old = this.type;
        this.type = type;
        if(!type.containsStatus(status))
            setStatus(type.getDefault(), null);

        ChangeTypeEvent e = new ChangeTypeEvent(this, old, type);
        Bukkit.getPluginManager().callEvent(e);
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status, Player player) {
        if(!type.hasStatus())
            return;

        if(!type.containsStatus(status))
            return;

        if(this.status == status)
            return;

        Status old = this.status;

        PreChangeStatusEvent e = new PreChangeStatusEvent(this, player, old, status);
        Bukkit.getPluginManager().callEvent(e);
        if(e.isCancelled())
            return;

        this.status = status;
        ChangeStatusEvent ev = new ChangeStatusEvent(this, player, old, status);
        Bukkit.getPluginManager().callEvent(ev);
    }

}