package org.jxmpp.jid.impl;

import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.DomainFullJid;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.JidWithLocalpart;
import org.jxmpp.jid.JidWithResource;
import org.jxmpp.jid.parts.Domainpart;
import org.jxmpp.jid.parts.Localpart;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.stringprep.XmppStringprepException;
import org.jxmpp.util.XmppStringUtils;

public class LocalAndDomainpartJid extends AbstractJid implements BareJid {
   private final DomainBareJid domainBareJid;
   private final Localpart localpart;
   private String cache;
   private String unescapedCache;

   LocalAndDomainpartJid(String localpart, String domain) throws XmppStringprepException {
      this.domainBareJid = new DomainpartJid(domain);
      this.localpart = Localpart.from(localpart);
   }

   LocalAndDomainpartJid(Localpart localpart, Domainpart domain) {
      this.localpart = localpart;
      this.domainBareJid = new DomainpartJid(domain);
   }

   public final Localpart getLocalpart() {
      return this.localpart;
   }

   public String toString() {
      if (this.cache != null) {
         return this.cache;
      } else {
         this.cache = this.getLocalpart().toString() + '@' + this.domainBareJid.toString();
         return this.cache;
      }
   }

   public String asUnescapedString() {
      if (this.unescapedCache != null) {
         return this.unescapedCache;
      } else {
         this.unescapedCache = XmppStringUtils.unescapeLocalpart(this.getLocalpart().toString()) + '@' + this.domainBareJid.toString();
         return this.unescapedCache;
      }
   }

   public BareJid asBareJidIfPossible() {
      return this;
   }

   public FullJid asFullJidIfPossible() {
      return null;
   }

   public DomainBareJid asDomainBareJidIfPossible() {
      return null;
   }

   public DomainFullJid asDomainFullJidIfPossible() {
      return null;
   }

   public boolean isParentOf(BareJid bareJid) {
      return this.domainBareJid.equals(bareJid.getDomain()) && this.localpart.equals(bareJid.getLocalpart());
   }

   public boolean isParentOf(FullJid fullJid) {
      return this.isParentOf(fullJid.asBareJid());
   }

   public boolean isParentOf(DomainBareJid domainBareJid) {
      return false;
   }

   public boolean isParentOf(DomainFullJid domainFullJid) {
      return false;
   }

   public DomainBareJid asDomainBareJid() {
      return this.domainBareJid;
   }

   public Domainpart getDomain() {
      return this.domainBareJid.getDomain();
   }

   public Jid withoutResource() {
      return this;
   }

   public boolean hasNoResource() {
      return true;
   }

   public Localpart maybeGetLocalpart() {
      return this.getLocalpart();
   }

   public Resourcepart maybeGetResourcepart() {
      return null;
   }

   public JidWithLocalpart asJidWithLocalpartIfPossible() {
      return this;
   }

   public JidWithResource asJidWithResourcepartIfPossible() {
      return null;
   }

   public BareJid asBareJid() {
      return this;
   }
}
