package com.interpress_project.modernshare.client.view.wizard;

import java.util.ArrayList;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.interpress_project.modernshare.client.controller.model.LocalModelManager;
import com.interpress_project.modernshare.client.controller.model.LocalRoot;
import com.interpress_project.modernshare.client.controller.model.SVNRoot;

/**
 * @stereotype Wizard page2
 */
public class RegWizardPage2 extends WizardPage {
	private final LocalModelManager lmmgr = LocalModelManager.getInstance();
	private final ArrayList<SVNRoot> tagNames;
	private final LocalRoot[] localRoots;
	private String tagName = "";

	/**
	 * RegWizardPage2
	 * @param tagNames
	 */
	public RegWizardPage2(ArrayList<SVNRoot> tagNames) {
		super("RegWizardPage2");

		this.setTitle("タグ名の指定");
		this.setDescription("登録するホットショット・フォルダに任意のタグ名を命名します。\n" + "ホットショット・フォルダに対応するデータセンタ領域はタグ名で識別することができます。");
		this.setPageComplete(false);

		this.tagNames = tagNames;
		this.localRoots = lmmgr.getRootLineItem().getRoots();
	}

	/**
	 * createControl
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;

		Label label = new Label(composite, SWT.NULL);
		label.setText("タグ名(データセンタ):");
		/**
		 * フォルダパスの入力ボックス
		 */
		final Text txtTag = new Text(composite, SWT.BORDER);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		txtTag.setLayoutData(gd);
		txtTag.setFocus();
		txtTag.addVerifyListener(new VerifyListener() {
			public void verifyText(VerifyEvent event) {
				switch (event.character) {
					default:
						pageComplete();
						break;

					case ' ': // space char
					case '*':
					case '?':
					case '&':
					case '/':
					case '\\':
					case '{':
					case '}':
					case '<':
					case '>':
					case '!':
					case '"':
					case '|':
					case '\'':
					case '`':
					case '+':
					case '^':
					case ':':
					case ';':
						pageIncomplete("無効な文字" + event.character + "が入力されました。");
						event.doit = false;
						break;
				}
			}
		});
		txtTag.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent event) {
				Text txtWidget = (Text) event.getSource();
				tagName = txtWidget.getText();
				if (findTagName(tagName)) {
					pageIncomplete(tagName + "は既にデータセンタに存在しています。他のタグ名を指定します。");
					return;
				}
				if (findLocalRoot(tagName)) {
					pageIncomplete(tagName + "は既にローカルのホットショット・リスト内に存在しています。他の名前を指定します。");
					return;
				}
				pageComplete();
			}
		});
		// Register controls.
		setControl(composite);
	}

	/**
	 * findTagName
	 * @param tagName
	 * @return
	 */
	private boolean findTagName(String tagName) {
		for (int i = 0; i < tagNames.size(); i++) {
			SVNRoot root = tagNames.get(i);
			if (root.getName().equalsIgnoreCase(tagName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * findLocalRoot
	 * @param tagName
	 * @return
	 */
	private boolean findLocalRoot(String tagName) {
		for (int i = 0; i < localRoots.length; i++) {
			if (localRoots[i].getName().equalsIgnoreCase(tagName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * getTagName
	 * 
	 * @return
	 */
	public String getTagName() {
		return tagName;
	}

	/**
	 * pageIncomplete
	 * @param msg
	 */
	protected void pageIncomplete(String msg) {
		setErrorMessage(msg);
		setPageComplete(false);
	}

	/**
	 * pageComplete
	 */
	protected void pageComplete() {
		setErrorMessage(null);
		setPageComplete(tagName.length() > 0);
	}

	/**
	 * getNextPage
	 *
	 * @return IWizardPage
	 */
	public IWizardPage getNextPage() {
		RegConfirmPage regConfirmPage = (RegConfirmPage) getWizard().getPage("RegConfirmPage");
		regConfirmPage.setTagName(this.getTagName());
		return super.getNextPage();
	}
}
