package com.linkedin.thirdeye.datalayer.bao.jdbc;

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

import com.linkedin.thirdeye.datalayer.bao.EmailConfigurationManager;
import com.linkedin.thirdeye.datalayer.dto.AnomalyFunctionDTO;
import com.linkedin.thirdeye.datalayer.dto.EmailConfigurationDTO;
import com.linkedin.thirdeye.datalayer.pojo.AnomalyFunctionBean;
import com.linkedin.thirdeye.datalayer.pojo.EmailConfigurationBean;
import com.linkedin.thirdeye.datalayer.util.Predicate;

public class EmailConfigurationManagerImpl extends AbstractManagerImpl<EmailConfigurationDTO>
    implements EmailConfigurationManager {
  private static final String FIND_BY_FUNCTION_ID =
      "select ec from EmailConfigurationDTO ec JOIN ec.functions fn where fn.id=:id";

  public EmailConfigurationManagerImpl() {
    super(EmailConfigurationDTO.class, EmailConfigurationBean.class);
  }

  @Override public Long save(EmailConfigurationDTO emailConfigurationDTO) {
    if (emailConfigurationDTO.getId() != null) {
      //TODO: throw exception and force the caller to call update instead
      update(emailConfigurationDTO);
      return emailConfigurationDTO.getId();
    }
    EmailConfigurationBean bean =
        (EmailConfigurationBean) convertEmailConfigurationDTO2Bean(emailConfigurationDTO);
    Long id = genericPojoDao.put(bean);
    emailConfigurationDTO.setId(id);
    return id;
  }

  private EmailConfigurationBean convertEmailConfigurationDTO2Bean(
      EmailConfigurationDTO emailConfigurationDTO) {
    EmailConfigurationBean emailConfigurationBean =
        convertDTO2Bean(emailConfigurationDTO, EmailConfigurationBean.class);
    List<Long> functionIds = new ArrayList<>();
    for (AnomalyFunctionDTO function : emailConfigurationDTO.getFunctions()) {
      functionIds.add(function.getId());
    }
    emailConfigurationBean.setFunctionIds(functionIds);
    return emailConfigurationBean;
  }

  @Override public void update(EmailConfigurationDTO emailConfigurationDTO) {
    EmailConfigurationBean emailConfigurationBean =
        (EmailConfigurationBean) convertEmailConfigurationDTO2Bean(emailConfigurationDTO);
    genericPojoDao.update(emailConfigurationBean);
  }

  @Override public EmailConfigurationDTO findById(Long id) {
    EmailConfigurationBean emailConfigurationBean =
        genericPojoDao.get(id, EmailConfigurationBean.class);
    if (emailConfigurationBean != null) {
      EmailConfigurationDTO emailConfigurationDTO =
          convertEmailConfigurationBean2DTO(emailConfigurationBean);
      return emailConfigurationDTO;
    }
    return null;
  }

  private EmailConfigurationDTO convertEmailConfigurationBean2DTO(
      EmailConfigurationBean emailConfigurationBean) {
    EmailConfigurationDTO emailConfigurationDTO =
        convertBean2DTO(emailConfigurationBean, EmailConfigurationDTO.class);
    List<Long> functionIds = emailConfigurationBean.getFunctionIds();
    if (functionIds != null && !functionIds.isEmpty()) {
      List<AnomalyFunctionBean> list = genericPojoDao.get(functionIds, AnomalyFunctionBean.class);
      List<AnomalyFunctionDTO> functions = new ArrayList<>();
      for (AnomalyFunctionBean bean : list) {
        AnomalyFunctionDTO dto = convertBean2DTO(bean, AnomalyFunctionDTO.class);
        functions.add(dto);
      }
      emailConfigurationDTO.setFunctions(functions);
    }
    return emailConfigurationDTO;
  }

  @Override public List<EmailConfigurationDTO> findByFunctionId(Long functionId) {
    //    return getEntityManager().createQuery(FIND_BY_FUNCTION_ID, entityClass)
    //        .setParameter("id", id)
    //        .getResultList();
    List<EmailConfigurationBean> list = genericPojoDao.getAll(EmailConfigurationBean.class);
    List<EmailConfigurationDTO> result = new ArrayList<>();
    for (EmailConfigurationBean bean : list) {
      if (bean.getFunctionIds() != null && bean.getFunctionIds().contains(functionId)) {
        EmailConfigurationDTO dto = convertEmailConfigurationBean2DTO(bean);
        result.add(dto);
      }
    }
    return result;
  }

  @Override
  public List<EmailConfigurationDTO> findByCollectionMetric(String collection,
      String metric) {
    Predicate predicate =
        Predicate.AND(Predicate.EQ("collection", collection), Predicate.EQ("metric", metric));
    return genericPojoDao.get(predicate, EmailConfigurationDTO.class);
  }

  @Override
  public List<EmailConfigurationDTO> findByCollection(String collection) {
    Predicate predicate = Predicate.EQ("collection", collection);
    return genericPojoDao.get(predicate, EmailConfigurationDTO.class);
  }
}

@@ -881,32 +881,34 @@
   %7D%0A%0A  @Override
+%0A 
  public Long sav
@@ -1912,32 +1912,34 @@
   %7D%0A%0A  @Override
+%0A 
  public void upd
@@ -2191,32 +2191,34 @@
   %7D%0A%0A  @Override
+%0A 
  public EmailCon
@@ -3403,16 +3403,18 @@
 Override
+%0A 
  public 
@@ -4148,22 +4148,16 @@
 lection,
-%0A     
  String 
@@ -4287,30 +4287,59 @@
 tric));%0A    
-return
+List%3CEmailConfigurationBean%3E list =
  genericPojo
@@ -4371,34 +4371,265 @@
 onfiguration
-DTO.class)
+Bean.class);%0A    List%3CEmailConfigurationDTO%3E result = new ArrayList%3C%3E();%0A    for (EmailConfigurationBean bean : list) %7B%0A      EmailConfigurationDTO dto = convertEmailConfigurationBean2DTO(bean);%0A      result.add(dto);%0A    %7D%0A    return result
 ;%0A  %7D%0A%0A  @Ov
@@ -4780,22 +4780,51 @@
 n);%0A    
-return
+List%3CEmailConfigurationBean%3E list =
  generic
@@ -4860,30 +4860,261 @@
 onfiguration
-DTO.class)
+Bean.class);%0A    List%3CEmailConfigurationDTO%3E result = new ArrayList%3C%3E();%0A    for (EmailConfigurationBean bean : list) %7B%0A      EmailConfigurationDTO dto = convertEmailConfigurationBean2DTO(bean);%0A      result.add(dto);%0A    %7D%0A    return result
 ;%0A  %7D%0A%7D%0A

Fix for bug in email manager for jdbc impl (#578)