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

  <var name="emptyMail" class="org.unitedfront2.domain.communication.Mail"/>
  <var name="to" class="org.unitedfront2.domain.SimpleUser"/>

  <input name="mail" type="org.unitedfront2.domain.communication.Mail"/>
  <input name="unmodifiableTo" type="org.unitedfront2.domain.SimpleUser"/>
  <input name="pageTitleCode" type="string"/>
  <input name="pageDescriptionCode" type="string"/>
  <input name="formSubmitCode" type="string"/>
  <input name="ignoreEmailFailure" type="boolean"/>
  <input name="statusCode" type="string" required="true"/>

  <decision-state id="initMailOrNot">
    <if test="mail == null" then="initMail" else="initTo"/>
  </decision-state>

  <action-state id="initMail">
    <set name="flowScope.mail" value="emptyMail"/>
    <transition on="success" to="initTo"/>
  </action-state>

  <decision-state id="initTo">
    <if test="unmodifiableTo != null" then="setUnmodifiableTo"/>
    <if test="requestParameters.code != null" then="setToByCode" else="setTo"/>
  </decision-state>

  <action-state id="setUnmodifiableTo">
    <set name="mail.to" value="unmodifiableTo"/>
    <transition on="success" to="createForm"/>
  </action-state>

  <action-state id="setToByCode">
    <set name="mail.to" value="simpleUserTable.findByCode(requestParameters.code)"/>
    <transition on="success" to="createForm"/>
  </action-state>

  <action-state id="setTo">
    <set name="mail.to" value="to"/>
    <transition on="success" to="createForm"/>
  </action-state>

  <view-state id="createForm" view="mail.CreateForm" model="mail">
    <binder>
      <binding property="subject"/>
      <binding property="body"/>
    </binder>
    <on-render>
      <set name="externalContext.nativeResponse.status" value="statusCode"/>
      <set name="flowScope.defaultPageTitleCode" value="'mail.CreateForm'"/>
      <set name="flowScope.defaultFormSubmitCode" value="'mail.CreateForm.submit'"/>
      <evaluate expression="subflowSupportAction.setupForm"/>
      <set name="flashScope.validator" value="mailValidator"/>
    </on-render>
    <transition on="submit" to="fromIsMeOrNot">
      <evaluate expression="mailCreateAction.validateTo"/>
    </transition>
  </view-state>

  <decision-state id="fromIsMeOrNot">
    <!-- アクターがユーザ自身なら送信元に設定 -->
    <if test="externalContext.requestMap._user != null" then="setFrom" else="validateTo"/>
  </decision-state>

  <action-state id="setFrom">
    <set name="mail.from" value="externalContext.requestMap._user"/>
    <transition to="validateTo"/>
  </action-state>

  <decision-state id="validateTo">
    <if test="unmodifiableTo != null &amp;&amp; !mail.toId.equals(unmodifiableTo.id)" then="error" else="send"/>
  </decision-state>

  <end-state id="error" view="externalRedirect:contextRelative:/error/badRequest.html"/>

  <action-state id="send">
    <evaluate expression="domainFactory.prototype(mail).send()" result="mail"/>
    <transition on="success" to="emailSendOrNot"/>
  </action-state>

  <decision-state id="emailSendOrNot">
    <if test="externalContext.requestMap._usingEmail &amp;&amp; mailTable.isForwarding(mail.getToId())" then="emailSend" else="finish"/>
  </decision-state>

  <action-state id="emailSend">
    <on-entry>
      <set name="mail.toMailAddr" value="accountTable.find(mail.toId).mailAddr"/>
    </on-entry>
    <evaluate expression="mailService.send('mail.Mail-mail', flowRequestContext.flowScope.asMap(), externalContext.requestMap._locale)"/>
    <transition on-exception="org.unitedfront2.web.mail.MailSendException" to="emailSendFailureIgnoreOrNot"/>
    <transition on="success" to="finish"/>
  </action-state>

  <decision-state id="emailSendFailureIgnoreOrNot">
    <if test="!ignoreEmailFailure" then="externalRedirect:contextRelative:/error/error.html" else="finish"/>
  </decision-state>

  <end-state id="finish">
    <output name="mail" type="org.unitedfront2.domain.communication.Mail"/>
  </end-state>

</flow>
