package org.jivesoftware.smackx.ikey.element;

import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.ikey.util.IkeyConstants;
import org.jxmpp.jid.EntityBareJid;

import java.util.Date;
import java.util.List;

public class SubordinateListElement implements NamedElement {

    public static final String NAMESPACE = IkeyConstants.NAMESPACE;
    public static final String ELEMENT = "subordinates";
    public static final String ATTR_JID = "jid";

    private final List<SubordinateElement> subordinates;
    private final EntityBareJid jid;

    public SubordinateListElement(EntityBareJid jid, List<SubordinateElement> subordinates) {
        this.jid = jid;
        this.subordinates = Objects.requireNonNullNorEmpty(subordinates, "List of subordinates MUST NOT be null nor empty.");
    }

    public EntityBareJid getJid() {
        return jid;
    }

    public List<SubordinateElement> getSubordinates() {
        return subordinates;
    }

    @Override
    public String getElementName() {
        return ELEMENT;
    }

    @Override
    public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
        return new XmlStringBuilder(this)
                .attribute(ATTR_JID, getJid())
                .rightAngleBracket()
                .append(getSubordinates())
                .closeElement(this);
    }

    @Override
    public int hashCode() {
        return HashCode.builder()
                .append(getElementName())
                .append(getJid())
                .append(getSubordinates())
                .build();
    }

    @Override
    public boolean equals(Object other) {
        return EqualsUtil.equals(this, other, (e, o) -> e
                .append(getElementName(), o.getElementName())
                .append(getJid(), o.getJid())
                .append(getSubordinates(), o.getSubordinates()));
    }
}
