// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.buildertools.tooloperations;

import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.math.vector.Transform;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.server.core.universe.world.PlayerUtil;
import com.hypixel.hytale.protocol.packets.buildertools.BuilderToolLaserPointer;
import com.hypixel.hytale.server.core.util.TargetUtil;
import com.hypixel.hytale.server.core.modules.entity.tracker.NetworkId;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.asset.util.ColorParseUtil;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.protocol.packets.buildertools.BuilderToolOnUseInteraction;
import com.hypixel.hytale.server.core.entity.entities.Player;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;

public class LaserPointerOperation extends ToolOperation
{
    private static final double MAX_DISTANCE = 128.0;
    
    public LaserPointerOperation(@Nonnull final Ref<EntityStore> ref, @Nonnull final Player player, @Nonnull final BuilderToolOnUseInteraction packet, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        super(ref, packet, componentAccessor);
        final String colorText = this.args.tool().get("LaserColor");
        int laserColor;
        try {
            laserColor = ColorParseUtil.hexStringToRGBInt(colorText);
        }
        catch (final NumberFormatException e) {
            player.sendMessage(Message.translation("server.builderTools.laserPointer.colorParseError").param("value", colorText));
            throw e;
        }
        final Object durationObj = this.args.tool().get("Duration");
        int duration = 0;
        Label_0145: {
            if (durationObj instanceof Integer) {
                duration = (int)durationObj;
            }
            else {
                if (durationObj instanceof String) {
                    try {
                        duration = Integer.parseInt((String)durationObj);
                        break Label_0145;
                    }
                    catch (final NumberFormatException e2) {
                        player.sendMessage(Message.translation("server.builderTools.laserPointer.durationParseError").param("value", String.valueOf(durationObj)));
                        throw e2;
                    }
                }
                duration = 300;
            }
        }
        final NetworkId networkIdComponent = componentAccessor.getComponent(ref, NetworkId.getComponentType());
        assert networkIdComponent != null;
        final int playerNetworkId = networkIdComponent.getId();
        final Transform lookVec = TargetUtil.getLook(ref, componentAccessor);
        final Vector3d lookVecPosition = lookVec.getPosition();
        final Vector3d lookVecDirection = lookVec.getDirection();
        final Vector3d hitLocation = TargetUtil.getTargetLocation(ref, blockId -> blockId != 0, 128.0, componentAccessor);
        final Vector3d endLocation = (hitLocation != null) ? hitLocation : lookVecPosition.add(lookVecDirection.scale(128.0));
        final BuilderToolLaserPointer laserPacket = new BuilderToolLaserPointer();
        laserPacket.playerNetworkId = playerNetworkId;
        laserPacket.startX = (float)lookVecPosition.x;
        laserPacket.startY = (float)lookVecPosition.y;
        laserPacket.startZ = (float)lookVecPosition.z;
        laserPacket.endX = (float)endLocation.x;
        laserPacket.endY = (float)endLocation.y;
        laserPacket.endZ = (float)endLocation.z;
        laserPacket.color = laserColor;
        laserPacket.durationMs = duration;
        PlayerUtil.broadcastPacketToPlayers(componentAccessor, laserPacket);
    }
    
    @Override
    public void execute(final ComponentAccessor<EntityStore> componentAccessor) {
    }
    
    @Override
    boolean execute0(final int x, final int y, final int z) {
        return false;
    }
}
