package authoritymanager.client;

import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.XMLParser;

//private StatusPanel statusPanel = new StatusPanel(this, "data/status.xml");
public class StatisticsPage extends Page {
	private FlexTable table	;
	
	public StatisticsPage(AuthorityManagerView view) {
		super(view);
		
		table = new FlexTable() ; 
		table.setText(0, 0, "Filename");
		table.setText(0, 1, "Source");
		table.setText(0, 2, "Date of ingest");
		table.setText(0, 3, "Time of ingest");
		table.setText(0, 4, "View XML");

		table.setBorderWidth(2);

		Label lblTitle = new Label("Last data ingests");
		lblTitle.setSize("120px", "20px");

		VerticalPanel pnlMain = new VerticalPanel() ;
		pnlMain.add(lblTitle) ;
		pnlMain.add(table) ;
		add(pnlMain) ;
		
		update();
	}

	public void update() { 
		AsyncCallback<String> callback = new AsyncCallback<String>() {
		  @Override 
		  public void onFailure(Throwable caught) { }
		  
		  @Override 
		  public void onSuccess(String result) { 

				Document doc = XMLParser.parse(result) ; 
				  Element root = doc.getDocumentElement() ; 
				  NodeList repositories = root.getChildNodes() ; 
				  for (int iRep = 0 ; iRep < repositories.getLength() ; iRep ++) { 
					  Node curNode = repositories.item(iRep) ; 
					  if (curNode.getNodeType() == Node.ELEMENT_NODE) { 
						  Element rep = (Element) curNode ; 
						  final String fileName = rep.getElementsByTagName("filename").item(0).getChildNodes().item(0).getNodeValue().trim() ; 
						  String source = rep.getElementsByTagName("source").item(0).getChildNodes().item(0).getNodeValue().trim() ; 
						  String date = rep.getElementsByTagName("date").item(0).getChildNodes().item(0).getNodeValue().trim() ; 
						  String time = rep.getElementsByTagName("time").item(0).getChildNodes().item(0).getNodeValue().trim() ; 
						  final String dir = rep.getElementsByTagName("directory").item(0).getChildNodes().item(0).getNodeValue().trim() ;
						  table.setText(iRep + 1, 0, fileName) ; 
						  table.setText(iRep + 1, 1, source) ;
						  table.setText(iRep + 1, 2, date) ; 
						  table.setText(iRep + 1, 3, time) ; 
						  Label lblViewXML = new Label("[View XML]") ;
						  table.setWidget(iRep + 1, 4, lblViewXML) ; 
						  lblViewXML.addClickHandler(new ClickHandler() {		  
							  @Override 
							  public void onClick(ClickEvent event) { 
								  view.showXMLContentPage(dir + "/" + fileName) ;
							  } 
						  }) ; 
					  } 
				  } 
		  } 
		}; 
		view.getController().getStatus(callback) ;
		  
	}
}
