/*
 * Created on 2003/06/25
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.j69.eewiki.plugin;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.struts.action.ActionForm;
import org.apache.struts.util.MessageResources;

import org.j69.eewiki.action.PluginSubmitForm;
import org.j69.eewiki.util.WebUtil;
import org.j69.eewiki.wiki.Wiki;

/**
 * @author sally
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class PluginComment extends WikiPlugin {

	/* (non-Javadoc)
	 * @see org.j69.eewiki.plugin.WikiPluginImp#execute(java.lang.String, java.lang.String[], javax.servlet.http.HttpServletRequest)
	 */
	public String execute(
		String pageData,
		String[] optinos) {

		//ページID
		String pid = super.getPid();
		//コンテキストパス
		String submitPath = super.getContextPath() + "/PluginSubmit.do";
		//メッセージ
		MessageResources msg = super.getMessageResources();
		
		String comHTML = "<form name=\"comment\" method=\"POST\" action=\"" + submitPath + "\">\n";
		comHTML = comHTML + "<input type=\"hidden\" name=\"pid\" value=\"" + pid + "\">\n";
		comHTML = comHTML + "<input type=\"hidden\" name=\"plugin\" value=\"Comment\">\n";
		comHTML = comHTML + "<input type=\"hidden\" name=\"rowid\" value=\"" + super.getRowid() + "\">\n";
		comHTML = comHTML + msg.getMessage(super.getLocale(), "caption.name","") + ":<input type=\"text\" name=\"options\" size=\"15\">\n";
		comHTML = comHTML + "<input type=\"text\" name=\"options\" size=\"80\">\n";
		comHTML = comHTML + "<input type=\"submit\" name=\"comment\" value=\""+ msg.getMessage(super.getLocale(), "caption.comment","") +  "\">\n";
		comHTML = comHTML + "</form>\n";
		return comHTML;
	}

	/* (non-Javadoc)
	 * @see org.j69.eewiki.plugin.WikiPluginImp#submit(org.j69.eewiki.wiki.Wiki, org.apache.struts.action.ActionForm)
	 */
	public Wiki submit(Wiki wiki, ActionForm form) {
		
		PluginSubmitForm psForm = (PluginSubmitForm)form;
		//ページ内でコマンドが置かれている行
		int rowid = psForm.getRowid();
		//名前
		String name = psForm.getOptions()[0];
		//メッセージ
		String msg = WebUtil.escapeChar(psForm.getOptions()[1]);
		
		Calendar rightNow = Calendar.getInstance();
		String timestump = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(rightNow.getTime());
		try {
			//ページデータの取得
			String pageData = wiki.getPageContent();
			StringBuffer buffer = new StringBuffer();
			String[] datas = pageData.split("\n");
			for (int i = 0; i < datas.length; i++) {
				if (rowid == i) {
					if (!name.equals("") && !msg.equals("")) {
						buffer.append("-" + msg + " -- " + name + " #Size(1:" + timestump + ")\n");
					}
					buffer.append(datas[i] + "\n");
				}
				else {
					buffer.append(datas[i] + "\n");	
				}
				wiki.setPageContent(buffer.toString(), true);
			}
		} catch (IOException e) {
			e.getMessage();;
		}
		return wiki;
	}

}
