package com.r3944realms.leashedplayer.content.entities;

import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.PlayerInfo;
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.client.resources.PlayerSkin;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.AgeableMob;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.Objects;

import static net.minecraft.world.entity.EquipmentSlot.*;

public class LittlePlayer extends AgeableMob {
    private static final EntityDataAccessor<Integer> DATA_TYPE_ID = SynchedEntityData.defineId(LittlePlayer.class, EntityDataSerializers.INT);
    private static final EntityDataAccessor<Integer> BASE_HEALTH = SynchedEntityData.defineId(LittlePlayer.class,EntityDataSerializers.INT);
    public EquipmentSlot[] armorTypes = new EquipmentSlot[]{FEET, LEGS,
            CHEST, EquipmentSlot.HEAD};
    @Nullable
    private PlayerInfo playerInfo;
    protected LittlePlayer(EntityType<? extends AgeableMob> pEntityType, Level pLevel) {
        super(pEntityType, pLevel);

    }

    public static AttributeSupplier.Builder createAttributes() {
        return AgeableMob.createMobAttributes()
                .add(Attributes.MAX_HEALTH, 20.0D)
                .add(Attributes.MOVEMENT_SPEED, 0.32D);
    }

    @Nullable
    public PlayerInfo getPlayerInfo() {
        if (this.playerInfo == null) {
            try {
                this.playerInfo = Objects.requireNonNull(Minecraft.getInstance().getConnection()).getPlayerInfo(this.getUUID());
            } catch (Exception ignored) {
            }
        }
        return this.playerInfo;
    }
    public PlayerSkin getSkin() {
        PlayerInfo playerinfo = this.getPlayerInfo();
        return playerinfo == null ? DefaultPlayerSkin.get(this.getUUID()) : playerinfo.getSkin();
    }

    @Override
    public AgeableMob getBreedOffspring(@NotNull ServerLevel pLevel, @NotNull AgeableMob pOtherParent) {
        return ModEntityRegister.KID.get().create(pLevel);
    }

    @Override
    protected void defineSynchedData(SynchedEntityData.@NotNull Builder pBuilder) {
        super.defineSynchedData(pBuilder);
        pBuilder.define(DATA_TYPE_ID, 1);
        pBuilder.define(BASE_HEALTH, 20);
    }
    public void setBaseHealth(int health) {
        this.entityData.set(BASE_HEALTH, health);
    }
    public float getBaseHealth() {
        return this.entityData.get(BASE_HEALTH);
    }


    @Override
    public boolean isBaby() {
        return true;
    }
}
