package implemB;

import java.lang.Comparable;

import implemB.NumeroTelephone.phoneType;

import java.util.List;
import java.util.LinkedList;


public class Contact implements Comparable<Contact> {
	private String lastName;
	private String firstName;
	
	private List<NumeroTelephone> numeros;
	
	public Contact(String firstName, String lastName)
	{
		this.setFirstName(firstName);
		this.setLastName(lastName);

		this.numeros = new LinkedList<NumeroTelephone>();
	}

	//:://////////////////////////////////////////////////////////////////////
	//::// Find, and delete numbers
	//:://////////////////////////////////////////////////////////////////////	
	
	public void addNumber(String num, phoneType type)
	{
		this.numeros.add(new NumeroTelephone(num, type));
	}
	
	public void addNumber(String num)
	{
		this.numeros.add(new NumeroTelephone(num));
	}
	
	public NumeroTelephone findNumber(String num)
	{
		NumeroTelephone phone;
		phone = null;
		
		for(NumeroTelephone p : this.getNumbers())
			if(num.equals(p.getNum()))
				phone = p;
		
		return phone;
	}
	
	public boolean delNumber(String num)
	{
		NumeroTelephone phone;
		phone = findNumber(num);
		
		if(phone != null)
			this.numeros.remove(phone);
			
		return phone != null;
	}
	
	//:://////////////////////////////////////////////////////////////////////
	//::// Overloaded
	//:://////////////////////////////////////////////////////////////////////
		
	public int compareTo(Contact with)
	{
		int ret;
		
		ret = this.getLastName().compareTo(with.getLastName());
		
		if(ret == 0)
			ret = this.getFirstName().compareTo(with.getFirstName());
		
		return ret;
	}
	
	public boolean equals(Object o)
	{
		Contact with;
		
		if(!(o instanceof Contact))
			return false;
		
		with = (Contact)o;
		
		return this.compareTo(with) == 0;
	}
	
	public String toString()
	{
		return this.getLastName().toUpperCase() + " " + this.getFirstName();
	}
	
	// DJBX33A
	public int hashCode()
	{
		int    hash;
		String str;
		
		hash = 5381;
		str  = this.toString();
		
		for(int i = 0; i < str.length(); i++)
			hash = hash * 33 + str.charAt(i);
		
		return hash;
	}
	
	//:://////////////////////////////////////////////////////////////////////
	//::// Getter, and setters
	//:://////////////////////////////////////////////////////////////////////

	public List<NumeroTelephone> getNumbers() {
		return this.numeros;
	}
	
	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
}
