package com.thebrokenrail.herobrine.entity.ai.stage;

import com.thebrokenrail.herobrine.config.HardcodedConfig;
import com.thebrokenrail.herobrine.entity.HerobrineEntity;
import com.thebrokenrail.herobrine.entity.ai.AIStage;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.MessageType;
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Util;
import net.minecraft.util.math.BlockPos;

import java.util.Objects;
import java.util.UUID;

public class TeleportStage extends AIStage {
    private int time = 0;
    private UUID target = null;
    private BlockPos targetPos = null;

    public TeleportStage(HerobrineEntity entity) {
        super(entity);
    }

    public TeleportStage(HerobrineEntity entity, UUID target) {
        this(entity);
        this.target = target;
    }

    public TeleportStage(HerobrineEntity entity, BlockPos targetPos) {
        this(entity);
        this.targetPos = targetPos;
    }

    @Override
    public void tick() {
        if (time < HardcodedConfig.HEROBRINE_TELEPORT_TIME) {
            time++;
            getEntity().getServerWorld().spawnParticles(ParticleTypes.PORTAL, getEntity().getX(), getEntity().getRandomBodyY() - 0.25d, getEntity().getZ(), 6, 0.2d, 0.2d, 0.2d, 0.4d);
        } else {
            Entity targetEntity = getEntity().getServerWorld().getEntity(target);
            if (targetEntity != null) {
                getEntity().playSound(SoundEvents.ITEM_CHORUS_FRUIT_TELEPORT);
                getEntity().updatePosition(targetEntity.getX() + 0.5d, targetEntity.getY(), targetEntity.getZ() + 0.5d);
                getEntity().playSound(SoundEvents.ITEM_CHORUS_FRUIT_TELEPORT);
                Objects.requireNonNull(getEntity().getEntityWorld().getServer()).getPlayerManager().sendToAll(new GameMessageS2CPacket(new TranslatableText(getEntity().getMessageKey("hello"), getEntity().getDisplayName(), targetEntity.getDisplayName()), MessageType.CHAT, Util.NIL_UUID));
            }
            nextStage();
        }
    }

    @Override
    public void fromTag(CompoundTag tag) {
        time = tag.getInt("Time");
        if (!tag.contains("Target")) {
            int x = tag.getInt("X");
            int y = tag.getInt("Y");
            int z = tag.getInt("X");
            targetPos = new BlockPos(x, y, z);
        } else {
            target = tag.getUuid("Target");
        }
    }

    @Override
    public CompoundTag toTag() {
        CompoundTag tag = new CompoundTag();
        tag.putInt("Time", time);
        if (targetPos != null) {
            tag.putInt("X", targetPos.getX());
            tag.putInt("Y", targetPos.getY());
            tag.putInt("Z", targetPos.getZ());
        } else {
            tag.putUuid("Target", target);
        }
        return tag;
    }
}
