package com.lightdev.app.shtm;

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.Action;
import javax.swing.text.SimpleAttributeSet;

import com.hexidec.ekit.GSHTMLEditorFoucsActionListner;
import com.hexidec.ekit.GSHTMLEditorSearchActionListner;
import com.hexidec.ekit.UnicodeDecode;

import smart_gs.logical.SmartDocument;


public class SGSHTMLEditor extends SHTMLPanelMultipleDocImpl{

	protected SmartDocument document; // GSHTMLEditor

	public String findTerm; // GSHTMLEditor
	public String replaceTerm;
	public boolean bCaseSensitive;
	public int iOffset;
	public boolean forward;

	private SHTMLPanelImpl mainPane;
	private SGSHTMLEditorTextPane jtpMain;

	private Action jtpUndoAction;
	private Action jtpRedoAction;

	public SGSHTMLEditor(boolean showViewSource, boolean editModeExclusive,
			boolean debugMode) {
		this(null, showViewSource, editModeExclusive, debugMode);
	}
	public SGSHTMLEditor(String source, boolean showViewSource,
			boolean editModeExclusive, boolean debugMode) {
		mainPane = new SHTMLPanelSingleDocImpl();
		SHTMLPanelImpl.setTextResources(null);
		jtpUndoAction = super.getAction(SHTMLPanelImpl.undoAction);
		jtpRedoAction = super.getAction(SHTMLPanelImpl.redoAction);
		jtpMain = new SGSHTMLEditorTextPane(null, null);
//		jtpMain = mainPane.getSGSHTMLEditorTextPane();

		mainPane.getDocumentPane().setDocumentText(source);
		this.setLayout(new BorderLayout());
		this.add(mainPane, BorderLayout.CENTER);
	}
	
	// from GSHTMLEditor
	public void createHyperlink(String tagname, String valueOf) {
		// TODO Auto-generated method stub
		
	}
	public void highlightOff() {
		// TODO Auto-generated method stub
		
	}
	public int findText(String findTerm, String replaceTerm,
			boolean bCaseSensitive, int iOffset, boolean forward) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void findAllText(String findTerm, boolean bCaseSensitive) {
		// TODO Auto-generated method stub
		
	}

	public void emphasize(int start, int length) {
		// TODO Auto-generated method stub
		
	}
	public void emphasize(String id) {
		// TODO Auto-generated method stub
		
	}
	public SGSHTMLEditorTextPane getSGSHTMLEditorTextPane() {
		// TODO Auto-generated method stub
		return jtpMain;
	}
	public String getPlainText() {
		// TODO Auto-generated method stub
		return null;
	}
	public SimpleAttributeSet getSelectID(Point point) {
		// TODO Auto-generated method stub
		return null;
	}
	public SimpleAttributeSet getSelectIDFromPos(int caretPosition) {
		// TODO Auto-generated method stub
		return null;
	}
	public int getSelectionEnd() {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getSelectionStart() {
		// TODO Auto-generated method stub
		return 0;
	}

	public String getSource() {
		// TODO Auto-generated method stub
		return mainPane.getDocumentText();
	}
	public String getSource(String containingTag) {
		// TODO copy from GSHTMLEditor.java
//		return jtpMain.getText();
		String docTextCase;
		docTextCase = mainPane.getDocumentText();

		int tagStart = docTextCase.indexOf("<" + containingTag.toLowerCase());
		int tagStartClose = docTextCase.indexOf(">", tagStart) + 1;
		String closeTag = "</" + containingTag.toLowerCase() + ">";
		int tagEndOpen = docTextCase.indexOf(closeTag);
		if (tagStartClose < 0) {
			tagStartClose = 0;
		}
		if (tagEndOpen < 0 || tagEndOpen > docTextCase.length()) {
			tagEndOpen = docTextCase.length();
		}
		String bodyText;
		bodyText = docTextCase.substring(tagStartClose, tagEndOpen);
		bodyText = bodyText.trim();
		bodyText = bodyText.replaceAll("\r", "");
		bodyText = bodyText.replaceAll("\n", "");
		bodyText = bodyText.replaceAll("\r\n", "");
		bodyText = bodyText.replaceAll("&#160;", " ");
		bodyText = UnicodeDecode.decode(bodyText);
		return bodyText;
	}

	public void setReplaceSelection(String replaceTerm) {
		// TODO Auto-generated method stub
		
	}
	public int setReplaceSelection(int i, int j, String replace) {
		// TODO Auto-generated method stub
		return 0;
	}


	public SGSHTMLEditorTextPane getTextPane() {
		// TODO Auto-generated method stub
		return jtpMain;
	}

	public void setSelection(int i, int j) {
		// TODO Auto-generated method stub
		
	}

	public String getSelectURLStr(Point point) {
		// TODO Auto-generated method stub
		return null;
	}

	public void setURL(Point point, String registUrl) {
		// TODO Auto-generated method stub
		
	}

	public void removeURL(Point point) {
		// TODO Auto-generated method stub
		
	}

	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void requestFocusX() {
		// TODO Auto-generated method stub
		
	}
	public void setCaretPosition(int start) {
		// TODO Auto-generated method stub
		
	}
	public void setEdit(boolean b) {
		// TODO Auto-generated method stub
		
	}
	public void setFocusAction(GSHTMLEditorFoucsActionListner foucsAction) { // GSHTMLEditorFoucsActionListner is in ekit
		// TODO Auto-generated method stub
		
	}
	public Action getRedoAction() { // RedoAction is in ekit
		// TODO Auto-generated method stub
		return jtpRedoAction;
	}
	public Action getUndoAction() { // UndoAction is in ekit
		// TODO Auto-generated method stub
		return jtpUndoAction;
	}

	public void setMenuMode(boolean b) {
		// TODO Auto-generated method stub
		
	}
	public void setSearchAction(GSHTMLEditorSearchActionListner searchAction) { // GSHTMLEditorSearchActionListner is in ekit
		// TODO Auto-generated method stub
		
	}
	public void setSource(String source) {
		// TODO Auto-generated method stub
		
	}

	public void setSourceMode(boolean b) {
		// TODO Auto-generated method stub
		
	}
	public void learn() {
		// TODO Auto-generated method stub
		
	}
	public void startDocumentListener() {
		// TODO Auto-generated method stub
		
	}
	public void stopDocumentListener() {
		// TODO Auto-generated method stub
		
	}

}
