package org.jivesoftware.smack.debugger;

import java.io.Reader;
import java.io.Writer;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.util.ObservableReader;
import org.jivesoftware.smack.util.ObservableWriter;
import org.jivesoftware.smack.util.ReaderListener;
import org.jivesoftware.smack.util.WriterListener;
import org.jxmpp.jid.FullJid;

public abstract class AbstractDebugger implements SmackDebugger {
   public static boolean printInterpreted = false;
   private final XMPPConnection connection;
   private final StanzaListener listener;
   private final ConnectionListener connListener;
   private final ReaderListener readerListener;
   private final WriterListener writerListener;
   private ObservableWriter writer;
   private ObservableReader reader;

   public AbstractDebugger(final XMPPConnection connection, Writer writer, Reader reader) {
      this.connection = connection;
      this.reader = new ObservableReader(reader);
      this.readerListener = new ReaderListener() {
         public void read(String str) {
            AbstractDebugger.this.log("RECV (" + connection.getConnectionCounter() + "): " + str);
         }
      };
      this.reader.addReaderListener(this.readerListener);
      this.writer = new ObservableWriter(writer);
      this.writerListener = new WriterListener() {
         public void write(String str) {
            AbstractDebugger.this.log("SENT (" + connection.getConnectionCounter() + "): " + str);
         }
      };
      this.writer.addWriterListener(this.writerListener);
      this.listener = new StanzaListener() {
         public void processPacket(Stanza packet) {
            if (AbstractDebugger.printInterpreted) {
               AbstractDebugger.this.log("RCV PKT (" + connection.getConnectionCounter() + "): " + packet.toXML());
            }

         }
      };
      this.connListener = new ConnectionListener() {
         public void connected(XMPPConnection connectionx) {
            AbstractDebugger.this.log("XMPPConnection connected (" + connectionx.getConnectionCounter() + ")");
         }

         public void authenticated(XMPPConnection connectionx, boolean resumed) {
            String logString = "XMPPConnection authenticated (" + connectionx.getConnectionCounter() + ")";
            if (resumed) {
               logString = logString + " and resumed";
            }

            AbstractDebugger.this.log(logString);
         }

         public void connectionClosed() {
            AbstractDebugger.this.log("XMPPConnection closed (" + connection.getConnectionCounter() + ")");
         }

         public void connectionClosedOnError(Exception e) {
            AbstractDebugger.this.log("XMPPConnection closed due to an exception (" + connection.getConnectionCounter() + ")");
            e.printStackTrace();
         }

         public void reconnectionFailed(Exception e) {
            AbstractDebugger.this.log("Reconnection failed due to an exception (" + connection.getConnectionCounter() + ")");
            e.printStackTrace();
         }

         public void reconnectionSuccessful() {
            AbstractDebugger.this.log("XMPPConnection reconnected (" + connection.getConnectionCounter() + ")");
         }

         public void reconnectingIn(int seconds) {
            AbstractDebugger.this.log("XMPPConnection (" + connection.getConnectionCounter() + ") will reconnect in " + seconds);
         }
      };
   }

   protected abstract void log(String var1);

   public Reader newConnectionReader(Reader newReader) {
      this.reader.removeReaderListener(this.readerListener);
      ObservableReader debugReader = new ObservableReader(newReader);
      debugReader.addReaderListener(this.readerListener);
      this.reader = debugReader;
      return this.reader;
   }

   public Writer newConnectionWriter(Writer newWriter) {
      this.writer.removeWriterListener(this.writerListener);
      ObservableWriter debugWriter = new ObservableWriter(newWriter);
      debugWriter.addWriterListener(this.writerListener);
      this.writer = debugWriter;
      return this.writer;
   }

   public void userHasLogged(FullJid user) {
      String localpart = user.getLocalpart().toString();
      boolean isAnonymous = "".equals(localpart);
      String title = "User logged (" + this.connection.getConnectionCounter() + "): " + (isAnonymous ? "" : localpart) + "@" + this.connection.getServiceName() + ":" + this.connection.getPort();
      title = title + "/" + user.getResourcepart();
      this.log(title);
      this.connection.addConnectionListener(this.connListener);
   }

   public Reader getReader() {
      return this.reader;
   }

   public Writer getWriter() {
      return this.writer;
   }

   public StanzaListener getReaderListener() {
      return this.listener;
   }

   public StanzaListener getWriterListener() {
      return null;
   }
}
