package jp.sourceforge.mailagent;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.apache.velocity.runtime.resource.loader.FileResourceLoader;

/**
 * MailAgent價儖僟.
 * <pre>
 * MailAgent 傪惗惉偡傞僋儔僗丅
 * 儊乕儖愝掕乮俽俵俿俹憲怣愝掕乯丄僥儞僾儗乕僩撉弌偟愭愝掕傪僐儞僗僩儔僋僞偱巜掕偟偰丄
 * MailAgent惗惉偺搒搙丄偙傟傜愝掕傪偟側偄傛偆偵偡傞丅
 * </pre>
 */
public final class MailAgentBuilder{
   private MailConfig config;
   private TemplateConfig  templateConfig;

   /**
    * 僐儞僗僩儔僋僞乮俽俵俿俹憲怣愝掕偺傒乯.
    * @param config 儊乕儖愝掕乮俽俵俿俹憲怣愝掕乯巜掕偡傞僆僽僕僃僋僩偵梊傔愝掕抣傪擖傟偰偍偔偙偲丅
    */
   public MailAgentBuilder(MailConfig config){
      this.config = config;
   }
   /**
    * 僐儞僗僩儔僋僞.
    * @param config 儊乕儖愝掕乮俽俵俿俹憲怣愝掕乯巜掕偡傞僆僽僕僃僋僩偵梊傔愝掕抣傪擖傟偰偍偔偙偲丅
    * @param templateConfig 僥儞僾儗乕僩撉弌偟愭愝掕丄巜掕偡傞僆僽僕僃僋僩偵梊傔愝掕抣傪擖傟偰偍偔偙偲丅
    */
   public MailAgentBuilder(MailConfig config,TemplateConfig templateConfig){
      this.config = config;
      this.templateConfig = templateConfig;
   }

   /**
    * MailAgent 惗惉
    * <pre>
    * 僥儞僾儗乕僩傪巊梡偟側偄応崌偼丄偙偺堷悢側偟傪巊偆丅
    * </pre>
    * @return MailAgent
    */
   public MailAgent create(){
      return this.create(null);
   }

   /**
    * MailAgent 惗惉乮僥儞僾儗乕僩僼傽僀儖巜掕乯
    * <pre>
    * 僥儞僾儗乕僩撉弌偟愭愝掕 TemplateConfig 偱巜掕偟偨僥儞僾儗乕僩傪扵偡僨傿儗僋僩儕捈壓偵丄
    * 堷悢偱巜掕偡傞僥儞僾儗乕僩僼傽僀儖偑懚嵼偟側偗傟偽側傜側偄丅
    * 仺 TemplateConfig#setTemplateLoadpath 偱 僥儞僾儗乕僩傪扵偡僨傿儗僋僩儕傪巜掕偡傞
    * </pre>
    * @param templateName 僥儞僾儗乕僩僼傽僀儖柤
    * @return MailAgent
    */
   public MailAgent create(String templateName){
      MailAgent mailAgent = new MailAgentImpl();
      mailAgent.setMailConfig(this.config);
      if (templateName==null && this.templateConfig==null){
         return mailAgent;
      }else if(templateName != null && this.templateConfig != null){
         return this.setTemplate(mailAgent,this.templateConfig,templateName);
      }else if(templateName == null && this.templateConfig != null){
         throw new RuntimeException("仧 templateName is NULL");
      }
      throw new RuntimeException("仧 TemplateConfig is NULL");
   }

   private MailAgent setTemplate(MailAgent mailAgent,TemplateConfig templateConfig,String templateName){
      Properties prop = new Properties();
      prop.setProperty("resource.loader", "file,class");
      prop.setProperty("file.resource.loader.class", FileResourceLoader.class.getName());
      prop.setProperty("file.resource.loader.path",templateConfig.getTemplateLoadpath());
      prop.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
      prop.setProperty("input.encoding",templateConfig.getTemplateInEncode());
      prop.setProperty("output.encoding",templateConfig.getTemplateOutEncode());
      // velocity 儘僌傪弌椡偝偣側偄愝掕両
      prop.setProperty("runtime.log.logsystem.class","org.apache.velocity.runtime.log.NullLogSystem");
      // Velocity弶婜壔
      VelocityEngine engine = new VelocityEngine();
      try{
      engine.init(prop);

      StringBuffer sb = new StringBuffer();
      sb.append("仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭"+"\n");
      sb.append("\t\t"+"仭 --------------- Velocity init  ---------------\n");
      sb.append("\t\t"+"仭 resource.loader             = "+engine.getProperty("resource.loader")+"\n");
      sb.append("\t\t"+"仭 file.resource.loader.class  = "+engine.getProperty("file.resource.loader.class")+"\n");
      sb.append("\t\t"+"仭 file.resource.loader.path   = "+engine.getProperty("file.resource.loader.path")+"\n");
      sb.append("\t\t"+"仭 class.resource.loader.class = "+engine.getProperty("class.resource.loader.class")+"\n");
      sb.append("\t\t"+"仭 input.encoding   = "+engine.getProperty("input.encoding")+"\n");
      sb.append("\t\t"+"仭 output.encoding  = "+engine.getProperty("output.encoding")+"\n");
      sb.append("\t\t"+"仭 runtime.log.logsystem.class = "+engine.getProperty("runtime.log.logsystem.class")+"\n");
      Logger logger = Logger.getLogger("mailagent");
      logger.debug(sb.toString());
      logger.debug("仭 template set = "+templateName);

      mailAgent.setTemplate(engine.getTemplate(templateName));
      return mailAgent;
      }catch(Exception e){
         throw new RuntimeException("仧 Velocity init error Exception : "+e.getMessage(),e);
      }
   }
}
