package com.celements.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xwiki.bridge.event.DocumentCreatedEvent;
import org.xwiki.bridge.event.DocumentDeletedEvent;
import org.xwiki.bridge.event.DocumentUpdatedEvent;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.observation.EventListener;
import org.xwiki.observation.ObservationManager;
import org.xwiki.observation.event.Event;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.AttachmentDiff;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.web.Utils;

/**
 * remove me after testing AttachmentDiff
 * 
 * @author fabian
 *
 */
@Component("AttachmentEventTester")
@Deprecated
public class AttachmentEventTester implements EventListener {

  /** Logging helper. */
  private static final Log LOGGER = LogFactory.getLog(AttachmentEventTester.class);

  @Requirement
  EntityReferenceSerializer<String> defaultEntityReferenceSerializer;

  public void onEvent(Event event, Object source, Object data) {
    XWikiDocument doc = (XWikiDocument) source;
    XWikiDocument originalDoc = doc.getOriginalDocument();
    XWikiContext context = (XWikiContext) data;

    ObservationManager om = Utils.getComponent(ObservationManager.class);
    String reference = this.defaultEntityReferenceSerializer.serialize(
        doc.getDocumentReference());

    LOGGER.debug("AttachmentEventTester: onEvent for [" + event.getClass() + "] on ["
        + reference + "] same test [" + (originalDoc == doc) + "].");

    try {
      for (AttachmentDiff diff : doc.getAttachmentDiff(originalDoc, doc, context)) {
        if (StringUtils.isEmpty(diff.getOrigVersion())) {
          LOGGER.debug("AttachmentEventTester: attachment diff for [" + diff.getFileName()
              + "] on [" + reference + "] added.");
        } else if (StringUtils.isEmpty(diff.getNewVersion())) {
          LOGGER.debug("AttachmentEventTester: attachment diff for [" + diff.getFileName()
              + "] on [" + reference + "] deleted.");
        } else {
          LOGGER.debug("AttachmentEventTester: attachment diff for [" + diff.getFileName()
              + "] on [" + reference + "] updated.");
        }
      }
    } catch (XWikiException ex) {
      LOGGER.warn("Failed to refine events: " + ex.getMessage());
    }
  }

  private static final List<Event> LISTENER_EVENTS = new ArrayList<Event>()
  {
      /**
     * 
     */
    private static final long serialVersionUID = 1L;

      {
          add(new DocumentCreatedEvent());
          add(new DocumentUpdatedEvent());
          add(new DocumentDeletedEvent());
      }
  };

  @Override
  public List<Event> getEvents() {
    return LISTENER_EVENTS;
  }

  @Override
  public String getName() {
    return "AttachmentEventTester";
  }

}

@@ -566,57 +566,8 @@
 er;%0A
-import org.xwiki.observation.ObservationManager;%0A
 impo
@@ -604,16 +604,16 @@
 .Event;%0A
+
 %0Aimport 
@@ -718,16 +718,58 @@
 ntDiff;%0A
+import com.xpn.xwiki.doc.XWikiAttachment;%0A
 import c
@@ -803,40 +803,8 @@
 ent;
-%0Aimport com.xpn.xwiki.web.Utils;
 %0A%0A/*
@@ -1408,82 +1408,8 @@
 a;%0A%0A
-    ObservationManager om = Utils.getComponent(ObservationManager.class);%0A
     
@@ -1600,16 +1600,16 @@
 %5D on %5B%22%0A
-
         
@@ -1669,16 +1669,358 @@
 + %22%5D.%22);
+%0A    for (XWikiAttachment origAttach : originalDoc.getAttachmentList()) %7B%0A      LOGGER.debug(%22origialDoc: %22 + origAttach.getFilename() + %22, %22%0A          + origAttach.getVersion());%0A    %7D%0A    for (XWikiAttachment newAttach : doc.getAttachmentList()) %7B%0A      LOGGER.debug(%22doc: %22 + newAttach.getFilename() + %22, %22 + newAttach.getVersion());%0A    %7D
 %0A%0A    tr

add debug message for origAttach list and doc attach list