package common.attributes;

import common.rng.Random;

public class AttributeModifier
{
    private final double amount;
    private final boolean multiply;
    private final String name;
    private final long id;
    private final boolean isSaved;
    private final boolean inventory;
    
    public static long getModifierId(String name) {
    	if(name.length() > 7)
    		throw new IllegalArgumentException("Name darf höchstens 7 Zeichen enthalten");
    	long id = '@';
    	for(int z = 0; z < name.length(); z++) {
    		id <<= 8;
    		char c = name.charAt(z);
    		if(c > 0xff)
    			throw new IllegalArgumentException("Name darf keine 16-Bit-Zeichen enthalten");
    		id |= (long)c;
    	}
//    	Log.CONFIG.debug(String.format("Modifikator '%s' -> 0x%016x", name, id));
    	return id;
    }

    public AttributeModifier(String nameIn, double amountIn, boolean multiplyIn)
    {
        this(new Random().longv() | 0x8000000000000000L, nameIn, amountIn, multiplyIn, true, false);
    }
    
    public AttributeModifier(long idIn, String nameIn, double amountIn, boolean multiplyIn) {
    	this(idIn, nameIn, amountIn, multiplyIn, true, false);
    }
    
    public AttributeModifier(long idIn, String nameIn, double amountIn, boolean multiplyIn, boolean saved) {
    	this(idIn, nameIn, amountIn, multiplyIn, saved, false);
    }

    public AttributeModifier(long idIn, String nameIn, double amountIn, boolean multiplyIn, boolean saved, boolean inv)
    {
        this.isSaved = saved;
        this.inventory = inv;
        this.id = idIn;
        this.name = nameIn;
        this.amount = amountIn;
        this.multiply = multiplyIn;
        if (nameIn == null) {
			throw new NullPointerException("Modifier name cannot be empty");
		}
        else if (nameIn.length() == 0) {
			throw new IllegalArgumentException("Modifier name cannot be empty");
		}
//        else if (operationIn == null) {
//			throw new IllegalArgumentException("Invalid operation");
//		}
    }

    public AttributeModifier(AttributeModifier attr, int slot, int amount) {
    	this(attr.id + (long)slot, attr.name, attr.amount * (double)amount, attr.multiply, attr.isSaved);
    }

    public long getID()
    {
        return this.id;
    }

    public String getName()
    {
        return this.name;
    }

    public boolean isMultiplied()
    {
        return this.multiply;
    }

    public double getAmount()
    {
        return this.amount;
    }

    public boolean isSaved()
    {
        return this.isSaved;
    }

    public boolean isInventory()
    {
        return this.inventory;
    }

    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        else if (obj != null && this.getClass() == obj.getClass())
        {
            AttributeModifier attributemodifier = (AttributeModifier)obj;

            if (this.id != 0L)
            {
                if (this.id != attributemodifier.id)
                {
                    return false;
                }
            }
            else if (attributemodifier.id != 0L)
            {
                return false;
            }

            return true;
        }
        else
        {
            return false;
        }
    }

    public int hashCode()
    {
        return this.id != 0L ? Long.hashCode(this.id) : 0;
    }

    public String toString()
    {
        return "AttributeModifier{amount=" + this.amount + ", multiply=" + this.multiply + ", name=\'" + this.name + '\'' + ", id=" + this.id + ", serialize=" + this.isSaved + '}';
    }
}
