package info.bukova.isspst.data;

import info.bukova.isspst.Constants;
import info.bukova.isspst.StringUtils;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.URL;

@Entity
@Table(name = "ADDRESS")
public class Address extends BaseData
{

	@Column(name = "COMPANY")
	private String company;
	@Column(name = "DEPARTMENT")
	private String department;
	@Column(name = "CONTACT_NAME")
	private String contactName;
	@Column(name = "STREET")
	private String street;
	@Column(name = "HOUSE_NUMBER")
	private String houseNumber;
	@Column(name = "ZIP_CODE")
	private String zipCode;
	@Column(name = "CITY")
	private String city;
	@Column(name = "STATE")
	private String state;
	@Column(name = "IC")
	private long ic;
	@Column(name = "DIC")
	private String dic;
	@Column(name = "PHONE")
	private String phone;
	@Column(name = "EMAIL")
	private String email;
	@Column(name = "WEB")
	private String web;
	@Column(name = "DESCRIPTION", length = Constants.LEN_DESCRIPTION)
	private String description;

	@NotNull(message = "Zadejte firmu")
	@NotEmpty(message = "Zadejte firmu")
	public String getCompany()
	{
		return company;
	}

	public void setCompany(String company)
	{
		this.company = company;
	}

	public String getDepartment()
	{
		return department;
	}

	public void setDepartment(String department)
	{
		this.department = department;
	}

	public String getContactName()
	{
		return contactName;
	}

	public void setContactName(String contactName)
	{
		this.contactName = contactName;
	}

	public String getStreet()
	{
		return street;
	}

	public void setStreet(String street)
	{
		this.street = street;
	}

	public String getHouseNumber()
	{
		return houseNumber;
	}

	public void setHouseNumber(String houseNumber)
	{
		this.houseNumber = houseNumber;
	}

	public String getZipCode()
	{
		return zipCode;
	}

	public void setZipCode(String zipCode)
	{
		this.zipCode = zipCode;
	}

	@NotNull(message = "Zadejte město")
	@NotEmpty(message = "Zadejte město")
	public String getCity()
	{
		return city;
	}

	public void setCity(String city)
	{
		this.city = city;
	}

	public String getState()
	{
		return state;
	}

	public void setState(String state)
	{
		this.state = state;
	}

	public long getIc()
	{
		return ic;
	}

	public void setIc(long ic)
	{
		this.ic = ic;
	}

	public String getDic()
	{
		return dic;
	}

	public void setDic(String dic)
	{
		this.dic = dic;
	}

	public String getPhone()
	{
		return phone;
	}

	public void setPhone(String phone)
	{
		this.phone = phone;
	}

	@Email(message = "Špatný formát adresy")
	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	@URL(message = "Špatný formát adresy")
	public String getWeb()
	{
		return web;
	}

	public void setWeb(String web)
	{
		this.web = web;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public String toString()
	{
		String str, street;

		List<String> listStreet = new ArrayList<String>();

		if (this.street != null)
		{
			listStreet.add(this.street);
		}

		if (this.houseNumber != null)
		{
			listStreet.add(this.houseNumber);
		}

		street = StringUtils.join(listStreet, " ");

		List<String> list = new ArrayList<String>();

		if (this.company != null)
		{
			list.add(this.company);
		}

		if (street != null)
		{
			list.add(street);
		}

		if (this.city != null)
		{
			list.add(this.city);
		}

		list.add(Long.toString(this.ic));
		str = StringUtils.join(list, ", ");

		return str;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Address other = (Address) obj;

		if (other.getId() == this.getId())
		{
			if (other.getId() == 0)
			{
				// Protože globální nastavení je serializováno (není v DB), nemá
				// vyplněno ID - musíme porovnávat jinak - obsahově
				if (city == null)
				{
					if (other.city != null)
						return false;
				}
				else if (!city.equals(other.city))
					return false;
				if (company == null)
				{
					if (other.company != null)
						return false;
				}
				else if (!company.equals(other.company))
					return false;
				if (contactName == null)
				{
					if (other.contactName != null)
						return false;
				}
				else if (!contactName.equals(other.contactName))
					return false;
				if (department == null)
				{
					if (other.department != null)
						return false;
				}
				else if (!department.equals(other.department))
					return false;
				if (description == null)
				{
					if (other.description != null)
						return false;
				}
				else if (!description.equals(other.description))
					return false;
				if (dic == null)
				{
					if (other.dic != null)
						return false;
				}
				else if (!dic.equals(other.dic))
					return false;
				if (email == null)
				{
					if (other.email != null)
						return false;
				}
				else if (!email.equals(other.email))
					return false;
				if (houseNumber == null)
				{
					if (other.houseNumber != null)
						return false;
				}
				else if (!houseNumber.equals(other.houseNumber))
					return false;
				if (ic != other.ic)
					return false;
				if (phone == null)
				{
					if (other.phone != null)
						return false;
				}
				else if (!phone.equals(other.phone))
					return false;
				if (state == null)
				{
					if (other.state != null)
						return false;
				}
				else if (!state.equals(other.state))
					return false;
				if (street == null)
				{
					if (other.street != null)
						return false;
				}
				else if (!street.equals(other.street))
					return false;
				if (web == null)
				{
					if (other.web != null)
						return false;
				}
				else if (!web.equals(other.web))
					return false;
				if (zipCode == null)
				{
					if (other.zipCode != null)
						return false;
				}
				else if (!zipCode.equals(other.zipCode))
					return false;
				return true;
			}
			else
			{
				return true;
			}
		}

		return false;
	}
}
