<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd

		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:util="http://www.springframework.org/schema/util">

    <!-- if we add task executor in publish-subscribe-channel than all mail processing will be done async,
    that would be needed if we are dealing with lot of incoming mails-->
    <int:publish-subscribe-channel id="mailChannelCaseFile"/>

    <bean id="acmEmailReceiverAdapter" class="com.armedia.acm.email.model.AcmEmailReceiverAdapter">
        <property name="emailReceiverConfig" ref="emailReceiverConfig"/>
        <property name="javaMailPropertiesCaseFile" ref="javaMailPropertiesCaseFile"/>
        <property name="javaMailPropertiesComplaint" ref="javaMailPropertiesComplaint"/>
    </bean>

    <bean id="caseImapMailReceiver" class="org.springframework.integration.mail.ImapMailReceiver" scope="refresh">
        <constructor-arg index="0"
                         value="imaps://#{emailReceiverConfig.caseFileUser}:#{emailReceiverConfig.caseFilePassword}@#{emailReceiverConfig.host}:#{emailReceiverConfig.port}/#{emailReceiverConfig.fetchFolder}"/>
        <aop:scoped-proxy/>
    </bean>

    <bean id="loggingHandlerCaseFile" class="org.springframework.integration.handler.LoggingHandler">
        <constructor-arg value="ERROR"/>
    </bean>

    <int:chain input-channel="mailErrorChannelCaseFile">
        <int:service-activator ref="loggingHandlerCaseFile" method="handleMessage"/>
    </int:chain>

    <util:properties id="javaMailPropertiesCaseFile">
        <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.imap.socketFactory.fallback">false</prop>
        <prop key="mail.store.protocol">#{emailReceiverConfig.protocol}</prop>
        <prop key="mail.debug">#{emailReceiverConfig.debug}</prop>
    </util:properties>

    <!-- Complaint mail channel -->
    <int:publish-subscribe-channel id="mailChannelComplaint"/>

    <bean id="complaintImapMailReceiver" class="org.springframework.integration.mail.ImapMailReceiver" scope="refresh">
        <constructor-arg index="0"
                         value="imaps://#{emailReceiverConfig.complaintUser}:#{emailReceiverConfig.complaintPassword}@#{emailReceiverConfig.host}:#{emailReceiverConfig.port}/#{emailReceiverConfig.fetchFolder}"/>
        <aop:scoped-proxy/>
    </bean>

    <bean id="loggingHandlerComplaint" class="org.springframework.integration.handler.LoggingHandler">
        <constructor-arg value="ERROR"/>
    </bean>

    <int:chain input-channel="mailErrorChannelComplaint">
        <int:service-activator ref="loggingHandlerCaseFile" method="handleMessage"/>
    </int:chain>

    <util:properties id="javaMailPropertiesComplaint">
        <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.imap.socketFactory.fallback">false</prop>
        <prop key="mail.store.protocol">#{emailReceiverConfig.protocol}</prop>
        <prop key="mail.debug">#{emailReceiverConfig.debug}</prop>
    </util:properties>

    <!-- Sender-Receiver mail channel -->
    <int:publish-subscribe-channel id="mailChannelReceiver"/>

    <bean id="senderImapMailReceiver" class="org.springframework.integration.mail.ImapMailReceiver" scope="refresh">
        <constructor-arg index="0"
                         value="imaps://#{emailSenderConfig.getUsernameEncoded}:#{emailSenderConfig.password}@#{emailReceiverConfig.host}:#{emailReceiverConfig.port}/#{emailReceiverConfig.fetchFolder}"/>
        <aop:scoped-proxy/>
    </bean>

    <bean id="loggingHandlerSender" class="org.springframework.integration.handler.LoggingHandler">
        <constructor-arg value="ERROR"/>
    </bean>

    <int:chain input-channel="mailErrorChannelReceiver">
        <int:service-activator ref="loggingHandlerSender" method="handleMessage"/>
    </int:chain>
    <util:properties id="javaMailPropertiesReceiver">
        <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.imap.socketFactory.fallback">false</prop>
        <prop key="mail.store.protocol">#{emailReceiverConfig.protocol}</prop>
        <prop key="mail.debug">#{emailReceiverConfig.debug}</prop>
    </util:properties>

    <bean id="emailReceiverConfig" class="com.armedia.acm.email.model.EmailReceiverConfig" scope="refresh">
        <aop:scoped-proxy/>
    </bean>

    <bean id="emailSenderConfig" class="com.armedia.acm.email.model.EmailSenderConfig" scope="refresh">
        <aop:scoped-proxy/>
    </bean>
</beans>
