/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2016 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.apps.base.service;

import static com.axelor.common.StringUtils.isBlank;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Singleton;
import javax.mail.internet.InternetAddress;

import com.axelor.apps.base.db.Partner;
import com.axelor.apps.base.db.repo.PartnerRepository;
import com.axelor.apps.message.service.MailServiceMessageImpl;
import com.axelor.auth.db.User;
import com.axelor.auth.db.repo.UserRepository;
import com.axelor.db.Model;
import com.axelor.db.Query;
import com.axelor.exception.service.TraceBackService;
import com.axelor.inject.Beans;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;

@Singleton
public class MailServiceBaseImpl extends MailServiceMessageImpl{
	
	@Override
	public Model resolve(String email) {
		final UserRepository users = Beans.get(UserRepository.class);
		final User user = users.all().filter("self.partner.emailAddress.address = ?1", email).fetchOne();
		if (user != null) {
			return user;
		}
		final PartnerRepository partners = Beans.get(PartnerRepository.class);
		final Partner partner = partners.all().filter("self.emailAddress.address = ?1", email).fetchOne();
		if (partner != null) {
			return partner;
		}
		return super.resolve(email);
	}

	
	@Override
	public List<InternetAddress> findEmails(String matching, List<String> selected, int maxResult) {
		
		//Users
		List<String> selectedWithoutNull = new ArrayList<String>(selected);
		for (int i = 0; i < selected.size() ; i++) {
			if(Strings.isNullOrEmpty(selected.get(i))) selectedWithoutNull.remove(i);
		}
		
		final List<String> where = new ArrayList<>();
		final Map<String, Object> params = new HashMap<>();
		
		where.add("self.partner is not null AND (self.partner.emailAddress is not null OR self.email is not null)");

		if (!isBlank(matching)) {
			where.add("(LOWER(self.partner.emailAddress.address) like LOWER(:email) OR LOWER(self.partner.fullName) like LOWER(:email))");
			params.put("email", "%" + matching + "%");
		}
		if (selectedWithoutNull != null && !selectedWithoutNull.isEmpty()) {
			where.add("self.partner.emailAddress.address not in (:selected)");
			params.put("selected", selectedWithoutNull);
		}

		final String filter = Joiner.on(" AND ").join(where);
		final Query<User> query = Query.of(User.class);

		if (!isBlank(filter)) {
			query.filter(filter);
			query.bind(params);
		}

		final List<InternetAddress> addresses = new ArrayList<>();
		for (User user : query.fetch(maxResult)) {
			try {
				if(user.getPartner().getEmailAddress() != null && !Strings.isNullOrEmpty(user.getPartner().getEmailAddress().getAddress())){
					final InternetAddress item = new InternetAddress(user.getPartner().getEmailAddress().getAddress(), user.getFullName());
					addresses.add(item);
					selectedWithoutNull.add(user.getPartner().getEmailAddress().getAddress());
				}
				else if(!Strings.isNullOrEmpty(user.getEmail())){
					final InternetAddress item = new InternetAddress(user.getEmail(), user.getFullName());
					addresses.add(item);
					selectedWithoutNull.add(user.getEmail());
				}
				
			} catch (UnsupportedEncodingException e) {
				TraceBackService.trace(e);
			}
		}
		
		//Partners
		
		final List<String> where2 = new ArrayList<>();
		final Map<String, Object> params2 = new HashMap<>();
		
		where2.add("self.emailAddress is not null");

		if (!isBlank(matching)) {
			where2.add("(LOWER(self.emailAddress.address) like LOWER(:email) OR LOWER(self.fullName) like LOWER(:email))");
			params2.put("email", "%" + matching + "%");
		}
		if (selectedWithoutNull != null && !selectedWithoutNull.isEmpty()) {
			where2.add("self.emailAddress.address not in (:selected)");
			params2.put("selected", selectedWithoutNull);
		}

		final String filter2 = Joiner.on(" AND ").join(where2);
		final Query<Partner> query2 = Query.of(Partner.class);

		if (!isBlank(filter2)) {
			query2.filter(filter2);
			query2.bind(params2);
		}

		for (Partner partner : query2.fetch(maxResult)) {
			try {
				if(partner.getEmailAddress() != null && !Strings.isNullOrEmpty(partner.getEmailAddress().getAddress())){
					final InternetAddress item = new InternetAddress(partner.getEmailAddress().getAddress(), partner.getFullName());
					addresses.add(item);
				}
			} catch (UnsupportedEncodingException e) {
			}
		}

		return addresses;
	}

}

@@ -2489,32 +2489,34 @@
 %09%09%0A%09%09where.add(%22
+((
 self.partner is 
@@ -2528,17 +2528,16 @@
 ull AND 
-(
 self.par
@@ -2565,20 +2565,22 @@
 not null
+)
  OR 
+(
 self.ema
@@ -2594,16 +2594,17 @@
 ot null)
+)
 %22);%0A%0A%09%09i
@@ -2726,32 +2726,111 @@
 artner.fullName)
+ like LOWER(:email) OR LOWER(self.email) like LOWER(:email) OR LOWER(self.name)
  like LOWER(:ema
@@ -5186,16 +5186,47 @@
 on e) %7B%0A
+%09%09%09%09TraceBackService.trace(e);%0A
 %09%09%09%7D%0A%09%09%7D

Define a filter for the followers