package wenrgise.workflow.utility;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.ServiceLocator;
import wenrgise.common.utility.UserInfo;
import wenrgise.workflow.core.WflDocumentInfo;
import wenrgise.workflow.core.WflResource;
import wenrgise.workflow.core.impl.WflResourceImpl;
import wenrgise.workflow.ejb.business.WorkFlowCommonBO;
import wenrgise.workflow.ejb.facade.WorkFlowFacade;
import wenrgise.workflow.ejb.facade.WorkFlowFacadeHome;
import wenrgise.workflow.exception.ApproverNotFoundException;
import wenrgise.workflow.exception.MailNotSentException;

public class WorkFlowUtil {
  public static Logger log = Logger.getLogger("wenrgise.workflow.utility.WorkFlowUtil");
  
  public static WflResource findNextApprover(WflDocumentInfo docInfo) throws ApproverNotFoundException {
    WflResourceImpl wflResourceImpl = new WflResourceImpl();
    if (docInfo.getDocumentType().equals("Leave"))
      wflResourceImpl.setEmpNo("90074"); 
    if (docInfo.getDocumentType().equals("Loan"))
      wflResourceImpl.setEmpNo("90038"); 
    return (WflResource)wflResourceImpl;
  }
  
  public static void sendMail(WflResource wflResource, WflDocumentInfo docInfo, String action) throws MailNotSentException {
    try {
      if (null != wflResource) {
        Session session = getMailSession();
        MimeMessage message = new MimeMessage(session);
        String content = String.valueOf(String.valueOf(String.valueOf(String.valueOf("The document ").concat(String.valueOf(docInfo.getDocumentNumber()))).concat(String.valueOf(" of type "))).concat(String.valueOf(docInfo.getDocumentType()))).concat(String.valueOf(" is pending for your approval. Thanks."));
        message.setText(content);
        message.setSubject(String.valueOf(String.valueOf(String.valueOf(String.valueOf("Notification: ").concat(String.valueOf(docInfo.getDocumentType()))).concat(String.valueOf("("))).concat(String.valueOf(docInfo.getDocumentNumber()))).concat(String.valueOf(") pending")));
        message.setFrom((Address)new InternetAddress("chayanika.das@tcs.com"));
        InternetAddress internetAddress = new InternetAddress(getMailAddress(wflResource));
        message.addRecipient(Message.RecipientType.TO, (Address)internetAddress);
        message.saveChanges();
        Transport.send((Message)message);
      } 
    } catch (MessagingException oMsx) {
      log.severe(String.valueOf(String.valueOf(oMsx.getClass().getName()).concat(String.valueOf(" "))).concat(String.valueOf(oMsx.getMessage())));
    } 
  }
  
  public static boolean canApprove(WflDocumentInfo docInfo) {
    return true;
  }
  
  public static boolean canReject(WflDocumentInfo docInfo) {
    return true;
  }
  
  public static void updateWorkList(String sUpsertFlag, WflResource wflResource, WflDocumentInfo docInfo) throws EnrgiseApplicationException, EnrgiseSystemException {
    log.severe(String.valueOf("Upsert Flag ").concat(String.valueOf(sUpsertFlag)));
    log.severe(String.valueOf(" Employee Id ").concat(String.valueOf(wflResource.getEmployeeId())));
    log.severe(String.valueOf(" Document Id ").concat(String.valueOf(docInfo.getDocumentId())));
    log.severe(String.valueOf(" Document Number ").concat(String.valueOf(docInfo.getDocumentNumber())));
    WorkFlowCommonBO oCommonBO = new WorkFlowCommonBO();
    oCommonBO.updateWorkList(sUpsertFlag, wflResource, docInfo);
  }
  
  private static Session getMailSession() {
    Properties props = System.getProperties();
    props.put("mail.smtp.host", "172.17.9.119");
    return Session.getDefaultInstance(props, null);
  }
  
  public static String getMailAddress(WflResource wflResource) {
    WorkFlowCommonBO oCommonBO = new WorkFlowCommonBO();
    if (null != wflResource) {
      log.severe(String.valueOf("Employee Id is ").concat(String.valueOf(wflResource.getEmployeeId())));
      return oCommonBO.getMailAddress(wflResource.getEmployeeId());
    } 
    return oCommonBO.getMailAddress(null);
  }
  
  public static ArrayList getDetailDocs(String sDocumentId, UserInfo oUserInfo) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      WorkFlowFacadeHome oHome = (WorkFlowFacadeHome)ServiceLocator.getLocator().getService("WorkFlowFacade");
      WorkFlowFacade oWorkFlowFacade = oHome.create();
      long recCount = oWorkFlowFacade.getDtlDocTypeMetaInfo(sDocumentId, oUserInfo).getRecordCount();
      if (recCount > 0L)
        return oWorkFlowFacade.getDtlDocTypeInfo(sDocumentId, 1L, recCount, oUserInfo); 
      return null;
    } catch (RemoteException oRe) {
      throw new EnrgiseSystemException(oRe);
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
  }
  
  public static String getRouteDtlId(WflDocumentInfo wflDocInfo) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      WorkFlowFacadeHome oHome = (WorkFlowFacadeHome)ServiceLocator.getLocator().getService("WorkFlowFacade");
      WorkFlowFacade oWorkFlowFacade = oHome.create();
      String routeDtlId = oWorkFlowFacade.getRouteDtlId(wflDocInfo);
      oWorkFlowFacade.remove();
      return routeDtlId;
    } catch (RemoteException oRe) {
      throw new EnrgiseSystemException(oRe);
    } catch (RemoveException oRv) {
      throw new EnrgiseSystemException(oRv);
    } catch (CreateException oCrt) {
      throw new EnrgiseSystemException(oCrt);
    } 
  }
  
  public static String getActivityCode(String activityId) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      log.severe("ActivityStarted");
      WorkFlowFacadeHome oHome = (WorkFlowFacadeHome)ServiceLocator.getLocator().getService("WorkFlowFacade");
      WorkFlowFacade oWorkFlowFacade = oHome.create();
      String activityCode = oWorkFlowFacade.getActivityCode(activityId);
      oWorkFlowFacade.remove();
      return activityCode;
    } catch (RemoteException oRe) {
      log.severe(String.valueOf(String.valueOf(oRe.getClass().getName()).concat(String.valueOf(" "))).concat(String.valueOf(oRe.getMessage())));
      throw new EnrgiseSystemException(oRe);
    } catch (RemoveException oRv) {
      log.severe(String.valueOf(String.valueOf(oRv.getClass().getName()).concat(String.valueOf(" "))).concat(String.valueOf(oRv.getMessage())));
      throw new EnrgiseSystemException(oRv);
    } catch (CreateException oCrt) {
      log.severe(String.valueOf(String.valueOf(oCrt.getClass().getName()).concat(String.valueOf(" "))).concat(String.valueOf(oCrt.getMessage())));
      throw new EnrgiseSystemException(oCrt);
    } 
  }
}
