package com.tandbergtv.metadatamanager.model;

public class CustomField {
	String xpath;
	String name;
	String value;
	public static final String CUSTOMFIELD = "tns:CustomField";
	public static final String ATTRIBUTE_NAME = "@name";
	public static final String ATTRIBUTE_VALUE = "@value";
	public static final String TNS_CUSTOMFIELDS = "/tns:CustomFields";
	public static final String XPATH_DELIMITER = "/";
	public static final String CUSTOM_FIELD_XPATH_DISTINGUISHER = "CustomField{";
	
	public static final String NAME = "name";

	public CustomField(String xpath, String name, String value) {
		super();
		this.xpath = xpath;
		this.name = name;
		this.value = value;
	}

	public CustomField(String xpath) {
		this.xpath = xpath;
	}

	public String getXpath() {
		return xpath;
	}

	public void setXpath(String xpath) {
		this.xpath = xpath;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String constructTTVString() {
		// For xpath like tns:Fields{1}/tns:CustomFields{1}/tns:CustomField{1}
		// xpathBeforeLastIndex =
		// tns:Fields{1}/tns:CustomFields{1}/tns:CustomField
		// xpathLastIndex = {1}
		int lastIndexPosition = this.xpath.lastIndexOf("{");
		String xpathBeforeLastIndex = this.xpath.substring(0, lastIndexPosition);
		String xpathLastIndex = this.xpath.substring(lastIndexPosition);

		// final format is sth like:
		// /tns:Fields{1}/tns:CustomFields{1}/tns:CustomField[@name=%s]{1}/@value==%s
		StringBuffer format = new StringBuffer();
		format.append(xpathBeforeLastIndex).append("[" + ATTRIBUTE_NAME + "=%s]").append(xpathLastIndex).append(
				XPATH_DELIMITER).append(ATTRIBUTE_VALUE + "==%s");

		return String.format(format.toString(), this.getName(), this.getValue());
	}

	@Override
	public String toString() {
		return super.toString() + "(" + xpath + "," + name + "," + value + ")";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		result = prime * result + ((xpath == null) ? 0 : xpath.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final CustomField other = (CustomField) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		if (xpath == null) {
			if (other.xpath != null)
				return false;
		} else if (!xpath.equals(other.xpath))
			return false;
		return true;
	}

}
