/*
 * ContactsBackend.java
 *
 * Copyright (C) 2012 Tim Marston <tim@ed.am>
 *
 * This file is part of the Import Contacts program (hereafter referred
 * to as "this program"). For more information, see
 * http://ed.am/dev/android/import-contacts
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package am.ed.importcontacts;

import java.util.HashSet;

import am.ed.importcontacts.ContactsCache.CacheIdentifier;
import am.ed.importcontacts.Importer.ContactData;
import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;


public class ContactsBackend implements Backend
{
	private Activity _activity = null;

	ContactsBackend( Activity activity )
	{
		_activity = activity;
	}

	@Override
	public void populateCache( ContactsCache cache )
	{
		Cursor cur;

		// set of contact ids that we have not yet added
		HashSet< Long > unadded = new HashSet< Long >();

		// get all contacts
		cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
			new String[] {
				Contacts.People._ID,
				Contacts.People.NAME,
			}, null, null, null );
		while( cur.moveToNext() ) {
			Long id = cur.getLong(
				cur.getColumnIndex( Contacts.People._ID ) );
			String name =
				ContactsCache.normaliseName( cur.getString(
					cur.getColumnIndex( Contacts.People.NAME ) ) );
			if( name != null )
			{
				// if we can, add a lookup for the contact id by name
				if( name.length() > 0 ) {
					cache.addLookup( new CacheIdentifier(
						CacheIdentifier.Type.NAME, name ), id );
					continue;
				}
			}

			// record that a lookup for this contact's id still needs to be
			// added by some other means
			unadded.add( id );
		}

		// get contact organisations, primary ones first
		cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
			new String[] {
				Contacts.Phones.PERSON_ID,
				Contacts.Organizations.COMPANY,
			}, null, null, Contacts.Organizations.ISPRIMARY + " DESC" );
		while( cur.moveToNext() ) {
			Long id = cur.getLong( cur.getColumnIndex(
				Contacts.Organizations.PERSON_ID ) );
			String organisation =
				ContactsCache.normaliseOrganisation( cur.getString(
					cur.getColumnIndex( Contacts.Organizations.COMPANY ) ) );
			if( organisation != null )
			{
				// if this is an organisation name for a contact for whom we
				// have not added a lookup, add a lookup for the contact id
				// by organisation
				if( unadded.contains( id ) ) {
					cache.addLookup( new CacheIdentifier(
						CacheIdentifier.Type.ORGANISATION, organisation ), id );
					unadded.remove( id );
				}

				// add associated data
				cache.addAssociatedOrganisation( id, organisation );
			}
		}

		// get all phone numbers, primary ones first
		cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
			new String[] {
				Contacts.Phones.PERSON_ID,
				Contacts.Phones.NUMBER,
			}, null, null, Contacts.Phones.ISPRIMARY + " DESC" );
		while( cur.moveToNext() ) {
			Long id = cur.getLong(
				cur.getColumnIndex( Contacts.Phones.PERSON_ID ) );
			String number =
				ContactsCache.normalisePhoneNumber( cur.getString(
					cur.getColumnIndex( Contacts.Phones.NUMBER ) ) );
			if( number != null )
			{
				// if this is a number for a contact for whom we have not
				// added a lookup, add a lookup for the contact id by phone
				// number
				if( unadded.contains( id ) ) {
					cache.addLookup( new CacheIdentifier(
						CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
					unadded.remove( id );
				}

				// add associated data
				cache.addAssociatedNumber( id, number );
			}
		}

		// now get all email addresses, primary ones first, and postal addresses
		cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
			new String[] {
				Contacts.ContactMethods.PERSON_ID,
				Contacts.ContactMethods.DATA,
				Contacts.ContactMethods.KIND,
			}, Contacts.ContactMethods.KIND + " IN( ?, ? )", new String[] {
				"" + Contacts.KIND_EMAIL,
				"" + Contacts.KIND_POSTAL,
			}, Contacts.ContactMethods.ISPRIMARY + " DESC" );
		while( cur.moveToNext() ) {
			Long id = cur.getLong(
				cur.getColumnIndex( Contacts.ContactMethods.PERSON_ID ) );
			int kind = cur.getInt(
				cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
			if( kind == Contacts.KIND_EMAIL )
			{
				String email =
					ContactsCache.normaliseEmailAddress( cur.getString(
						cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
				if( email != null )
				{
					// if this is an email address for a contact for whom we
					// have not added a lookup, add a lookup for the contact
					// id by email address
					if( unadded.contains( id ) ) {
						cache.addLookup( new CacheIdentifier(
							CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
						unadded.remove( id );
					}

					// add associated data
					cache.addAssociatedEmail( id, email );
				}
			}
			else if( kind == Contacts.KIND_POSTAL )
			{
				String address =
					ContactsCache.normaliseAddress( cur.getString(
						cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
				if( address != null )
				{
					// add associated data
					cache.addAssociatedAddress( id, address );
				}
			}
		}
	}

	@Override
	public void deleteContact( Long id )
	{
		Uri contact_uri =
			ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id );
		_activity.getContentResolver().delete( contact_uri, null, null );
	}

	@Override
	public Long addContact( String name )
	{
		ContentValues values = new ContentValues();
		values.put( Contacts.People.NAME, name );
		Uri contact_uri = _activity.getContentResolver().insert(
			Contacts.People.CONTENT_URI, values );
		Long id = ContentUris.parseId( contact_uri );

		// try to add them to the "My Contacts" group
		if( id != null && id > 0 ) {
			try {
				Contacts.People.addToMyContactsGroup(
					_activity.getContentResolver(), id );
			}
			catch( IllegalStateException e ) {
				// ignore any failure
			}
		}

		return id == null? 0 : id;
	}

	@Override
	public void addContactPhone( Long id, String number,
		ContactData.PreferredDetail data )
	{
		Uri contact_phones_uri = Uri.withAppendedPath(
			ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
			Contacts.People.Phones.CONTENT_DIRECTORY );

		ContentValues values = new ContentValues();
		values.put( Contacts.Phones.TYPE, data.getType() );
		values.put( Contacts.Phones.NUMBER, number );
		if( data.isPreferred() )
			values.put( Contacts.Phones.ISPRIMARY, 1 );

		_activity.getContentResolver().insert( contact_phones_uri, values );
	}

	@Override
	public void addContactEmail( Long id, String email,
		ContactData.PreferredDetail data )
	{
		Uri contact_contact_methods_uri = Uri.withAppendedPath(
			ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
			Contacts.People.ContactMethods.CONTENT_DIRECTORY );

		ContentValues values = new ContentValues();
		values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
		values.put( Contacts.ContactMethods.DATA, email );
		values.put( Contacts.ContactMethods.TYPE, data.getType() );
		if( data.isPreferred() )
			values.put( Contacts.ContactMethods.ISPRIMARY, 1 );

		_activity.getContentResolver().insert( contact_contact_methods_uri,
			values );
	}

	@Override
	public void addContactAddresses( Long id, String address,
		ContactData.TypeDetail data )
	{
		Uri contact_contact_methods_uri = Uri.withAppendedPath(
			ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
			Contacts.People.ContactMethods.CONTENT_DIRECTORY );

		ContentValues values = new ContentValues();
		values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
		values.put( Contacts.ContactMethods.DATA, address );
		values.put( Contacts.ContactMethods.TYPE, data.getType() );

		_activity.getContentResolver().insert( contact_contact_methods_uri,
			values );
	}

	@Override
	public void addContactOrganisation( Long id, String organisation,
		ContactData.ExtraDetail data )
	{
		ContentValues values = new ContentValues();
		values.put( Contacts.Organizations.PERSON_ID, id );
		values.put( Contacts.Organizations.COMPANY, organisation );
		values.put( Contacts.ContactMethods.TYPE,
			Contacts.OrganizationColumns.TYPE_WORK );
		if( data.getExtra() != null )
			values.put( Contacts.Organizations.TITLE, data.getExtra() );

		_activity.getContentResolver().insert(
			Contacts.Organizations.CONTENT_URI, values );
	}


}
