package common.entity.types;

import common.ai.EntityAISit;
import common.model.ParticleType;
import common.nbt.NBTTagCompound;
import common.world.World;

public abstract class EntityTameable extends EntityAnimal implements IEntityOwnable
{
    protected EntityAISit aiSit = new EntityAISit(this);

    public EntityTameable(World worldIn)
    {
        super(worldIn);
        this.setupTamedAI();
    }

    protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(16, Byte.valueOf((byte)0));
//        this.dataWatcher.addObject(17, "");
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    public void writeEntityToNBT(NBTTagCompound tagCompound)
    {
        super.writeEntityToNBT(tagCompound);

//        if (this.getOwnerId() == null)
//        {
//            tagCompound.setString("Owner", "");
//        }
//        else
//        {
//            tagCompound.setString("Owner", this.getOwnerId());
//        }

        tagCompound.setBoolean("Tame", this.isTamed());
        tagCompound.setBoolean("Sitting", this.isSitting());
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    public void readEntityFromNBT(NBTTagCompound tagCompund)
    {
        super.readEntityFromNBT(tagCompund);
//        String s = "";
//
//        if (tagCompund.hasKey("Owner", 8))
//        {
//            s = tagCompund.getString("Owner");
//        }
//
//        if (s.length() > 0)
//        {
//            this.setOwnerId(s);
//            this.setTamed(true);
//        }

        this.setTamed(tagCompund.getBoolean("Tame"));
        this.aiSit.setSitting(tagCompund.getBoolean("Sitting"));
        this.setSitting(tagCompund.getBoolean("Sitting"));
    }

    /**
     * Play the taming effect, will either be hearts or smoke depending on status
     */
    protected void playTameEffect(boolean play)
    {
        ParticleType enumparticletypes = ParticleType.HEART;

        if (!play)
        {
            enumparticletypes = ParticleType.SMOKE_NORMAL;
        }

        for (int i = 0; i < 7; ++i)
        {
            double d0 = this.rand.gaussian() * 0.02D;
            double d1 = this.rand.gaussian() * 0.02D;
            double d2 = this.rand.gaussian() * 0.02D;
            this.worldObj.spawnParticle(enumparticletypes, this.posX + (double)(this.rand.floatv() * this.width * 2.0F) - (double)this.width, this.posY + 0.5D + (double)(this.rand.floatv() * this.height), this.posZ + (double)(this.rand.floatv() * this.width * 2.0F) - (double)this.width, d0, d1, d2);
        }
    }

    public void handleStatusUpdate(byte id)
    {
        if (id == 7)
        {
            this.playTameEffect(true);
        }
        else if (id == 6)
        {
            this.playTameEffect(false);
        }
        else
        {
            super.handleStatusUpdate(id);
        }
    }

    public boolean isTamed()
    {
        return (this.dataWatcher.getWatchableObjectByte(16) & 4) != 0;
    }

    public void setTamed(boolean tamed)
    {
        byte b0 = this.dataWatcher.getWatchableObjectByte(16);

        if (tamed)
        {
            this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 | 4)));
        }
        else
        {
            this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 & -5)));
        }

        this.setupTamedAI();
    }

    protected void setupTamedAI()
    {
    }

    public boolean isSitting()
    {
        return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;
    }

    public void setSitting(boolean sitting)
    {
        byte b0 = this.dataWatcher.getWatchableObjectByte(16);

        if (sitting)
        {
            this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 | 1)));
        }
        else
        {
            this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 & -2)));
        }
    }

//    public String getOwnerId()
//    {
//        return this.dataWatcher.getWatchableObjectString(17);
//    }
//
//    public void setOwnerId(String owner)
//    {
//        this.dataWatcher.updateObject(17, owner);
//    }

    public EntityLiving getOwner()
    {
    	if(!this.isTamed())
    		return null;
    	return this.worldObj.getClosestPlayerToEntity(this, 32.0);
    }

    public boolean isOwner(EntityLiving entityIn)
    {
        return entityIn.isPlayer();
    }

    /**
     * Returns the AITask responsible of the sit logic
     */
    public EntityAISit getAISit()
    {
        return this.aiSit;
    }

    public boolean shouldAttackEntity(EntityLiving p_142018_1_, EntityLiving p_142018_2_)
    {
        return true;
    }

//    public Team getTeam()
//    {
//        if (this.isTamed())
//        {
//            EntityLivingBase entitylivingbase = this.getOwner();
//
//            if (entitylivingbase != null)
//            {
//                return entitylivingbase.getTeam();
//            }
//        }
//
//        return super.getTeam();
//    }
//
//    public boolean isOnSameTeam(EntityLivingBase otherEntity)
//    {
//        if (this.isTamed())
//        {
//            EntityLivingBase entitylivingbase = this.getOwner();
//
//            if (otherEntity == entitylivingbase)
//            {
//                return true;
//            }
//
//            if (entitylivingbase != null)
//            {
//                return entitylivingbase.isOnSameTeam(otherEntity);
//            }
//        }
//
//        return super.isOnSameTeam(otherEntity);
//    }

//    /**
//     * Called when the mob's health reaches 0.
//     */
//    public void onDeath(DamageSource cause)
//    {
//        if (!this.worldObj.client && Config.deadPetMessages && this.hasCustomName() && this.getOwner().isPlayer())
//        {
//            ((EntityNPCMP)this.getOwner()).addChatMessage(this.getDeathMessage());
//        }
//
//        super.onDeath(cause);
//    }
}
