package artistmetrics.views;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import artistmetrics.actions.User;




/**
 * This sample class demonstrates how to plug-in a new
 * workbench view. The view shows data obtained from the
 * model. The sample creates a dummy model on the fly,
 * but a real implementation would connect 		to the model
 * available either in this or another plug-in (e.g. the workspace).
 * The view is connectedT.RIGHT to the model using a content provider.
 * <p>
      }
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */

public class ThumbsView extends ViewPart{

	private Button up;
	private Button down;
	private Button refreshView;
	private Label classText;
	private Label voteText;
	private Label tallyText;
	private Label className;
	private Label tallyValue;
	private String classNameValue = "";


	/*
	 * The content provider class is responsible for
	 * providing objects to the view. It can wrap
	 * existing objects in adapters or simply return
	 * objects as-is. These objects may be sensitive
	 * to the current input of the view, or ignore
	 * it and always show the same content 
	 * (like Task List, for example).
	 */

	/**
	 * The constructor.
	 */

	public ThumbsView() {
	}
	
	public void buttonPressed(int value)
	{	
		//code what happens when button is pressed - input is 1 for thumbs up and 0 for thumbs down
		User user = User.getSystemUser();

		if(user == null || !user.hasServer())
			return;
		
		IWorkbench editors = PlatformUI.getWorkbench();
		IWorkbenchWindow window = editors.getActiveWorkbenchWindow();
		IWorkbenchPage activePage = window.getActivePage();
		
		if(activePage == null || activePage.getActiveEditor() == null)
			return;
		
		String name = activePage.getActiveEditor().getTitle();
		
		Map<String, String> data = new HashMap<String, String>();
		data.put("class", name);
		data.put("rating", "" + value);
		
		user.sendMetric("classrating", data);
		
		tallyValue.setText(readTally());
	}
	
	public String readTally()
	{
		IWorkbench editors = PlatformUI.getWorkbench();
		IWorkbenchWindow window = editors.getActiveWorkbenchWindow();
		IWorkbenchPage activePage = window.getActivePage();
		
		if(activePage == null || activePage.getActiveEditor() == null)
			return "0";
		
		String name = activePage.getActiveEditor().getTitle();
		
		
		User user = User.getSystemUser();
		
		if(user == null || !user.hasServer())
			return "No connection to server.";
		
		NodeList allMetrics = user.getMetrics().getElementsByTagName("metric");
		int tally = 0;
		int count = 0;
		
		for(int i = 0; i < allMetrics.getLength(); i++)
		{
			Node metric = allMetrics.item(i);
			String nodeText = metric.getTextContent();
			
			if(nodeText.contains("classrating") && nodeText.contains(name))
			{
				if(nodeText.contains("  1"))
					tally++;
				count++;
			}
		}
		
		float average = 0;
		if(count != 0)
			average = (float)tally / (float)count;
		
		return "" + average;
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {
		try {
		IWorkbench editors = PlatformUI.getWorkbench();
		IWorkbenchWindow window = editors.getActiveWorkbenchWindow();
		IWorkbenchPage activePage = window.getActivePage();
		
		FormLayout layout = new FormLayout();
		parent.setLayout(layout);		
		FormData data = new FormData();
		
		if(activePage != null && !(activePage.getActiveEditor() == null))
			classNameValue = activePage.getActiveEditor().getTitle();
		
		classText = new Label(parent, SWT.PUSH);
		classText.setText("Class/Method title:");
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.top = new FormAttachment(0, 0);
		classText.setLayoutData(data);
		
		voteText = new Label(parent, SWT.PUSH);
		voteText.setText("Vote:");
		data = new FormData();;
		data.left = new FormAttachment(50, -15);
		data.top = new FormAttachment(0, 0);
		voteText.setLayoutData(data);
		
		tallyText = new Label(parent, SWT.PUSH);
		tallyText.setText("Tally:");
		data = new FormData();
		data.right = new FormAttachment(100,-10);
		data.top = new FormAttachment(0, 0);
		tallyText.setLayoutData(data);
		
		className = new Label(parent, SWT.PUSH);
		className.setText(classNameValue);
		data = new FormData();
		data.left = new FormAttachment(0,0);
		data.top = new FormAttachment(classText, 0);
		data.width = 150;
		className.setLayoutData(data);
		
		up = new Button(parent, SWT.PUSH);
		up.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				buttonPressed(1);
			}
		});
		up.setText("Up");
		data = new FormData();
		data.width = 60;
		data.right = new FormAttachment(50,0);
		data.top = new FormAttachment(classText, 0);
		up.setLayoutData(data);
		
		down = new Button(parent, SWT.PUSH);
		down.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				buttonPressed(0);
			}
		});
		
		down.setText("Down");
		data = new FormData();
		data.width = 60;
		data.left = new FormAttachment(50,0);
		data.top = new FormAttachment(classText, 0);
		down.setLayoutData(data);
		
		refreshView = new Button(parent, SWT.PUSH);
		refreshView.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				IWorkbench editors = PlatformUI.getWorkbench();
				IWorkbenchWindow window = editors.getActiveWorkbenchWindow();
				IWorkbenchPage activePage = window.getActivePage();
				className.setText(activePage.getActiveEditor().getTitle());
				
				tallyValue.setText(readTally());
			}
		});
		
		refreshView.setText("Refresh");
		data = new FormData();
		data.width = 60;
		data.left = new FormAttachment(100,-60);
		data.top = new FormAttachment(100,-28);
		refreshView.setLayoutData(data);

		tallyValue = new Label(parent, SWT.PUSH);
		tallyValue.setText(readTally());
		data = new FormData();
		data.right = new FormAttachment(100,-20);
		data.top = new FormAttachment(classText, 0);
		tallyValue.setLayoutData(data);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
	}
}
