//
// Created       : 2006 Jun 14 (Wed) 18:29:38 by Harold Carr.
// Last Modified : 2006 Aug 12 (Sat) 19:37:03 by Harold Carr.
//

package com.differentity.client;

import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Frame; // *****
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.Window; // *****

import com.differentity.client.Main;
import com.differentity.client.QueryRequest;
import com.differentity.client.Test; // *****


public class MainPanel
{
    private final SVOPanel subjectPanel;
    private final SVOPanel verbPanel;
    private final SVOPanel objectPanel;
    private final VerticalPanel subjectVerticalPanel;
    private final VerticalPanel verbVerticalPanel;
    private final VerticalPanel objectVerticalPanel;
    private final HorizontalPanel svoHorizontalPanel;
    private final DockPanel dockPanel;
    private final HTML north;
    private final HTML south;
    private final QueryPanel queryPanel;
    // TODO - there is a race here.
    private static final HTML statusHTML = new HTML();

    MainPanel() {

	//
	// QueryPanel created before SVO panels since needed.
	//

	queryPanel = new QueryPanel();

	//
	// Subject, Verb, Object panels.
	// Create now to get contents from server.
	//

	subjectPanel = new SVOPanel(Main.subject);
	verbPanel    = new SVOPanel(Main.verb);
	objectPanel  = new SVOPanel(Main.object);
	subjectVerticalPanel = subjectPanel.getPanel();
	verbVerticalPanel    = verbPanel.getPanel();
	objectVerticalPanel  = objectPanel.getPanel();
	svoHorizontalPanel = new HorizontalPanel();
	svoHorizontalPanel.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
	svoHorizontalPanel.add(subjectVerticalPanel);
	svoHorizontalPanel.add(verbVerticalPanel);
	svoHorizontalPanel.add(objectVerticalPanel);

	doQuery();

	//
	// Main panel.
	//    

	dockPanel = new DockPanel();
	dockPanel.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
	north = new HTML(Main.differentityDotCom, true);
	south = new HTML(Main.copyright, true);
	dockPanel.add(north, DockPanel.NORTH);

	dockPanel.add(queryPanel.getHorizontalPanel(), DockPanel.NORTH);
	// NOTE: - if SOUTH added after CENTER does not show up.
	dockPanel.add(south, DockPanel.SOUTH);
	dockPanel.add(svoHorizontalPanel, DockPanel.CENTER);

	// Host HTML has elements with IDs are "slot1", "slot2".
	// Better: Search for all elements with a particular CSS class 
	// and replace them with widgets.

	RootPanel.get("slot1").add(statusHTML);
	RootPanel.get("slot2").add(dockPanel);

	// XXX - frame test
	RootPanel.get("slot3").add(new Frame("http://www.google.com/"));

	// XXX - test
	RootPanel.get("slot4"). add(new Test().getWidget());
    }

    public void doQuery()
    {
	String subject = getSVOQueryValue("?s",queryPanel.getSubjectTextBox());
	String verb    = getSVOQueryValue("?v",queryPanel.getVerbTextBox());
	String object  = getSVOQueryValue("?o",queryPanel.getObjectTextBox());
	QueryRequest queryRequest = new QueryRequest(subject, verb, object);
	// "this" so async request can call handleQueryResponse.
	Main.getServerCalls().doQuery(this, queryRequest);
    }

    public void handleQueryResponse(QueryResponse queryResponse)
    {
	subjectPanel.setContents(queryResponse.getSubject());
	verbPanel.setContents(queryResponse.getVerb());
	objectPanel.setContents(queryResponse.getObject());
    }

    public void svoLinkClicked(final String categoryAndURL)
    {
	// TODO: Send to server.  Receive updates for other panels.
	int i = categoryAndURL.indexOf(" ");
	String category = categoryAndURL.substring(0, i);
	String url = categoryAndURL.substring(i+1);
	if (category.equals(Main.subject)) {
	    Main.getMainPanel()
		.getQueryPanel().getSubjectTextBox().setText(url);
	} else if (category.equals(Main.verb)) {
	    Main.getMainPanel()
		.getQueryPanel().getVerbTextBox().setText(url);
	} else if (category.equals(Main.object)) {
            Main.getMainPanel()
		.getQueryPanel().getObjectTextBox().setText(url);
	} else {
	    // TODO: FIX
	    Main.getMainPanel()
		.getQueryPanel().getSubjectTextBox().setText("ERROR");
	    return;
	}
	doQuery();
    }

    private String getSVOQueryValue(String def, TextBox textBox)
    {
	String text = textBox.getText();
	if (text != null && (! text.equals(""))) {
	    return text;
	}
	return def;
    }

    public QueryPanel getQueryPanel()   {return queryPanel;}
    public static HTML getStatusHTML()  {return statusHTML;}
}

// End of file.

@@ -99,16 +99,16 @@
 at) 
-19:37:03
+21:34:24
  by 
@@ -2936,16 +2936,22 @@
 bject = 
+%0A%09    
 getSVOQu
@@ -2963,13 +2963,22 @@
 lue(
-%22?s%22,
+Main.subject, 
 quer
@@ -3020,24 +3020,30 @@
 g verb    = 
+%0A%09    
 getSVOQueryV
@@ -3051,13 +3051,22 @@
 lue(
-%22?v%22,
+Main.verb,    
 quer
@@ -3109,16 +3109,22 @@
 ject  = 
+%0A%09    
 getSVOQu
@@ -3136,13 +3136,22 @@
 lue(
-%22?o%22,
+Main.object,  
 quer
@@ -3178,16 +3178,17 @@
 Box());%0A
+%0A
 %09QueryRe
@@ -3249,16 +3249,17 @@
 bject);%0A
+%0A
 %09// %22thi
@@ -3542,16 +3542,16 @@
 erb());%0A
-
 %09objectP
@@ -3591,24 +3591,77 @@
 tObject());%0A
+%09getStatusHTML().setHTML(queryResponse.getStatus());%0A
     %7D%0A%0A    p
@@ -4599,16 +4599,42 @@
 %0A%09return
+ Main.questionMarkSymbol +
  def;%0A  

["Refactoring.\n", ""]