package com.tandbergtv.watchpoint.pmm.entities;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * @author spuranik
 * 
 */
public class Partner implements Serializable, IContainer {

	private static final long serialVersionUID = 3543030394003557805L;

	// fields that can be used as parameter references
	private static String PARTNER_NAME = "Name";
	private static String PARTNER_ID = "Id";
	private static String PARTNER_TYPE = "Type";
	private static String PARTNER_PROVIDERID = "ProviderId";
	private static String PARTNER_CONTEXT_ID = "ContextId";
	private static String PARTNER_EMAIL_ID = "EmailId";
	
	private long id;
	private String name;
	private PartnerType type;
	private String providerId;
	private Context context;
	private Set<Service> services;
	private PartnerContact contact;
	private PitchFrequency defaultPitchFrequency;
	private String notes;
	private Boolean isActive;
	private String lookupKey;

	public Partner() { 
		context = new PartnerContext();
		((PartnerContext)context).setContainer(this);
	}
	
	public Partner(long partnerId) {
		this.id = partnerId;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setType(PartnerType type) {
		this.type = type;
	}

	public PartnerType getType() {
		return type;
	}

	public void setProviderId(String providerId) {
		this.providerId = providerId;
	}

	public String getProviderId() {
		return providerId;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public Context getContext() {
		return context;
	}

	public void setServices(Set<Service> services) {
		this.services = services;
	}

	public Set<Service> getServices() {
		return services;
	}

	public String getContainerName() {
		return this.getName();
	}

	public long getContainerContextId() {
		return this.getContext().getId();
	}

	public long getContainerId() {
		return this.id;
	}
	
	public String getContainerLookupKey() {
		return this.lookupKey;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tandbergtv.watchpoint.pmm.entities.IContainer#getContainerType()
	 */
	public ContainerType getContainerType() {
		return ContainerType.PARTNER;
	}

	/**
	 * @return the defaultPitchFrequency
	 */
	public PitchFrequency getDefaultPitchFrequency() {
		return defaultPitchFrequency;
	}

	/**
	 * @param defaultPitchFrequency
	 *            the defaultPitchFrequency to set
	 */
	public void setDefaultPitchFrequency(PitchFrequency defaultPitchFrequency) {
		this.defaultPitchFrequency = defaultPitchFrequency;
	}

	/**
	 * @return the notes
	 */
	public String getNotes() {
		return notes;
	}

	/**
	 * @param notes
	 *            the notes to set
	 */
	public void setNotes(String notes) {
		this.notes = notes;
	}


	
	/**
	 * @return the contact
	 */
	public PartnerContact getContact() {
		return contact;
	}

	/**
	 * @param contact
	 *            the contact to set
	 */
	public void setContact(PartnerContact contact) {
		this.contact = contact;
	}

	/**
	 * @return the isActive
	 */
	public Boolean getIsActive() {
		return isActive;
	}

	/**
	 * @param isActive the isActive to set
	 */
	public void setIsActive(Boolean isActive) {
		this.isActive = isActive;
	}

	/**
	 * @return the lookupKey
	 */
	public String getLookupKey() {
		return lookupKey;
	}

	/**
	 * @param lookupKey the lookupKey to set
	 */
	public void setLookupKey(String lookupKey) {
		this.lookupKey = lookupKey;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Partner other = (Partner) obj;
		if (id != other.id)
			return false;
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tandbergtv.watchpoint.pmm.entities.IContainer#getValue(java.lang.String)
	 */
	public String getValue(String propertyName) {

		// this method is used for evaluation of parameter references
		if (propertyName.equalsIgnoreCase(PARTNER_CONTEXT_ID)) {
			return String.valueOf(this.context.getId());
		} else if (propertyName.equalsIgnoreCase(PARTNER_EMAIL_ID)) {
			return contact.getEmail();
		} else if (propertyName.equalsIgnoreCase(PARTNER_ID)) {
			return String.valueOf(this.id);
		} else if (propertyName.equalsIgnoreCase(PARTNER_NAME)) {
			return this.name;
		} else if (propertyName.equalsIgnoreCase(PARTNER_PROVIDERID)) {
			return this.providerId;
		} else if (propertyName.equalsIgnoreCase(PARTNER_TYPE)) {
			return this.getType().toString();
		}

		return new String();
	}

	@Override
	public List<ContainerProperty> getProperties() {
		return this.getContext().getProperties();
	}

	@Override
	public void setProperties(List<ContainerProperty> properties) {
		this.getContext().setProperties(properties);
	}

	@Override
	public void addProperty(String name, String value) {
		this.getContext().addProperty(name, value);
	}

	@Override
	public void removeProperty(String name) {
		this.getContext().removeProperty(name);
	}

	@Override
	public void removeAllProperties() {
		this.context.getProperties().clear();
	}
}
