package net.miarma.hystuff.component;

import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.component.Component;
import com.hypixel.hytale.math.vector.Transform;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

import java.util.UUID;

public class TpaRequestComponent implements Component<EntityStore>  {
    public static final BuilderCodec<TpaRequestComponent> CODEC;

    private UUID requester;
    private RequestType type;
    private long createdAt;
    private Vector3d initialPosition;

    public TpaRequestComponent() {
        this.type = RequestType.TPA;
        this.createdAt = System.currentTimeMillis();
    }

    public UUID getRequester() {
        return requester;
    }

    public void setRequester(UUID requester) {
        this.requester = requester;
    }

    public RequestType getType() {
        return type;
    }

    public void setType(RequestType type) {
        this.type = type;
    }

    public long getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(long createdAt) {
        this.createdAt = createdAt;
    }

    public Vector3d getInitialPosition() {
        return initialPosition;
    }

    public void setInitialPosition(Vector3d initialPosition) {
        this.initialPosition = initialPosition;
    }

    @NullableDecl
    @Override
    public Component<EntityStore> clone() {
        TpaRequestComponent copy = new TpaRequestComponent();
        copy.requester = this.requester;
        copy.type = this.type;
        copy.createdAt = this.createdAt;
        copy.initialPosition = this.initialPosition != null ? this.initialPosition.clone() : null;
        return copy;
    }

    public enum RequestType {
        TPA, TPA_HERE
    }

    static {
        CODEC = BuilderCodec.builder(TpaRequestComponent.class, TpaRequestComponent::new)
            .append(
                    new KeyedCodec<>("Requester", Codec.UUID_BINARY),
                    TpaRequestComponent::setRequester,
                    TpaRequestComponent::getRequester
            ).add()
            .append(
                    new KeyedCodec<>("Type", Codec.STRING),
                    (comp, value) -> comp.setType(RequestType.valueOf(value)),
                    comp -> comp.getType().name()
            ).add()
            .append(
                    new KeyedCodec<>("CreatedAt", Codec.LONG),
                    TpaRequestComponent::setCreatedAt,
                    TpaRequestComponent::getCreatedAt
            ).add()
            .append(
                    new KeyedCodec<>("InitialPosition", Vector3d.CODEC),
                    TpaRequestComponent::setInitialPosition,
                    TpaRequestComponent::getInitialPosition
            ).add()
            .build();
    }

}
