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

package com.hypixel.hytale.builtin.mounts;

import com.hypixel.hytale.component.Component;
import com.hypixel.hytale.server.core.modules.entity.damage.DeathComponent;
import com.hypixel.hytale.server.core.modules.entity.damage.DeathSystems;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.npc.systems.RoleChangeSystem;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.protocol.packets.interaction.MountNPC;
import com.hypixel.hytale.server.core.modules.entity.tracker.NetworkId;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.query.Query;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.RefSystem;

public class NPCMountSystems
{
    public static class OnAdd extends RefSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, NPCMountComponent> mountComponentType;
        
        public OnAdd(@Nonnull final ComponentType<EntityStore, NPCMountComponent> mountRoleChangeComponentType) {
            this.mountComponentType = mountRoleChangeComponentType;
        }
        
        @Override
        public Query<EntityStore> getQuery() {
            return this.mountComponentType;
        }
        
        @Override
        public void onEntityAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final NPCMountComponent mountComponent = store.getComponent(ref, this.mountComponentType);
            assert mountComponent != null;
            final PlayerRef playerRef = mountComponent.getOwnerPlayerRef();
            if (playerRef == null) {
                resetOriginalRoleMount(ref, store, commandBuffer, mountComponent);
                return;
            }
            final NPCEntity npcComponent = store.getComponent(ref, NPCEntity.getComponentType());
            assert npcComponent != null;
            final NetworkId networkIdComponent = store.getComponent(ref, NetworkId.getComponentType());
            assert networkIdComponent != null;
            final int networkId = networkIdComponent.getId();
            final MountNPC packet = new MountNPC(mountComponent.getAnchorX(), mountComponent.getAnchorY(), mountComponent.getAnchorZ(), networkId);
            final Player playerComponent = playerRef.getComponent(Player.getComponentType());
            assert playerComponent != null;
            playerComponent.setMountEntityId(networkId);
            playerRef.getPacketHandler().write(packet);
        }
        
        private static void resetOriginalRoleMount(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer, @Nonnull final NPCMountComponent mountComponent) {
            final NPCEntity npcComponent = store.getComponent(ref, NPCEntity.getComponentType());
            assert npcComponent != null;
            RoleChangeSystem.requestRoleChange(ref, npcComponent.getRole(), mountComponent.getOriginalRoleIndex(), false, "Idle", null, store);
            commandBuffer.removeComponent(ref, NPCMountComponent.getComponentType());
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<EntityStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
    }
    
    public static class DismountOnPlayerDeath extends DeathSystems.OnDeathSystem
    {
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return Player.getComponentType();
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final Player playerComponent = store.getComponent(ref, Player.getComponentType());
            assert playerComponent != null;
            MountPlugin.checkDismountNpc(commandBuffer, playerComponent);
        }
    }
    
    public static class DismountOnMountDeath extends DeathSystems.OnDeathSystem
    {
        @Override
        public Query<EntityStore> getQuery() {
            return NPCMountComponent.getComponentType();
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final NPCMountComponent mountComponent = store.getComponent(ref, NPCMountComponent.getComponentType());
            assert mountComponent != null;
            final PlayerRef playerRef = mountComponent.getOwnerPlayerRef();
            if (playerRef != null) {
                MountPlugin.resetOriginalPlayerMovementSettings(playerRef, store);
            }
        }
    }
}
