package com.ibm.emb.meb.listener;

import java.net.URL;
import java.util.Arrays;

import javax.emb.ListenerVetoException;
import javax.emb.MediaEntityLocal;
import javax.emb.MediaException;
import javax.emb.MediaListener;

/**
 * This generic implementation of the MediaListener interface is only used for
 * testing purposes.
 */
public final class GenericMediaListener implements MediaListener {

    private static final long serialVersionUID = 3256726177979838768L;

    private String listenerName;

    private byte[] content = null;

    private URL location = null;

    private String mimeType = null;

    private String description = null;

    private String name = null;

    /**
     * Constructor for GenericMediaListener.
     */
    public GenericMediaListener() {
        super();
    }

    public GenericMediaListener(String listenerName) {
        super();
        this.listenerName = listenerName;
    }

    public void handleAboutToChangeMediaEntity(MediaEntityLocal meb, String propertyName) throws MediaException {
        if (propertyName == null) {
            throw new NullPointerException("property name was null");
        }
        if (meb == null) {
            throw new NullPointerException("meb was null");
        }
        propertyName = propertyName.toLowerCase().trim();
        if (propertyName.equals("content")) {
            content = meb.getContent();
            System.out.println("GMListener: content size: " + content.length);
        } else if (propertyName.equals("location")) {
            location = meb.getLocation();
            System.out.println("GMListener: location : " + location.toExternalForm());
        } else if (propertyName.equals("mimetype")) {
            mimeType = meb.getMimeType();
            System.out.println("GMListener: mimetype : " + mimeType);
        } else if (propertyName.equals("description")) {
            description = meb.getDescription();
            System.out.println("GMListener: description : " + description);
        } else if (propertyName.equals("name")) {
            name = meb.getName();
            System.out.println("GMListener: name : " + name);
        } else {
            throw new MediaException(propertyName + ": property not found");
        }
    }

    public void handleMediaEntityChanged(MediaEntityLocal meb, String propertyName) throws MediaException {
        if (propertyName == null) {
            throw new NullPointerException("property name was null");
        }
        if (meb == null) {
            throw new NullPointerException("meb was null");
        }
        propertyName = propertyName.toLowerCase().trim();

        if (propertyName.equals("content")) {
            if (!java.util.Arrays.equals(content, meb.getContent()))
                throw new ListenerVetoException("media content changed");
            else
                throw new MediaException("handleAboutToChangeMediaEntity was not called before changing content");

        } else if (propertyName.equals("location")) {
            if (location == null ? meb.getLocation() != null : (!location.equals(meb.getLocation())))
                throw new ListenerVetoException("location has changed");
            else
                throw new MediaException("handleAboutToChangeMediaEntity was not called before changing location");

        } else if (propertyName.equals("mimetype")) {
            if (mimeType == null ? meb.getMimeType() != null : (!mimeType.equals(meb.getMimeType())))
                throw new ListenerVetoException("mimetype has changed");
            else
                throw new MediaException("handleAboutToChangeMediaEntity was not called before changing mimeType");

        } else if (propertyName.equals("description")) {
            if (description == null ? meb.getDescription() != null : (!description.equals(meb.getDescription())))
                throw new ListenerVetoException("description has changed");
            else
                throw new MediaException("handleAboutToChangeMediaEntity was not called before changing description");

        } else if (propertyName.equals("name")) {
            if (name == null ? meb.getName() != null : (!name.equals(meb.getName())))
                throw new ListenerVetoException("name has changed");
            else {
                throw new MediaException("handleAboutToChangeMediaEntity was not called before changing proxy");
            }
        } else {
            throw new MediaException(propertyName + ": property not found");
        }
    }

    public boolean equals(Object obj) {
        if (obj instanceof GenericMediaListener) {
            GenericMediaListener gml = (GenericMediaListener) obj;
            if (listenerName == null ? gml.listenerName == null : listenerName.equals(gml.listenerName)) {
                // if (proxy == null? gml.proxy == null:
                // proxy.equals(gml.proxy)){
                if (name == null ? gml.name == null : name.equals(gml.name)) {
                    if (content == null ? gml.content == null : Arrays.equals(content, gml.content)) {
                        if (location == null ? gml.location == null : location.equals(gml.location)) {
                            if (mimeType == null ? gml.mimeType == null : mimeType.equals(gml.mimeType)) {
                                if (description == null ? gml.description == null : description.equals(gml.description)) {
                                    return true;
                                }
                            }
                        }
                    }
                }
                // }
            }
        }

        return false;
    }

    public int hashCode() {
        int hash = 1;
        hash = hash * 31 + ((listenerName == null) ? 0 : listenerName.hashCode());
        hash = hash * 31 + ((description == null) ? 0 : description.hashCode());
        hash = hash * 31 + ((mimeType == null) ? 0 : mimeType.hashCode());
        hash = hash * 31 + ((name == null) ? 0 : name.hashCode());
        hash = hash * 31 + ((location == null) ? 0 : location.hashCode());
        int bytehash = 0;
        if (content != null) {
            for (int i = 0; i < content.length; i++) {
                bytehash += new Byte(content[i]).hashCode();
            }
        }
        hash = hash * 31 + bytehash;
        return hash;
    }

    public void handleAboutToRemoveMediaEntity(MediaEntityLocal mediaEntity) throws MediaException {
        if (mediaEntity == null) throw new NullPointerException("media entity is null");
    }

}
